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
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]
const App = () => (
<div>
{'*TURING*(2019)!'.match(/([^A-Za-z*])/g)}
</div>
);
answer
(2019)!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); } ```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, []);
</details>
## Brackets
check("{()}[]") // true check("{[}]") // false
<details>
<summary>answer</summary>
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;
}
</details>
## Write sum function
`sum(1)(2)(5)(10) // 17`
<details>
<summary>answer</summary>
const sum = a => b => c => d => b + c + d;
</details>
## Write five, add, one functions
`five(add(one())) // 6`
<details>
<summary>answer</summary>
one = () => 1; add = a => b => a + b; five = fn => fn(5);
</details>
## Write seven, subtract, two functions
`seven(subtract(two())) // 5`
<details>
<summary>answer</summary>
two = () => 2; subtract = a => b => b - a; seven = fn => fn(7);
</details>
---
- https://projecteuler.net