RosettaCodeData/Task/Humble-numbers/Pluto/humble-numbers.pluto
2026-02-01 16:33:20 -08:00

31 lines
1.3 KiB
Text

do -- find some Humble numbers - numbers with no prime factors above 7
local maxHumble <const>, maxShownHumble <const> = 2048, 50
local h, hCount = {}, { [0] = 0, 0, 0, 0, 0, 0, 0 }
local p2, p3, p5, p7 = 2, 3, 5, 7
local last2, last3, last5, last7 = 1, 1, 1, 1
-- 1 is the first humble number ( 2^0 * 3^0 * 5^0 * 7^0 ) and has 1 digit
h[ 1 ], hCount[ 1 ] = 1, 1
io.write( "1" )
for n = 2, maxHumble do
-- the next humble number is the lowest of the next multiples of 2, 3, 5, 7
local m = math.min( p2, p3, p5, p7 )
h[ n ] = m
if n <= maxShownHumble then io.write( " ", m ) end
if m == p2 then last2 += 1 p2 = 2 * h[ last2 ] end
if m == p3 then last3 += 1 p3 = 3 * h[ last3 ] end
if m == p5 then last5 += 1 p5 = 5 * h[ last5 ] end
if m == p7 then last7 += 1 p7 = 7 * h[ last7 ] end
hCount[ ( m < 10 ) ? 1
: ( m < 100 ) ? 2
: ( m < 1_000 ) ? 3
: ( m < 10_000 ) ? 4
: ( m < 100_000 ) ? 5
: ( m < 1_000_000 ) ? 6
: 0
] += 1
end
io.write( "\n" )
for i = 1, 6 do
io.write( string.format( "There are %4d Humble numbers with %d digits\n", hCount[ i ], i ) )
end
end