Happy numbers: Difference between revisions

Add Miranda
(→‎{{header|Lambdatalk}}: adding lambdatalk task)
(Add Miranda)
Line 4,606:
{{out}}
<pre>First 8 happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show (take 8 happynumbers)))]
 
happynumbers :: [num]
happynumbers = filter ishappy [1..]
 
ishappy :: num->bool
ishappy n = 1 $in loop (iterate sumdigitsquares n)
 
sumdigitsquares :: num->num
sumdigitsquares 0 = 0
sumdigitsquares n = (n mod 10)^2 + sumdigitsquares (n div 10)
 
loop :: [*]->[*]
loop = loop' []
where loop' mem (a:as) = mem, if a $in mem
= loop' (a:mem) as, otherwise
 
in :: *->[*]->bool
in val [] = False
in val (a:as) = True, if a=val
= val $in as, otherwise</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|ML}}==
2,125

edits