17 lines
439 B
Text
17 lines
439 B
Text
procedure main(arglist)
|
|
local n
|
|
n := arglist[1] | 8 # limiting number of happy numbers to generate, default=8
|
|
writes("The first ",n," happy numbers are:")
|
|
every writes(" ", happy(seq()) \ n )
|
|
write()
|
|
end
|
|
|
|
procedure happy(i) #: returns i if i is happy
|
|
local n
|
|
|
|
if 4 ~= (0 <= i) then { # unhappy if negative, 0, or 4
|
|
if i = 1 then return i
|
|
every (n := 0) +:= !i ^ 2
|
|
if happy(n) then return i
|
|
}
|
|
end
|