
Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
const isPrime = (number) => {
if (number <= 1) {
return false
}
for (let i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false
}
}
return true
}
const summationOfPrimes = (number) => {
let sum = 0
for (let index = 1; index <= number; index++) {
if (isPrime(index)) {
sum += index
}
}
return sum
}
console.log(summationOfPrimes(2 * 1000000))
Related articles
Multiples of 3 or 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.
Sum square difference
The sum of the squares of the first ten natural numbers is