RosettaCodeData/Task/Euclid-Mullin-sequence/Pluto/euclid-mullin-sequence.pluto
2026-04-30 12:34:36 -04:00

22 lines
816 B
Text

-- find elements of the Euclid-Mullin sequence: starting from 2,
-- the next element is the smallest prime factor of 1 + the product
-- of the previous elements
do
local bigint = require( "pluto:bigint" ) -- use the standard bigint library
io.write( "2" )
local b0 <const>, b1 <const>, b2 <const>, b3 <const>
= new bigint( 0 ), new bigint( 1 ), new bigint( 2 ), new bigint( 3 )
local product = new bigint( 2 )
for _ = 2, 16 do
local nextV, p, found = product + b1, b3, false
-- find the first prime factor of nextV
while p * p <= nextV and not found do
found = nextV % p == b0
if not found then p += b2 end
end
if found then nextV = p end
io.write( $" { nextV:tostring() }" )
product *= nextV
end
end