RosettaCodeData/Task/Perfect-numbers/Pluto/perfect-numbers.pluto
2026-04-30 12:34:36 -04:00

30 lines
611 B
Text

local int = require "int"
local function is_perfect(n)
if n <= 2 then return false end
local tot = 1
for i = 2, math.floor(math.sqrt(n)) do
if n % i == 0 do
tot += i
local q = n // i
if q > i then tot += q end
end
end
return n == tot
end
print("The first seven perfect numbers are:")
local count = 0
local p = 2
while count < 7 do
local n = (1 << p) - 1
if int.isprime(n) then
n *= 1 << (p - 1)
if is_perfect(n) then
io.write($"{n} ")
count += 1
end
end
p += 1
end
print()