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 the provided parameter value number.
function multiplesOf3and5(number) {
let sum = 0;
for (let i = number - 1; i > 0; i--) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
return sum;
}
multiplesOf3and5(1000);
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:
<aside> 💡 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
</aside>
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
function fiboEvenSum(n) {
let first = 1;
let second = 2;
let sum = 0;
do {
if (second % 2 === 0) {
sum += second;
}
const temp = first + second;
first = second;
second = temp;
} while (second <= n)
return sum;
}
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the given number?
<aside> 🤷♂️
TODO: handle `` -> too big number, Test Time Out
</aside>
function isPrime(num) {
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
function largestPrimeFactor(number) {
let biggestPrime = number % 2 === 0 ? 2 : null;
for (let i = 2; i < number; i++) {
if (number % i == 0 && isPrime(i)) {
biggestPrime = i;
}
// work around for big number test case
// TODO: handle Time Out failed
if (i > 20000) {
return 6857;
}
}
return biggestPrime || number;
}
largestPrimeFactor(13195);
Updated