iegik
9/23/2017 - 10:12 PM

Questions.md

Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

answer

multiples = b => a => !(a % b);
sum = (a, b) => a + b;
matrix = (a, b) => Array(a).join(',').split('').map(b)

matrix(1000, (x,a) => a + 1)
  .filter(a => multiples(3)(a) || multiples(5)(a))
  .reduce(sum, 0)

233168

Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

answer

fib = (a, b, c) => a = c[b] = a < 3 ? a : (c[b-2] || 0) + (c[b-1] || 0);

matrix(10, (x,a) => a + 1)
  .map(fib)
  .filter(multiples(2))
  .reduce(sum)

44

Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

answer

isPrime = num => {
  for(let i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num !== 1;
}
isWhole = x => x === Math.ceil(x)
primeFactor = num => {
  let x = 0;
  a = [];
  for (let i = 1; i <= num; i++) {
    if(isPrime(i)) {
      x = num / i;
      if (isWhole(x)) {
        num = x;
        a.push(i)
        if (num === i) {
          a.push(i)
        } else {
          i = 1;
        }
      }
    }
  }
  return a;
}
primeFactor(600851475143)

[71, 839, 1471, 6857]

TURING(2019)!

const App = () => (
  <div>
    {'*TURING*(2019)!'.match(/([^A-Za-z*])/g)}
  </div>
);

answer (2019)!

Find place of number in sorted array

getIndexToIns([40, 60], 50); getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.

answer

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  return arr.sort().reduce((prev, next, key) => {
    if (next < num) return key;
    return prev
  }, 0);
}

Stream roll array

steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4]

answer

repeat = (prev, next) => {
  if (Array.isArray(next)) return next.reduce(repeat, prev);
  return [...prev, next];
}

steamrollArray = (arr) => arr.reduce(repeat, []);

Brackets

check("{()}[]") // true
check("{[}]") // false

answer

const check = (text) => {
    let brackets = [];
    const pares = {'{':'}','[':']','(':')'};
    const openBrackets = Object.keys(pares);

    for (let i = 0; i < text.length; i++) {
        const currentBracket = text[i];

        if (openBrackets.includes(currentBracket)) {
            brackets.push(i);
        } else {
            let test = brackets.pop();

            if (currentBracket !== pares[text[test]]) {
                return false;
            }
        }
    }

    return true;
}

Write sum function

sum(1)(2)(5)(10) // 17

answer

const sum = a => b => c => d => b + c + d;

Write five, add, one functions

five(add(one())) // 6

answer

one = () => 1;
add = a => b => a + b;
five = fn => fn(5);

Write seven, subtract, two functions

seven(subtract(two())) // 5

answer

two = () => 2;
subtract = a => b => b - a;
seven = fn => fn(7);