RosettaCodeData/Task/Attractive-numbers/Pluto/attractive-numbers.pluto
2025-08-11 18:05:26 -07:00

40 lines
1.1 KiB
Text

do -- find some attractive numbers - numbers whose prime factor count is prime
-- Returns true if x is prime, and false otherwise
function isPrime (x)
if x < 2 then return false end
if x < 4 then return true end
if x % 2 == 0 then return false end
for d = 3, math.sqrt(x), 2 do
if x % d == 0 then return false end
end
return true
end
-- Compute the prime factors of n
function factors (n)
local facList, divisor = {}, 1
if n > 1 then
while not isPrime(n) do
while not isPrime(divisor) do ++ divisor end
while n % divisor == 0 do
n //= divisor
facList:insert(divisor)
end
++ divisor
if n == 1 then return facList end
end
facList:insert(n)
end
return facList
end
-- Main procedure
local count = 0
for i = 1, 120 do
if isPrime(#factors(i)) then
++ count
io.write( string.format( "%6d", i ), if count % 12 == 0 then "\n" else "" end )
end
end
end