Posted 10 March 2014 - 07:22 AM
For a project I'm working on, I need to find the sum of the first 1000 prime number.
I know the answer should be: 3682913
But instead I'm getting: 3690837
The code I use is below.
I know the answer should be: 3682913
But instead I'm getting: 3690837
The code I use is below.
function primes(n)
if n<2 then
return false
elseif n==2 then
return true
elseif n%2==0 or n%3==0 then
return false
else
for i=3, math.ceil(n^0.5) do
if n%i==0 then
return false
end
end
end
return true
end
local count=0
local number=2
local sum=0
while count<1000 do
if primes(number) then
count=count+1
sum=sum+number
end
number=number+1
end
print(sum)
I ran a trace and found that just one sum down, I get, 3682913, which is just 3 off. Not sure if my problem has to do with determining primes in the first place, limitations of lua, or just simple errors. Help is appreciated.