Goldbach's comet: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(→‎{{header|Lua}}: added Lua solution)
Line 523:
The value of G(1000000) is 5402
</pre>
 
=={{header|Lua}}==
<lang lua>function T(t) return setmetatable(t, {__index=table}) end
table.range = function(t,n) local s=T{} for i=1,n do s[i]=i end return s end
table.map = function(t,f) local s=T{} for i=1,#t do s[i]=f(t[i]) end return s end
table.batch = function(t,n,f) for i=1,#t,n do local s=T{} for j=1,n do s[j]=t[i+j-1] end f(s) end return t end
 
function isprime(n)
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
if n % 3 == 0 then return n==3 end
for f = 5, n^0.5, 6 do
if n%f==0 or n%(f+2)==0 then return false end
end
return true
end
 
function goldbach(n)
local count = 0
for i = 1, n/2 do
if isprime(i) and isprime(n-i) then
count = count + 1
end
end
return count
end
 
print("The first 100 G numbers:")
g = T{}:range(100):map(function(n) return goldbach(2+n*2) end)
g:map(function(n) return string.format("%2d ",n) end):batch(10,function(t) print(t:concat()) end)
print("G(1000000) = "..goldbach(1000000))</lang>
{{out}}
<pre>The first 100 G numbers:
1 1 1 2 1 2 2 2 2 3
3 3 2 3 2 4 4 2 3 4
3 4 5 4 3 5 3 4 6 3
5 6 2 5 6 5 5 7 4 5
8 5 4 9 4 5 7 3 6 8
5 6 8 6 7 10 6 6 12 4
5 10 3 7 9 6 5 8 7 8
11 6 5 12 4 8 11 5 8 10
5 6 13 9 6 11 7 7 14 6
8 13 5 8 11 7 9 13 8 9
G(1000000) = 5402</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Anonymous user