I am trying to translate my prime number program I wrote in Lua into C++.
I am doing this because I believe it will help me learn C++, and also because I think that it will be faster.
I am an expert Lua programmer, so my program is somewhat complicated for efficiency. the program will calculate all prime numbers that it can up to a certain number, then print some output.
local g=1e6--goal: 1*10^6
local next=next--localize function
local k={2}--known primes
local c=1
local r=4
local s=os.clock()--start time
for n=3,g,2 do
local p=true--n is prime by defaultfor i,q in next,k doif n%q==0 then
p=falsebreak
elseif i>c then
if n>r then
r=q^2
c=i
elsebreak
end
end
end
if p then
k[#k+1]=n--add n to the table
end
end
local e=os.clock()--completion time
print(#k.." in "..(e-s).."s @ "..math.floor(#k/(e-s)).."/s. Ratio:"..(g/#k))
Lua output:
78498 in 1.1s @ 71361/s. Ratio:12.739178068231