() => {
let sum = 0;
for (let i = 0; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
return sum;
}
() => {
let sum = 0;
let a = 0;
let b = 1;
let fib = 0;
while (fib < 4000000) {
fib = a + b;
a = b;
b = fib;
if (fib % 2 === 0) {
sum += fib;
}
}
return sum;
}
(n = 600851475143) => {
const factorize = num => {
let factorMap = {};
let n = num;
let i = 2;
const count = n => {
if (factorMap[n]) ++factorMap[n];
else factorMap[n] = 1;
};
while (i * i <= n) {
while (n % i === 0) {
n /= i;
count(i);
}
i++;
}
if (n !== num) {
if (n > 1) count(n);
} else {
count(num);
}
return factorMap;
};
let factorMap = factorize(n);
let max = 0;
for (let i in factorMap) {
max = Math.max(max, i);
}
return max;
};
() => {
let max = 0;
const isPalindrome = n => {
const str = n.toString();
if (
str ===
str
.split("")
.reverse()
.join("")
) {
return true;
}
return false;
};
for (let i = 999; i > 0; i--) {
for (let j = 999; j > 0; j--) {
const product = i * j;
if (product > max) {
if (isPalindrome(product)) {
max = product;
}
} else {
break;
}
}
}
return max;
}
() => {
const isGood = (val, n = 20) => {
for (let i = 2; i < n + 1; i++) {
if (val % i !== 0) {
return false;
}
}
return true;
};
let val = 1;
while (!isGood(val)) {
val++;
}
return val;
};
(n = 100) => {
let sumOfSquares = 0;
let sum = 0;
for (let i = 1; i < n + 1; i++) {
sumOfSquares += Math.pow(i, 2);
sum += i;
}
return Math.pow(sum, 2) - sumOfSquares;
};
(n = 10001) => {
const isPrime = n => {
if (n < 2) return false;
if (n % 2 === 0) return n === 2;
if (n % 3 === 0) return n === 3;
let squareRoot = Math.floor(1 + Math.sqrt(n));
let i = 5;
while (i <= squareRoot) {
if (n % i === 0) return false;
if (n % (i + 2) === 0) return false;
i += 6;
}
return true;
};
let primeCounter = 2;
let x = 2;
let answer = null;
while (primeCounter <= n) {
if (x > 2 && x % 2 !== 0) {
if (isPrime(x)) {
primeCounter++;
answer = x;
}
}
x++;
}
return answer;
};
(span = 13, n = n1000) => {
const getProduct = arr => {
if (arr.includes("0")) return 0;
let product = 1;
while (arr.length) {
product *= parseInt(arr.pop());
}
return product;
};
const getMaxProduct = n => {
let max = 0;
let i = 0;
const arr = n.split("");
while (arr[i + span - 1]) {
const slice = arr.slice(i, i + span);
let product = -1;
if (slice.length === span && !slice.includes("0")) {
product = getProduct(slice);
}
if (product > max) {
max = product;
}
i++;
}
return max;
};
if (n.length % span === 0) {
return getMaxProduct(n);
}
return Math.max(getMaxProduct(n), getProduct(n.slice(-span).split("")));
};
(n = 1000) => {
for (let c = Math.floor(n / 3 + 1); c < n / 2; c++) {
let abSquared = c * c - n * n + 2 * n * c;
let abSquareRoot = Math.floor(Math.sqrt(abSquared));
if (abSquareRoot * abSquareRoot === abSquared) {
let b = (n - c + abSquareRoot) / 2;
let a = n - b - c;
return a * b * c;
}
}
return -1;
}
(n = 2000000) => {
let sieve = new Uint8Array(n + 1);
sieve[0] = 1;
sieve[1] = 1;
for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++) {
if (sieve[i] === 0) {
for (let j = i + i; j <= n; j += i) {
if (sieve[j] === 0) {
sieve[j] = 1;
}
}
}
}
return sieve.reduce((acc, curr, idx) => {
if (curr === 0) {
acc += idx;
}
return acc;
}, 0);
};