Happy numbers: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 1,598: | Line 1,598: | ||
(println (take 8 happy-numbers))</lang> |
(println (take 8 happy-numbers))</lang> |
||
Same output. |
Same output. |
||
=={{header|CLU}}== |
|||
<lang clu>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 </lang> |
|||
{{out}} |
|||
<pre>1 |
|||
7 |
|||
10 |
|||
13 |
|||
19 |
|||
23 |
|||
28 |
|||
31</pre> |
|||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |