36 lines
788 B
Text
36 lines
788 B
Text
sum_dig_sq = proc (n: int) returns (int)
|
|
sum_sq: int := 0
|
|
while n > 0 do
|
|
sum_sq := sum_sq + (n // 10) ** 2
|
|
n := n / 10
|
|
end
|
|
return (sum_sq)
|
|
end sum_dig_sq
|
|
|
|
is_happy = proc (n: int) returns (bool)
|
|
nn: int := sum_dig_sq(n)
|
|
while nn ~= n cand nn ~= 1 do
|
|
n := sum_dig_sq(n)
|
|
nn := sum_dig_sq(sum_dig_sq(nn))
|
|
end
|
|
return (nn = 1)
|
|
end is_happy
|
|
|
|
happy_numbers = iter (start, num: int) yields (int)
|
|
n: int := start
|
|
while num > 0 do
|
|
if is_happy(n) then
|
|
yield (n)
|
|
num := num-1
|
|
end
|
|
n := n+1
|
|
end
|
|
end happy_numbers
|
|
|
|
start_up = proc ()
|
|
po: stream := stream$primary_output()
|
|
|
|
for i: int in happy_numbers(1, 8) do
|
|
stream$putl(po, int$unparse(i))
|
|
end
|
|
end start_up
|