RosettaCodeData/Task/Euclid-Mullin-sequence/Lua/euclid-mullin-sequence-1.lua

19 lines
579 B
Lua
Raw Permalink Normal View History

2023-08-01 14:30:30 -07:00
-- 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
io.write( "2" )
local product = 2
2026-04-30 12:34:36 -04:00
for i = 2, 9 do
2026-02-01 16:33:20 -08:00
local nextV, p, found = product + 1, 3, false
2023-08-01 14:30:30 -07:00
-- find the first prime factor of nextV
while p * p <= nextV and not found do
found = nextV % p == 0
if not found then p = p + 2 end
end
if found then nextV = p end
io.write( " ", nextV )
product = product * nextV
end
end