RosettaCodeData/Task/Goldbachs-comet/Pluto/goldbachs-comet-2.pluto
2026-04-30 12:34:36 -04:00

69 lines
2.4 KiB
Text

do -- create a bitmap of Goldbach's Comet
local int = require( "int" ) -- RC Pluto integer library, inc. prime utilities
require( "bitmap" ) -- RC bitmap library
local bmp = bitmap.of( 400, 240, color.black, "Goldbachs_comet_Pluto" )
local maxG <const>, maxP <const> = 4000, 1_000_000
local rootMaxP <const>, halfMaxG <const> = math.floor( math.sqrt( maxP ) ), maxG // 2
local prime <const> = int.arecomps( maxP ):map( | v | -> not v ) -- sieve the primes to maxP
local G <const> = {} -- construct a table of G numbers
for i = 1, maxG do G[ i ] = 0 end
G[ 4 ] = 1 -- 4 is the only sum of 2 even primes
for i = 3, halfMaxG, 2 do
if prime[ i ] then
G[ i + i ] += 1
for j = i + 2, maxG - i do
if prime[ j ] then
G[ i + j ] += 1
end
end
end
end
-- print the first few G numbers to the bitmap - 8x8 charaxters
local c, tx, ty = 0, 2, 2
for i = 4, 202, 2 do
c += 1
bmp:text( string.format( "%3d", G[ i ] ), tx, ty, color.white, 1 )
if c % 10 != 0 then
tx += 24
else
tx, ty = 2, ty + 10
end
end
local Gm = 0
for i = 3, maxP // 2, 2 do
if prime[ i ] then
if prime[ maxP - i ] then Gm += 1 end
end
end
-- print G(1_000_000) to the bitmap
bmp:text( string.format( "G(%d): %d", maxP, Gm ), 2, ty + 10, color.white, 1 )
for i = 2, maxG - 10, 10 do
local maxY = 0
local x <const> = i // 10
for j = i, i + 8, 2 do
if G[ j ] > 0 then
local y <const> = 240 - G[ j ]
bmp:set( x, y, switch bmp:get( x, y ) do
case color.black -> color.orange
case color.orange -> color.yellow
default -> color.white
end
)
if y > maxY then maxY = y end
end
end
-- we're going to save it as a png, so convert from BGR to RGb
for j = 1, maxY do
local bgr <const> = bmp:get( x, j )
if bgr != color.black then
bmp:set( x, j, bmp.flipColor( bgr ) )
end
end
end
bmp:view( true )
end