JSFiddle - React, Tailwind, and code Playground
by ronilan
HTML
// Find the sum of all the prime numbers below two million.
// A prime number is any number only divisible by 1 and itself (except 1).
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
JavaScript
// Find the sum of all the prime numbers below two million.
// A prime number is any number only divisible by 1 and itself (except 1).
// The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
sumPrime = function (toWhere) {
var i,
j,
isPrime = true,
result = [2],
sum = 0;
for (i = 2; i <= toWhere; i++) {
j = result.length;
while (j--) {
// devisable
if (i % result[j] === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
result.push(i);
}
isPrime = true;
}
j = result.length;
while (j--) {
sum = sum + result[j];
}
return sum;
}
console.log(sumPrime(2000));