Hello. Recently I was solving Project Euler problem 10 (namely, summation of primes)
My code works fine, but it just doesn’t pass test 5 ( primeSummation(2000000)
should return 142913828922.)
function primeSummation(n) {
let primes = [2]
let nums = []
let i = 2
for (i = 2; i < n; i++) {
nums.push(i)
}
let shallowcopy = nums
for (i = 2; i < n; i++) {
shallowcopy.shift()
shallowcopy = shallowcopy.filter((num) => num % i != 0)
if (shallowcopy[0]) {
primes.push(shallowcopy[0])
}
}
let sum = primes.reduce((a,b) => a + b)
console.log(`The prime summation of ${n} is ${sum}`)
return sum
}
primeSummation(2000000)
This is solely because of the infinite loop detection thing.
Acording to the PE page:
Each problem has been designed according to a “one-minute rule”, which means that although it may take several hours to design a successful algorithm with more difficult problems, an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute.
Meanwhile, the infinite loop detection on the code editor fires after around 10 seconds in a loop or less
My suggestion is to increase this limit ONLY on PE to at least 40 seconds