Happy numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 24: Line 24:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F happy(=n)
<syntaxhighlight lang="11l">F happy(=n)
Set[Int] past
Set[Int] past
L n != 1
L n != 1
Line 33: Line 33:
R 1B
R 1B


print((0.<500).filter(x -> happy(x))[0.<8])</lang>
print((0.<500).filter(x -> happy(x))[0.<8])</syntaxhighlight>


{{out}}
{{out}}
Line 52: Line 52:
under 256, the cycle never goes above 163; this program could be trivially changed to print up to 39 happy numbers.
under 256, the cycle never goes above 163; this program could be trivially changed to print up to 39 happy numbers.


<lang 8080asm>flags: equ 2 ; 256-byte page in which to keep track of cycles
<syntaxhighlight lang="8080asm">flags: equ 2 ; 256-byte page in which to keep track of cycles
puts: equ 9 ; CP/M print string
puts: equ 9 ; CP/M print string
bdos: equ 5 ; CP/M entry point
bdos: equ 5 ; CP/M entry point
Line 128: Line 128:
adi 10
adi 10
ret ; 1s digit is left in A afterwards
ret ; 1s digit is left in A afterwards
string: db '000',13,10,'$'</lang>
string: db '000',13,10,'$'</syntaxhighlight>


{{out}}
{{out}}
Line 143: Line 143:


=={{header|8th}}==
=={{header|8th}}==
<lang 8th>
<syntaxhighlight lang="8th">
: until! "not while!" eval i;
: until! "not while!" eval i;


Line 173: Line 173:
;with
;with
;with
;with
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 181: Line 181:


=={{header|ACL2}}==
=={{header|ACL2}}==
<lang Lisp>(include-book "arithmetic-3/top" :dir :system)
<syntaxhighlight lang="lisp">(include-book "arithmetic-3/top" :dir :system)


(defun sum-of-digit-squares (n)
(defun sum-of-digit-squares (n)
Line 205: Line 205:


(defun first-happy-nums (n)
(defun first-happy-nums (n)
(first-happy-nums-r n 1))</lang>
(first-happy-nums-r n 1))</syntaxhighlight>
Output:
Output:
<pre>(1 7 10 13 19 23 28 31)</pre>
<pre>(1 7 10 13 19 23 28 31)</pre>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC SumOfSquares(BYTE x)
<syntaxhighlight lang="action!">BYTE FUNC SumOfSquares(BYTE x)
BYTE sum,d
BYTE sum,d


Line 260: Line 260:
x==+1
x==+1
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Happy_numbers.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Happy_numbers.png Screenshot from Atari 8-bit computer]
Line 275: Line 275:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang ActionScript>function sumOfSquares(n:uint)
<syntaxhighlight lang="actionscript">function sumOfSquares(n:uint)
{
{
var sum:uint = 0;
var sum:uint = 0;
Line 316: Line 316:
}
}
}
}
printHappy();</lang>
printHappy();</syntaxhighlight>
Sample output:
Sample output:
<pre>
<pre>
Line 330: Line 330:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Ordered_Sets;
with Ada.Containers.Ordered_Sets;


Line 370: Line 370:
end if;
end if;
end loop;
end loop;
end Test_Happy_Digits;</lang>
end Test_Happy_Digits;</syntaxhighlight>
Sample output:
Sample output:
<pre>
<pre>
Line 380: Line 380:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>INT base10 = 10, num happy = 8;
<syntaxhighlight lang="algol68">INT base10 = 10, num happy = 8;


PROC next = (INT in n)INT: (
PROC next = (INT in n)INT: (
Line 404: Line 404:
print((i, new line))
print((i, new line))
FI
FI
OD</lang>
OD</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 418: Line 418:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<lang algolm>begin
<syntaxhighlight lang="algolm">begin
integer function mod(a,b);
integer function mod(a,b);
integer a,b;
integer a,b;
Line 455: Line 455:
i := i + 1;
i := i + 1;
end;
end;
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 467: Line 467:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% find some happy numbers %
% find some happy numbers %
% returns true if n is happy, false otherwise; n must be >= 0 %
% returns true if n is happy, false otherwise; n must be >= 0 %
Line 520: Line 520:
end while_happyCount_lt_8
end while_happyCount_lt_8
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 529: Line 529:


===Tradfn===
===Tradfn===
<lang APL> ∇ HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
<syntaxhighlight lang="apl"> ∇ HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
[1] ⍝0: Happy number
[1] ⍝0: Happy number
[2] ⍝1: http://rosettacode.org/wiki/Happy_numbers
[2] ⍝1: http://rosettacode.org/wiki/Happy_numbers
Line 548: Line 548:
[17]
[17]
[18] ⎕←~∘0¨∆first↑bin/iroof ⍝ Show ∆first numbers, but not 0
[18] ⎕←~∘0¨∆first↑bin/iroof ⍝ Show ∆first numbers, but not 0
∇</lang>
∇</syntaxhighlight>
<pre>
<pre>
HappyNumbers 100 8
HappyNumbers 100 8
Line 555: Line 555:


===Dfn===
===Dfn===
<syntaxhighlight lang="apl">
<lang APL>
HappyNumbers←{ ⍝ return the first ⍵ Happy Numbers
HappyNumbers←{ ⍝ return the first ⍵ Happy Numbers
⍺←⍬ ⍝ initial list
⍺←⍬ ⍝ initial list
Line 570: Line 570:
HappyNumbers 8
HappyNumbers 8
1 7 10 13 19 23 28 31
1 7 10 13 19 23 28 31
</syntaxhighlight>
</lang>


=={{header|AppleScript}}==
=={{header|AppleScript}}==


===Iteration===
===Iteration===
<lang AppleScript>on run
<syntaxhighlight lang="applescript">on run
set howManyHappyNumbers to 8
set howManyHappyNumbers to 8
set happyNumberList to {}
set happyNumberList to {}
Line 606: Line 606:
end repeat
end repeat
return (numberToCheck = 1)
return (numberToCheck = 1)
end isHappy</lang>
end isHappy</syntaxhighlight>
<pre>
<pre>
Result: (*1, 7, 10, 13, 19, 23, 28, 31*)
Result: (*1, 7, 10, 13, 19, 23, 28, 31*)
Line 614: Line 614:
{{Trans|JavaScript}}
{{Trans|JavaScript}}
{{Trans|Haskell}}
{{Trans|Haskell}}
<lang AppleScript>---------------------- HAPPY NUMBERS -----------------------
<syntaxhighlight lang="applescript">---------------------- HAPPY NUMBERS -----------------------


-- isHappy :: Int -> Bool
-- isHappy :: Int -> Bool
Line 740: Line 740:
end tell
end tell
return v
return v
end |until|</lang>
end |until|</syntaxhighlight>
{{Out}}
{{Out}}
<lang AppleScript>{1, 7, 10, 13, 19, 23, 28, 31}</lang>
<syntaxhighlight lang="applescript">{1, 7, 10, 13, 19, 23, 28, 31}</syntaxhighlight>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 0 C = 8: DIM S(16):B = 10: PRINT "THE FIRST "C" HAPPY NUMBERS": FOR R = C TO 0 STEP 0:N = H: GOSUB 1: PRINT MID$ (" " + STR$ (H),1 + (R = C),255 * I);:R = R - I:H = H + 1: NEXT R: END
<syntaxhighlight lang="gwbasic"> 0 C = 8: DIM S(16):B = 10: PRINT "THE FIRST "C" HAPPY NUMBERS": FOR R = C TO 0 STEP 0:N = H: GOSUB 1: PRINT MID$ (" " + STR$ (H),1 + (R = C),255 * I);:R = R - I:H = H + 1: NEXT R: END
1 S = 0: GOSUB 3:I = N = 1: IF NOT Q THEN RETURN
1 S = 0: GOSUB 3:I = N = 1: IF NOT Q THEN RETURN
2 FOR Q = 1 TO 0 STEP 0:S(S) = N:S = S + 1: GOSUB 6:N = T: GOSUB 3: NEXT Q:I = N = 1: RETURN
2 FOR Q = 1 TO 0 STEP 0:S(S) = N:S = S + 1: GOSUB 6:N = T: GOSUB 3: NEXT Q:I = N = 1: RETURN
Line 751: Line 751:
4 Q = 0: FOR I = 0 TO S - 1: IF N = S(I) THEN RETURN
4 Q = 0: FOR I = 0 TO S - 1: IF N = S(I) THEN RETURN
5 NEXT I:Q = 1: RETURN
5 NEXT I:Q = 1: RETURN
6 T = 0: FOR I = N TO 0 STEP 0:M = INT (I / B):T = INT (T + (I - M * B) ^ 2):I = M: NEXT I: RETURN</lang>
6 T = 0: FOR I = N TO 0 STEP 0:M = INT (I / B):T = INT (T + (I - M * B) ^ 2):I = M: NEXT I: RETURN</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 761: Line 761:
{{trans|Nim}}
{{trans|Nim}}


<lang rebol>ord0: to :integer `0`
<syntaxhighlight lang="rebol">ord0: to :integer `0`
happy?: function [x][
happy?: function [x][
n: x
n: x
Line 781: Line 781:
loop 0..31 'x [
loop 0..31 'x [
if happy? x -> print x
if happy? x -> print x
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 795: Line 795:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Loop {
<syntaxhighlight lang="autohotkey">Loop {
If isHappy(A_Index) {
If isHappy(A_Index) {
out .= (out="" ? "" : ",") . A_Index
out .= (out="" ? "" : ",") . A_Index
Line 815: Line 815:
Return false
Return false
Else Return isHappy(sum, list)
Else Return isHappy(sum, list)
}</lang>
}</syntaxhighlight>
<pre>
<pre>
The first 8 happy numbers are: 1,7,10,13,19,23,28,31
The first 8 happy numbers are: 1,7,10,13,19,23,28,31
</pre>
</pre>
===Alternative version===
===Alternative version===
<lang AutoHotkey>while h < 8
<syntaxhighlight lang="autohotkey">while h < 8
if (Happy(A_Index)) {
if (Happy(A_Index)) {
Out .= A_Index A_Space
Out .= A_Index A_Space
Line 837: Line 837:
n := t, t := 0
n := t, t := 0
}
}
}</lang>
}</syntaxhighlight>
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>


=={{header|AutoIt}}==
=={{header|AutoIt}}==
<lang autoit>
<syntaxhighlight lang="autoit">
$c = 0
$c = 0
$k = 0
$k = 0
Line 864: Line 864:
EndIf
EndIf
WEnd
WEnd
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 880: Line 880:


===Alternative version===
===Alternative version===
<lang autoit>
<syntaxhighlight lang="autoit">
$c = 0
$c = 0
$k = 0
$k = 0
Line 905: Line 905:
$a.Clear
$a.Clear
WEnd
WEnd
</syntaxhighlight>
</lang>
<pre>
<pre>
Saves all numbers in a list, duplicate entry indicates a loop.
Saves all numbers in a list, duplicate entry indicates a loop.
Line 920: Line 920:


=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>function is_happy(n)
<syntaxhighlight lang="awk">function is_happy(n)
{
{
if ( n in happy ) return 1;
if ( n in happy ) return 1;
Line 960: Line 960:
}
}
}
}
}</lang>
}</syntaxhighlight>
Result:
Result:
<pre>1
<pre>1
Line 975: Line 975:
Alternately, for legibility one might write:
Alternately, for legibility one might write:


<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
for (i = 1; i < 50; ++i){
for (i = 1; i < 50; ++i){
if (isHappy(i)) {
if (isHappy(i)) {
Line 1,002: Line 1,002:
}
}
return tot
return tot
}</lang>
}</syntaxhighlight>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang freebasic>n = 1 : cnt = 0
<syntaxhighlight lang="freebasic">n = 1 : cnt = 0
print "The first 8 isHappy numbers are:"
print "The first 8 isHappy numbers are:"
print
print
Line 1,029: Line 1,029:
num = isHappy
num = isHappy
end while
end while
end function</lang>
end function</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
happy.bat
happy.bat
<lang dos>@echo off
<syntaxhighlight lang="dos">@echo off
setlocal enableDelayedExpansion
setlocal enableDelayedExpansion
::Define a list with 10 terms as a convenience for defining a loop
::Define a list with 10 terms as a convenience for defining a loop
Line 1,113: Line 1,113:
)
)
set /a n=sum
set /a n=sum
)</lang>
)</syntaxhighlight>
Sample usage and output
Sample usage and output
<pre>
<pre>
Line 1,173: Line 1,173:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> number% = 0
<syntaxhighlight lang="bbcbasic"> number% = 0
total% = 0
total% = 0
REPEAT
REPEAT
Line 1,193: Line 1,193:
num% = MOD(digit&())^2 + 0.5
num% = MOD(digit&())^2 + 0.5
UNTIL num% = 1 OR num% = 4
UNTIL num% = 1 OR num% = 4
= (num% = 1)</lang>
= (num% = 1)</syntaxhighlight>
Output:
Output:
<pre> 1 is a happy number
<pre> 1 is a happy number
Line 1,205: Line 1,205:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let sumdigitsq(n) =
let sumdigitsq(n) =
Line 1,229: Line 1,229:
$)
$)
wrch('*N')
wrch('*N')
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>


=={{header|Bori}}==
=={{header|Bori}}==
<lang bori>bool isHappy (int n)
<syntaxhighlight lang="bori">bool isHappy (int n)
{
{
ints cache;
ints cache;
Line 1,269: Line 1,269:
}
}
puts("First 8 happy numbers : " + str.newline + happynums);
puts("First 8 happy numbers : " + str.newline + happynums);
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>First 8 happy numbers :
<pre>First 8 happy numbers :
Line 1,275: Line 1,275:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>SumSqDgt ← +´2⋆˜ •Fmt-'0'˙
<syntaxhighlight lang="bqn">SumSqDgt ← +´2⋆˜ •Fmt-'0'˙
Happy ← ⟨⟩{𝕨((⊑∊˜ )◶⟨∾𝕊(SumSqDgt⊢),1=⊢⟩)𝕩}⊢
Happy ← ⟨⟩{𝕨((⊑∊˜ )◶⟨∾𝕊(SumSqDgt⊢),1=⊢⟩)𝕩}⊢
8↑Happy¨⊸/↕50</lang>
8↑Happy¨⊸/↕50</syntaxhighlight>
{{out}}
{{out}}
<pre>⟨ 1 7 10 13 19 23 28 31 ⟩</pre>
<pre>⟨ 1 7 10 13 19 23 28 31 ⟩</pre>


=={{header|Brat}}==
=={{header|Brat}}==
<lang brat>include :set
<syntaxhighlight lang="brat">include :set


happiness = set.new 1
happiness = set.new 1
Line 1,310: Line 1,310:
p "First eight happy numbers: #{happies}"
p "First eight happy numbers: #{happies}"
p "Happy numbers found: #{happiness.to_array.sort}"
p "Happy numbers found: #{happiness.to_array.sort}"
p "Sad numbers found: #{sadness.to_array.sort}"</lang>
p "Sad numbers found: #{sadness.to_array.sort}"</syntaxhighlight>
Output:
Output:
<pre>First eight happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]
<pre>First eight happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]
Line 1,318: Line 1,318:
=={{header|C}}==
=={{header|C}}==
Recursively look up if digit square sum is happy.
Recursively look up if digit square sum is happy.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


#define CACHE 256
#define CACHE 256
Line 1,350: Line 1,350:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
output<pre>1 7 10 13 19 23 28 31
output<pre>1 7 10 13 19 23 28 31
The 1000000th happy number: 7105849</pre>
The 1000000th happy number: 7105849</pre>
Without caching, using cycle detection:
Without caching, using cycle detection:
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int dsum(int n)
int dsum(int n)
Line 1,384: Line 1,384:


return 0;
return 0;
}</lang> Output is same as above, but much slower.
}</syntaxhighlight> Output is same as above, but much slower.


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 1,435: Line 1,435:
}
}
}
}
}</lang>
}</syntaxhighlight>
<pre>
<pre>
First 8 happy numbers : 1,7,10,13,19,23,28,31
First 8 happy numbers : 1,7,10,13,19,23,28,31
Line 1,442: Line 1,442:
===Alternate (cacheless)===
===Alternate (cacheless)===
Instead of caching and checking for being stuck in a loop, one can terminate on the "unhappy" endpoint of 89. One might be temped to try caching the so-far-found happy and unhappy numbers and checking the cache to speed things up. However, I have found that the cache implementation overhead reduces performance compared to this cacheless version.<br/>
Instead of caching and checking for being stuck in a loop, one can terminate on the "unhappy" endpoint of 89. One might be temped to try caching the so-far-found happy and unhappy numbers and checking the cache to speed things up. However, I have found that the cache implementation overhead reduces performance compared to this cacheless version.<br/>
Reaching 10 million, the <34 second computation time was from Tio.run. It takes under 5 seconds on a somewhat modern CPU. If you edit it to max out at 100 million, it takes about 50 seconds (on the somewhat modern CPU).<lang csharp>using System;
Reaching 10 million, the <34 second computation time was from Tio.run. It takes under 5 seconds on a somewhat modern CPU. If you edit it to max out at 100 million, it takes about 50 seconds (on the somewhat modern CPU).<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
class Program
class Program
Line 1,475: Line 1,475:
Console.WriteLine("\nComputation time {0} seconds.", (DateTime.Now - st).TotalSeconds);
Console.WriteLine("\nComputation time {0} seconds.", (DateTime.Now - st).TotalSeconds);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>---Happy Numbers---
<pre>---Happy Numbers---
Line 1,490: Line 1,490:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Python}}
{{trans|Python}}
<lang cpp>#include <map>
<syntaxhighlight lang="cpp">#include <map>
#include <set>
#include <set>


Line 1,525: Line 1,525:
std::cout << i << std::endl;
std::cout << i << std::endl;
return 0;
return 0;
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 1,539: Line 1,539:
49</pre>
49</pre>
Alternative version without caching:
Alternative version without caching:
<lang cpp>unsigned int happy_iteration(unsigned int n)
<syntaxhighlight lang="cpp">unsigned int happy_iteration(unsigned int n)
{
{
unsigned int result = 0;
unsigned int result = 0;
Line 1,579: Line 1,579:
}
}
std::cout << std::endl;
std::cout << std::endl;
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>1 7 10 13 19 23 28 31 </pre>
<pre>1 7 10 13 19 23 28 31 </pre>
Line 1,585: Line 1,585:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(defn happy? [n]
<syntaxhighlight lang="clojure">(defn happy? [n]
(loop [n n, seen #{}]
(loop [n n, seen #{}]
(cond
(cond
Line 1,599: Line 1,599:
(def happy-numbers (filter happy? (iterate inc 1)))
(def happy-numbers (filter happy? (iterate inc 1)))


(println (take 8 happy-numbers))</lang>
(println (take 8 happy-numbers))</syntaxhighlight>
Output:<pre>(1 7 10 13 19 23 28 31)</pre>
Output:<pre>(1 7 10 13 19 23 28 31)</pre>
===Alternate Version (with caching)===
===Alternate Version (with caching)===
<lang clojure>(require '[clojure.set :refer [union]])
<syntaxhighlight lang="clojure">(require '[clojure.set :refer [union]])


(def ^{:private true} cache {:happy (atom #{}) :sad (atom #{})})
(def ^{:private true} cache {:happy (atom #{}) :sad (atom #{})})
Line 1,636: Line 1,636:
(filter #(= :happy (happy-algo %)))))
(filter #(= :happy (happy-algo %)))))


(println (take 8 happy-numbers))</lang>
(println (take 8 happy-numbers))</syntaxhighlight>
Same output.
Same output.


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>sum_dig_sq = proc (n: int) returns (int)
<syntaxhighlight lang="clu">sum_dig_sq = proc (n: int) returns (int)
sum_sq: int := 0
sum_sq: int := 0
while n > 0 do
while n > 0 do
Line 1,675: Line 1,675:
stream$putl(po, int$unparse(i))
stream$putl(po, int$unparse(i))
end
end
end start_up </lang>
end start_up </syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 1,687: Line 1,687:


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>happy = (n) ->
<syntaxhighlight lang="coffeescript">happy = (n) ->
seen = {}
seen = {}
while true
while true
Line 1,708: Line 1,708:
console.log i
console.log i
cnt += 1
cnt += 1
i += 1</lang>
i += 1</syntaxhighlight>
output
output
<pre>
<pre>
Line 1,723: Line 1,723:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun sqr (n)
<syntaxhighlight lang="lisp">(defun sqr (n)
(* n n))
(* n n))


Line 1,744: Line 1,744:
(print (happys))
(print (happys))
</syntaxhighlight>
</lang>


Output:<pre>(1 7 10 13 19 23 28 31)</pre>
Output:<pre>(1 7 10 13 19 23 28 31)</pre>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub sumDigitSquare(n: uint8): (s: uint8) is
sub sumDigitSquare(n: uint8): (s: uint8) is
Line 1,786: Line 1,786:
end if;
end if;
n := n + 1;
n := n + 1;
end loop;</lang>
end loop;</syntaxhighlight>


{{out}}
{{out}}
Line 1,801: Line 1,801:
=={{header|Crystal}}==
=={{header|Crystal}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>def happy?(n)
<syntaxhighlight lang="ruby">def happy?(n)
past = [] of Int32 | Int64
past = [] of Int32 | Int64
until n == 1
until n == 1
Line 1,814: Line 1,814:
until count == 8; (puts i; count += 1) if happy?(i += 1) end
until count == 8; (puts i; count += 1) if happy?(i += 1) end
puts
puts
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }</lang>
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,840: Line 1,840:


=={{header|D}}==
=={{header|D}}==
<lang d>bool isHappy(int n) pure nothrow {
<syntaxhighlight lang="d">bool isHappy(int n) pure nothrow {
int[int] past;
int[int] past;


Line 1,862: Line 1,862:


int.max.iota.filter!isHappy.take(8).writeln;
int.max.iota.filter!isHappy.take(8).writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
===Alternative Version===
===Alternative Version===
<lang d>import std.stdio, std.algorithm, std.range, std.conv, std.string;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.conv, std.string;


bool isHappy(int n) pure nothrow {
bool isHappy(int n) pure nothrow {
Line 1,884: Line 1,884:
void main() {
void main() {
int.max.iota.filter!isHappy.take(8).writeln;
int.max.iota.filter!isHappy.take(8).writeln;
}</lang>
}</syntaxhighlight>
Same output.
Same output.


=={{header|Dart}}==
=={{header|Dart}}==
<lang dart>main() {
<syntaxhighlight lang="dart">main() {
HashMap<int,bool> happy=new HashMap<int,bool>();
HashMap<int,bool> happy=new HashMap<int,bool>();
happy[1]=true;
happy[1]=true;
Line 1,922: Line 1,922:
i++;
i++;
}
}
}</lang>
}</syntaxhighlight>


=={{header|dc}}==
=={{header|dc}}==
<lang dc>[lcI~rscd*+lc0<H]sH
<syntaxhighlight lang="dc">[lcI~rscd*+lc0<H]sH
[0rsclHxd4<h]sh
[0rsclHxd4<h]sh
[lIp]s_
[lIp]s_
0sI[lI1+dsIlhx2>_z8>s]dssx</lang>
0sI[lI1+dsIlhx2>_z8>s]dssx</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 1,940: Line 1,940:


=={{header|DCL}}==
=={{header|DCL}}==
<lang DCL>$ happy_1 = 1
<syntaxhighlight lang="dcl">$ happy_1 = 1
$ found = 0
$ found = 0
$ i = 1
$ i = 1
Line 1,989: Line 1,989:
$ goto loop1
$ goto loop1
$ done:
$ done:
$ show symbol found*</lang>
$ show symbol found*</syntaxhighlight>
{{out}}
{{out}}
<pre> FOUND = 8 Hex = 00000008 Octal = 00000000010
<pre> FOUND = 8 Hex = 00000008 Octal = 00000000010
Line 2,004: Line 2,004:
{{libheader| Boost.Int}}
{{libheader| Boost.Int}}
Adaptation of [[#Pascal]]. The lib '''Boost.Int''' can be found here [https://github.com/MaiconSoft/DelphiBoostLib]
Adaptation of [[#Pascal]]. The lib '''Boost.Int''' can be found here [https://github.com/MaiconSoft/DelphiBoostLib]
<syntaxhighlight lang="delphi">
<lang Delphi>
program Happy_numbers;
program Happy_numbers;


Line 2,068: Line 2,068:
writeln;
writeln;
readln;
readln;
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>
=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec dsumsq(byte n) byte:
<syntaxhighlight lang="draco">proc nonrec dsumsq(byte n) byte:
byte r, d;
byte r, d;
r := 0;
r := 0;
Line 2,105: Line 2,105:
n := n + 1
n := n + 1
od
od
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 2,117: Line 2,117:


=={{header|DWScript}}==
=={{header|DWScript}}==
<lang delphi>function IsHappy(n : Integer) : Boolean;
<syntaxhighlight lang="delphi">function IsHappy(n : Integer) : Boolean;
var
var
cache : array of Integer;
cache : array of Integer;
Line 2,146: Line 2,146:
Dec(n);
Dec(n);
end;
end;
end;</lang>
end;</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 2,159: Line 2,159:
=={{header|Dyalect}}==
=={{header|Dyalect}}==


<lang dyalect>func happy(n) {
<syntaxhighlight lang="dyalect">func happy(n) {
var m = []
var m = []
while n > 1 {
while n > 1 {
Line 2,185: Line 2,185:
n += 1
n += 1
}
}
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 2,192: Line 2,192:


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==
<lang dejavu>next-num:
<syntaxhighlight lang="dejavu">next-num:
0
0
while over:
while over:
Line 2,228: Line 2,228:
++
++
drop
drop
drop</lang>
drop</syntaxhighlight>
{{output}}
{{output}}
<pre>A happy number: 1
<pre>A happy number: 1
Line 2,241: Line 2,241:
=={{header|E}}==
=={{header|E}}==
{{output?|E}}
{{output?|E}}
<lang e>def isHappyNumber(var x :int) {
<syntaxhighlight lang="e">def isHappyNumber(var x :int) {
var seen := [].asSet()
var seen := [].asSet()
while (!seen.contains(x)) {
while (!seen.contains(x)) {
Line 2,260: Line 2,260:
println(x)
println(x)
if ((count += 1) >= 8) { break }
if ((count += 1) >= 8) { break }
}</lang>
}</syntaxhighlight>


=={{header|Eiffel}}==
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
class
APPLICATION
APPLICATION
Line 2,335: Line 2,335:
end
end


</syntaxhighlight>
</lang>


=={{header|Elena}}==
=={{header|Elena}}==
{{trans|C#}}
{{trans|C#}}
ELENA 4.x :
ELENA 4.x :
<lang elena>import extensions;
<syntaxhighlight lang="elena">import extensions;
import system'collections;
import system'collections;
import system'routines;
import system'routines;
Line 2,383: Line 2,383:
};
};
console.printLine("First 8 happy numbers: ", happynums.asEnumerable())
console.printLine("First 8 happy numbers: ", happynums.asEnumerable())
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,390: Line 2,390:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Happy do
<syntaxhighlight lang="elixir">defmodule Happy do
def task(num) do
def task(num) do
Process.put({:happy, 1}, true)
Process.put({:happy, 1}, true)
Line 2,416: Line 2,416:
end
end


IO.inspect Happy.task(8)</lang>
IO.inspect Happy.task(8)</syntaxhighlight>


{{out}}
{{out}}
Line 2,424: Line 2,424:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>-module(tasks).
<syntaxhighlight lang="erlang">-module(tasks).
-export([main/0]).
-export([main/0]).
-import(lists, [map/2, member/2, sort/1, sum/1]).
-import(lists, [map/2, member/2, sort/1, sum/1]).
Line 2,456: Line 2,456:
main() ->
main() ->
main(0, []).
main(0, []).
</syntaxhighlight>
</lang>
Command: <lang Bash>erl -run tasks main -run init stop -noshell</lang>
Command: <syntaxhighlight lang="bash">erl -run tasks main -run init stop -noshell</syntaxhighlight>
Output: <lang Bash>8 Happy Numbers: [1,7,10,13,19,23,28,31]</lang>
Output: <syntaxhighlight lang="bash">8 Happy Numbers: [1,7,10,13,19,23,28,31]</syntaxhighlight>


In a more functional style (assumes integer_to_list/1 will convert to the ASCII value of a number, which then has to be converted to the integer value by subtracting 48):
In a more functional style (assumes integer_to_list/1 will convert to the ASCII value of a number, which then has to be converted to the integer value by subtracting 48):
<lang Erlang>-module(tasks).
<syntaxhighlight lang="erlang">-module(tasks).


-export([main/0]).
-export([main/0]).
Line 2,478: Line 2,478:
N_As_Digits = [Y - 48 || Y <- integer_to_list(N)],
N_As_Digits = [Y - 48 || Y <- integer_to_list(N)],
is_happy(lists:foldl(fun(X, Sum) -> (X * X) + Sum end, 0, N_As_Digits));
is_happy(lists:foldl(fun(X, Sum) -> (X * X) + Sum end, 0, N_As_Digits));
is_happy(_) -> false.</lang>
is_happy(_) -> false.</syntaxhighlight>
Output:
Output:
<pre>[1,7,10,13,19,23,28,31]</pre>
<pre>[1,7,10,13,19,23,28,31]</pre>


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang euphoria>function is_happy(integer n)
<syntaxhighlight lang="euphoria">function is_happy(integer n)
sequence seen
sequence seen
integer k
integer k
Line 2,511: Line 2,511:
end if
end if
n += 1
n += 1
end while</lang>
end while</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 2,525: Line 2,525:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This requires the F# power pack to be referenced and the 2010 beta of F#
This requires the F# power pack to be referenced and the 2010 beta of F#
<lang fsharp>open System.Collections.Generic
<syntaxhighlight lang="fsharp">open System.Collections.Generic
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Collections


Line 2,554: Line 2,554:
|> Seq.truncate 8 // Stop when we've found 8
|> Seq.truncate 8 // Stop when we've found 8
|> Seq.iter (Printf.printf "%d\n") // Print results
|> Seq.iter (Printf.printf "%d\n") // Print results
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,568: Line 2,568:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: combinators kernel make math sequences ;
<syntaxhighlight lang="factor">USING: combinators kernel make math sequences ;


: squares ( n -- s )
: squares ( n -- s )
Line 2,588: Line 2,588:
dup happy? [ dup , [ 1 - ] dip ] when 1 +
dup happy? [ dup , [ 1 - ] dip ] when 1 +
] while 2drop
] while 2drop
] { } make ;</lang>
] { } make ;</syntaxhighlight>
{{out}}
{{out}}
<lang factor>8 happy-numbers ! { 1 7 10 13 19 23 28 31 }</lang>
<syntaxhighlight lang="factor">8 happy-numbers ! { 1 7 10 13 19 23 28 31 }</syntaxhighlight>


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>[$10/$10*@\-$*\]m: {modulo squared and division}
<syntaxhighlight lang="false">[$10/$10*@\-$*\]m: {modulo squared and division}
[$m;![$9>][m;!@@+\]#$*+]s: {sum of squares}
[$m;![$9>][m;!@@+\]#$*+]s: {sum of squares}
[$0[1ø1>][1ø3+ø3ø=|\1-\]#\%]f: {look for duplicates}
[$0[1ø1>][1ø3+ø3ø=|\1-\]#\%]f: {look for duplicates}
Line 2,607: Line 2,607:
"Happy numbers:"
"Happy numbers:"
[1ø8=~][h;![" "$.\1+\]?1+]#
[1ø8=~][h;![" "$.\1+\]?1+]#
%%</lang>
%%</syntaxhighlight>


{{out}}
{{out}}
Line 2,613: Line 2,613:


=={{header|Fantom}}==
=={{header|Fantom}}==
<lang fantom>class Main
<syntaxhighlight lang="fantom">class Main
{
{
static Bool isHappy (Int n)
static Bool isHappy (Int n)
Line 2,648: Line 2,648:
}
}
}
}
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,662: Line 2,662:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 S J=0;S N=1;T %2
<syntaxhighlight lang="focal">01.10 S J=0;S N=1;T %2
01.20 D 3;I (K-2)1.5
01.20 D 3;I (K-2)1.5
01.30 S N=N+1
01.30 S N=N+1
Line 2,680: Line 2,680:
03.30 S S(K)=0
03.30 S S(K)=0
03.40 D 2;S K=R
03.40 D 2;S K=R
03.50 I (S(K))3.3</lang>
03.50 I (S(K))3.3</syntaxhighlight>


{{out}}
{{out}}
Line 2,694: Line 2,694:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: next ( n -- n )
<syntaxhighlight lang="forth">: next ( n -- n )
0 swap begin 10 /mod >r dup * + r> ?dup 0= until ;
0 swap begin 10 /mod >r dup * + r> ?dup 0= until ;


Line 2,713: Line 2,713:
loop drop ;
loop drop ;


8 happy-numbers \ 1 7 10 13 19 23 28 31</lang>
8 happy-numbers \ 1 7 10 13 19 23 28 31</syntaxhighlight>


===Lookup Table===
===Lookup Table===
Every sequence either ends in 1, or contains a 4 as part of a cycle. Extending the table through 9 is a (modest) optimization/memoization. This executes '500000 happy-numbers' about 5 times faster than the above solution.
Every sequence either ends in 1, or contains a 4 as part of a cycle. Extending the table through 9 is a (modest) optimization/memoization. This executes '500000 happy-numbers' about 5 times faster than the above solution.
<lang forth>CREATE HAPPINESS 0 C, 1 C, 0 C, 0 C, 0 C, 0 C, 0 C, 1 C, 0 C, 0 C,
<syntaxhighlight lang="forth">CREATE HAPPINESS 0 C, 1 C, 0 C, 0 C, 0 C, 0 C, 0 C, 1 C, 0 C, 0 C,
: next ( n -- n')
: next ( n -- n')
0 swap BEGIN dup WHILE 10 /mod >r dup * + r> REPEAT drop ;
0 swap BEGIN dup WHILE 10 /mod >r dup * + r> REPEAT drop ;
Line 2,726: Line 2,726:
BEGIN 1+ dup happy? UNTIL dup . r> 1- >r
BEGIN 1+ dup happy? UNTIL dup . r> 1- >r
REPEAT r> drop drop ;
REPEAT r> drop drop ;
8 happy-numbers</lang>
8 happy-numbers</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>
Produces the 1 millionth happy number with:
Produces the 1 millionth happy number with:
<lang forth>: happy-number ( n -- n') \ produce the nth happy number
<syntaxhighlight lang="forth">: happy-number ( n -- n') \ produce the nth happy number
>r 0 BEGIN r@ WHILE
>r 0 BEGIN r@ WHILE
BEGIN 1+ dup happy? UNTIL r> 1- >r
BEGIN 1+ dup happy? UNTIL r> 1- >r
REPEAT r> drop ;
REPEAT r> drop ;
1000000 happy-number . \ 7105849</lang>
1000000 happy-number . \ 7105849</syntaxhighlight>
in about 9 seconds.
in about 9 seconds.


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran>program happy
<syntaxhighlight lang="fortran">program happy


implicit none
implicit none
Line 2,804: Line 2,804:
end function is_happy
end function is_happy


end program happy</lang>
end program happy</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 2,816: Line 2,816:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function isHappy(n As Integer) As Boolean
Function isHappy(n As Integer) As Boolean
Line 2,859: Line 2,859:
Print
Print
Print "Press any key to quit"
Print "Press any key to quit"
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 2,878: Line 2,878:
{{Works with|Frege|3.21.586-g026e8d7}}
{{Works with|Frege|3.21.586-g026e8d7}}


<lang frege>module Happy where
<syntaxhighlight lang="frege">module Happy where


import Prelude.Math
import Prelude.Math
Line 2,894: Line 2,894:
f = sum . map (sqr . digitToInteger) . unpacked . show
f = sum . map (sqr . digitToInteger) . unpacked . show


main _ = putStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n</lang>
main _ = putStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n</syntaxhighlight>


{{out}}
{{out}}
Line 2,912: Line 2,912:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 2,940: Line 2,940:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,947: Line 2,947:


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>Number.metaClass.isHappy = {
<syntaxhighlight lang="groovy">Number.metaClass.isHappy = {
def number = delegate as Long
def number = delegate as Long
def cycle = new HashSet<Long>()
def cycle = new HashSet<Long>()
Line 2,961: Line 2,961:
if (i.happy) { matches << i }
if (i.happy) { matches << i }
}
}
println matches</lang>
println matches</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>


=={{header|Harbour}}==
=={{header|Harbour}}==
<lang xbase>PROCEDURE Main()
<syntaxhighlight lang="xbase">PROCEDURE Main()
LOCAL i := 8, nH := 0
LOCAL i := 8, nH := 0


Line 2,998: Line 2,998:
AAdd( aUnhappy, nSum )
AAdd( aUnhappy, nSum )


RETURN IsHappy( nSum )</lang>
RETURN IsHappy( nSum )</syntaxhighlight>
Output:
Output:


Line 3,005: Line 3,005:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Char (digitToInt)
<syntaxhighlight lang="haskell">import Data.Char (digitToInt)
import Data.Set (member, insert, empty)
import Data.Set (member, insert, empty)


Line 3,018: Line 3,018:


main :: IO ()
main :: IO ()
main = mapM_ print $ take 8 $ filter isHappy [1 ..]</lang>
main = mapM_ print $ take 8 $ filter isHappy [1 ..]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1
<pre>1
Line 3,030: Line 3,030:


We can create a cache for small numbers to greatly speed up the process:
We can create a cache for small numbers to greatly speed up the process:
<lang haskell>import Data.Array (Array, (!), listArray)
<syntaxhighlight lang="haskell">import Data.Array (Array, (!), listArray)


happy :: Int -> Bool
happy :: Int -> Bool
Line 3,048: Line 3,048:


main :: IO ()
main :: IO ()
main = print $ sum $ take 10000 $ filter happy [1 ..]</lang>
main = print $ sum $ take 10000 $ filter happy [1 ..]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>327604323</pre>
<pre>327604323</pre>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main(arglist)
<syntaxhighlight lang="icon">procedure main(arglist)
local n
local n
n := arglist[1] | 8 # limiting number of happy numbers to generate, default=8
n := arglist[1] | 8 # limiting number of happy numbers to generate, default=8
Line 3,069: Line 3,069:
if happy(n) then return i
if happy(n) then return i
}
}
end</lang>
end</syntaxhighlight>
Usage and Output:
Usage and Output:
<pre>
<pre>
Line 3,078: Line 3,078:


=={{header|J}}==
=={{header|J}}==
<lang j> 8{. (#~1=+/@(*:@(,.&.":))^:(1&~:*.4&~:)^:_ "0) 1+i.100
<syntaxhighlight lang="j"> 8{. (#~1=+/@(*:@(,.&.":))^:(1&~:*.4&~:)^:_ "0) 1+i.100
1 7 10 13 19 23 28 31</lang>
1 7 10 13 19 23 28 31</syntaxhighlight>
This is a repeat while construction
This is a repeat while construction
<lang j> f ^: cond ^: _ input</lang>
<syntaxhighlight lang="j"> f ^: cond ^: _ input</syntaxhighlight>
that produces an array of 1's and 4's, which is converted to 1's and 0's forming a binary array having a 1 for a happy number. Finally the happy numbers are extracted by a binary selector.
that produces an array of 1's and 4's, which is converted to 1's and 0's forming a binary array having a 1 for a happy number. Finally the happy numbers are extracted by a binary selector.
<lang j> (binary array) # 1..100</lang>
<syntaxhighlight lang="j"> (binary array) # 1..100</syntaxhighlight>
So for easier reading the solution could be expressed as:
So for easier reading the solution could be expressed as:
<lang j> cond=: 1&~: *. 4&~: NB. not equal to 1 and not equal to 4
<syntaxhighlight lang="j"> cond=: 1&~: *. 4&~: NB. not equal to 1 and not equal to 4
sumSqrDigits=: +/@(*:@(,.&.":))
sumSqrDigits=: +/@(*:@(,.&.":))


Line 3,091: Line 3,091:
14
14
8{. (#~ 1 = sumSqrDigits ^: cond ^:_ "0) 1 + i.100
8{. (#~ 1 = sumSqrDigits ^: cond ^:_ "0) 1 + i.100
1 7 10 13 19 23 28 31</lang>
1 7 10 13 19 23 28 31</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
{{trans|JavaScript}}
{{trans|JavaScript}}
<lang java5>import java.util.HashSet;
<syntaxhighlight lang="java5">import java.util.HashSet;
public class Happy{
public class Happy{
public static boolean happy(long number){
public static boolean happy(long number){
Line 3,122: Line 3,122:
}
}
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 3,137: Line 3,137:
{{works with|Java|1.8}}
{{works with|Java|1.8}}
{{trans|Java}}
{{trans|Java}}
<lang java>
<syntaxhighlight lang="java">


import java.util.Arrays;
import java.util.Arrays;
Line 3,164: Line 3,164:
return number == 1;
return number == 1;
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 3,179: Line 3,179:
===ES5===
===ES5===
====Iteration====
====Iteration====
<lang javascript>function happy(number) {
<syntaxhighlight lang="javascript">function happy(number) {
var m, digit ;
var m, digit ;
var cycle = [] ;
var cycle = [] ;
Line 3,204: Line 3,204:
document.write(number + " ") ;
document.write(number + " ") ;
number++ ;
number++ ;
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>1 7 10 13 19 23 28 31 </pre>
<pre>1 7 10 13 19 23 28 31 </pre>
Line 3,211: Line 3,211:
====Functional composition====
====Functional composition====
{{Trans|Haskell}}
{{Trans|Haskell}}
<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {


// isHappy :: Int -> Bool
// isHappy :: Int -> Bool
Line 3,270: Line 3,270:
take(8, filter(isHappy, enumFromTo(1, 50)))
take(8, filter(isHappy, enumFromTo(1, 50)))
);
);
})()</lang>
})()</syntaxhighlight>
{{Out}}
{{Out}}
<lang JavaScript>[1, 7, 10, 13, 19, 23, 28, 31]</lang>
<syntaxhighlight lang="javascript">[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>


Or, to stop immediately at the 8th member of the series, we can preserve functional composition while using an iteratively implemented '''until()''' function:
Or, to stop immediately at the 8th member of the series, we can preserve functional composition while using an iteratively implemented '''until()''' function:
<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {


// isHappy :: Int -> Bool
// isHappy :: Int -> Bool
Line 3,345: Line 3,345:
.xs
.xs
);
);
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<lang JavaScript>[1, 7, 10, 13, 19, 23, 28, 31]</lang>
<syntaxhighlight lang="javascript">[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|1.4}}
{{works with|jq|1.4}}
<lang jq>def is_happy_number:
<syntaxhighlight lang="jq">def is_happy_number:
def next: tostring | explode | map( (. - 48) | .*.) | add;
def next: tostring | explode | map( (. - 48) | .*.) | add;
def last(g): reduce g as $i (null; $i);
def last(g): reduce g as $i (null; $i);
Line 3,365: Line 3,365:
end
end
end );
end );
1 == last( [.,{}] | loop );</lang>
1 == last( [.,{}] | loop );</syntaxhighlight>
'''Emit a stream of the first n happy numbers''':
'''Emit a stream of the first n happy numbers''':
<lang jq># Set n to -1 to continue indefinitely:
<syntaxhighlight lang="jq"># Set n to -1 to continue indefinitely:
def happy(n):
def happy(n):
def subtask: # state: [i, found]
def subtask: # state: [i, found]
Line 3,378: Line 3,378:
[0,0] | subtask;
[0,0] | subtask;


happy($n|tonumber)</lang>
happy($n|tonumber)</syntaxhighlight>
{{out}}
{{out}}
<lang sh>$ jq --arg n 8 -n -f happy.jq
<syntaxhighlight lang="sh">$ jq --arg n 8 -n -f happy.jq
1
1
7
7
Line 3,389: Line 3,389:
28
28
31
31
</syntaxhighlight>
</lang>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>
<syntaxhighlight lang="julia">
function happy(x)
function happy(x)
happy_ints = ref(Int)
happy_ints = ref(Int)
Line 3,407: Line 3,407:
end
end
return happy_ints
return happy_ints
end</lang>
end</syntaxhighlight>
Output
Output
<pre> julia> happy(8)
<pre> julia> happy(8)
Line 3,420: Line 3,420:
31</pre>
31</pre>
A recursive version:
A recursive version:
<lang julia>sumhappy(n) = sum(x->x^2, digits(n))
<syntaxhighlight lang="julia">sumhappy(n) = sum(x->x^2, digits(n))


function ishappy(x, mem = [])
function ishappy(x, mem = [])
Line 3,431: Line 3,431:


happy(n) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
happy(n) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>julia> show(happy(8))
<pre>julia> show(happy(8))
Line 3,439: Line 3,439:
Faster with use of cache
Faster with use of cache
{{trans|C}}
{{trans|C}}
<lang julia>const CACHE = 256
<syntaxhighlight lang="julia">const CACHE = 256
buf = zeros(Int,CACHE)
buf = zeros(Int,CACHE)
buf[1] = 1
buf[1] = 1
Line 3,468: Line 3,468:
end
end
return i-1
return i-1
end</lang>
end</syntaxhighlight>


=={{header|K}}==
=={{header|K}}==
<lang k> hpy: {x@&1={~|/x=1 4}{_+/_sqr 0$'$x}//:x}
<syntaxhighlight lang="k"> hpy: {x@&1={~|/x=1 4}{_+/_sqr 0$'$x}//:x}


hpy 1+!100
hpy 1+!100
Line 3,477: Line 3,477:


8#hpy 1+!100
8#hpy 1+!100
1 7 10 13 19 23 28 31</lang>
1 7 10 13 19 23 28 31</syntaxhighlight>


Another implementation which is easy to follow is given below:
Another implementation which is easy to follow is given below:
<syntaxhighlight lang="k">
<lang K>
/ happynum.k
/ happynum.k


Line 3,490: Line 3,490:
hnum: {[x]; h::();i:1;while[(#h)<x; :[(isHappy i); h::(h,i)]; i+:1]; `0: ,"List of ", ($x), " Happy Numbers"; h}
hnum: {[x]; h::();i:1;while[(#h)<x; :[(isHappy i); h::(h,i)]; i+:1]; `0: ,"List of ", ($x), " Happy Numbers"; h}


</syntaxhighlight>
</lang>


The output of a session with this implementation is given below:
The output of a session with this implementation is given below:
Line 3,505: Line 3,505:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C#}}
{{trans|C#}}
<lang scala>// version 1.0.5-2
<syntaxhighlight lang="scala">// version 1.0.5-2


fun isHappy(n: Int): Boolean {
fun isHappy(n: Int): Boolean {
Line 3,534: Line 3,534:
}
}
println("First 8 happy numbers : " + happyNums.joinToString(", "))
println("First 8 happy numbers : " + happyNums.joinToString(", "))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,542: Line 3,542:


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang lasso>#!/usr/bin/lasso9
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9
define isHappy(n::integer) => {
define isHappy(n::integer) => {
Line 3,556: Line 3,556:
where isHappy(#x)
where isHappy(#x)
take 8
take 8
select #x</lang>
select #x</syntaxhighlight>
Output:
Output:
<lang lasso>1, 7, 10, 13, 19, 23, 28, 31</lang>
<syntaxhighlight lang="lasso">1, 7, 10, 13, 19, 23, 28, 31</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb> ct = 0
<syntaxhighlight lang="lb"> ct = 0
n = 0
n = 0
DO
DO
Line 3,588: Line 3,588:
sqrInts$ = sqrInts$ + Str$(sqrInts) + ":"
sqrInts$ = sqrInts$ + Str$(sqrInts) + ":"
HappyN = HappyN(sqrInts, sqrInts$)
HappyN = HappyN(sqrInts, sqrInts$)
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>
Output:-
Output:-
<pre>1 1
<pre>1 1
Line 3,602: Line 3,602:
=={{header|Locomotive Basic}}==
=={{header|Locomotive Basic}}==


<lang locobasic>10 mode 1:defint a-z
<syntaxhighlight lang="locobasic">10 mode 1:defint a-z
20 for i=1 to 100
20 for i=1 to 100
30 i2=i
30 i2=i
Line 3,617: Line 3,617:
140 ' check if we have reached 8 numbers yet
140 ' check if we have reached 8 numbers yet
150 if n=8 then end
150 if n=8 then end
160 next i</lang>
160 next i</syntaxhighlight>


[[File:Happy Numbers, Locomotive BASIC.png]]
[[File:Happy Numbers, Locomotive BASIC.png]]
Line 3,623: Line 3,623:
=={{header|Logo}}==
=={{header|Logo}}==


<lang logo>to sum_of_square_digits :number
<syntaxhighlight lang="logo">to sum_of_square_digits :number
output (apply "sum (map [[d] d*d] ` :number))
output (apply "sum (map [[d] d*d] ` :number))
end
end
Line 3,646: Line 3,646:


print n_happy 8
print n_happy 8
bye</lang>
bye</syntaxhighlight>


Output:
Output:
Line 3,654: Line 3,654:
{{works with|lci 0.10.3}}
{{works with|lci 0.10.3}}


<lang lolcode>OBTW
<syntaxhighlight lang="lolcode">OBTW
Happy Numbers Rosetta Code task in LOLCODE
Happy Numbers Rosetta Code task in LOLCODE
Requires 1.3 for BUKKIT availability
Requires 1.3 for BUKKIT availability
Line 3,736: Line 3,736:
OIC
OIC
IM OUTTA YR LOOP
IM OUTTA YR LOOP
KTHXBYE</lang>
KTHXBYE</syntaxhighlight>


Output:<pre>1
Output:<pre>1
Line 3,748: Line 3,748:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function digits(n)
<syntaxhighlight lang="lua">function digits(n)
if n > 0 then return n % 10, digits(math.floor(n/10)) end
if n > 0 then return n % 10, digits(math.floor(n/10)) end
end
end
Line 3,762: Line 3,762:
repeat
repeat
i, j = happy[j] and (print(j) or i+1) or i, j + 1
i, j = happy[j] and (print(j) or i+1) or i, j + 1
until i == 8</lang>
until i == 8</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 3,779: Line 3,779:




<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Function FactoryHappy {
Function FactoryHappy {
sumOfSquares= lambda (n) ->{
sumOfSquares= lambda (n) ->{
Line 3,812: Line 3,812:
PrintHappy=factoryHappy()
PrintHappy=factoryHappy()
Call PrintHappy()
Call PrintHappy()
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,826: Line 3,826:
=={{header|MAD}}==
=={{header|MAD}}==


<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
BOOLEAN CYCLE
BOOLEAN CYCLE
DIMENSION CYCLE(200)
DIMENSION CYCLE(200)
Line 3,854: Line 3,854:
END OF PROGRAM
END OF PROGRAM
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,869: Line 3,869:
=={{header|Maple}}==
=={{header|Maple}}==
To begin, here is a procedure to compute the sum of the squares of the digits of a positive integer. It uses the built-in procedure irem, which computes the integer remainder and, if passed a name as the optional third argument, assigns it the corresponding quotient. (In other words, it performs integer division with remainder. There is also a dual, companion procedure iquo, which returns the integer quotient and assigns the remainder to the (optional) third argument.)
To begin, here is a procedure to compute the sum of the squares of the digits of a positive integer. It uses the built-in procedure irem, which computes the integer remainder and, if passed a name as the optional third argument, assigns it the corresponding quotient. (In other words, it performs integer division with remainder. There is also a dual, companion procedure iquo, which returns the integer quotient and assigns the remainder to the (optional) third argument.)
<lang Maple>SumSqDigits := proc( n :: posint )
<syntaxhighlight lang="maple">SumSqDigits := proc( n :: posint )
local s := 0;
local s := 0;
local m := n;
local m := n;
Line 3,876: Line 3,876:
end do;
end do;
s
s
end proc:</lang>
end proc:</syntaxhighlight>
(Note that the unevaluation quotes on the third argument to irem are essential here, as that argument must be a name and, if m were passed without quotes, it would evaluate to a number.)
(Note that the unevaluation quotes on the third argument to irem are essential here, as that argument must be a name and, if m were passed without quotes, it would evaluate to a number.)


For example,
For example,
<syntaxhighlight lang="maple">
<lang Maple>
> SumSqDigits( 1234567890987654321 );
> SumSqDigits( 1234567890987654321 );
570
570
</syntaxhighlight>
</lang>
We can check this by computing it another way (more directly).
We can check this by computing it another way (more directly).
<syntaxhighlight lang="maple">
<lang Maple>
> n := 1234567890987654321:
> n := 1234567890987654321:
> `+`( op( map( parse, StringTools:-Explode( convert( n, 'string' ) ) )^~2) );
> `+`( op( map( parse, StringTools:-Explode( convert( n, 'string' ) ) )^~2) );
570
570
</syntaxhighlight>
</lang>
The most straight-forward way to check whether a number is happy or sad seems also to be the fastest (that I could think of).
The most straight-forward way to check whether a number is happy or sad seems also to be the fastest (that I could think of).
<lang Maple>Happy? := proc( n )
<syntaxhighlight lang="maple">Happy? := proc( n )
if n = 1 then
if n = 1 then
true
true
Line 3,903: Line 3,903:
evalb( s = 1 )
evalb( s = 1 )
end if
end if
end proc:</lang>
end proc:</syntaxhighlight>
We can use this to determine the number of happy (H) and sad (S) numbers up to one million as follows.
We can use this to determine the number of happy (H) and sad (S) numbers up to one million as follows.
<syntaxhighlight lang="maple">
<lang Maple>
> H, S := selectremove( Happy?, [seq]( 1 .. N ) ):
> H, S := selectremove( Happy?, [seq]( 1 .. N ) ):
> nops( H ), nops( S );
> nops( H ), nops( S );
143071, 856929
143071, 856929
</syntaxhighlight>
</lang>
Finally, to solve the stated problem, here is a completely straight-forward routine to locate the first N happy numbers, returning them in a set.
Finally, to solve the stated problem, here is a completely straight-forward routine to locate the first N happy numbers, returning them in a set.
<lang Maple>FindHappiness := proc( N )
<syntaxhighlight lang="maple">FindHappiness := proc( N )
local count := 0;
local count := 0;
local T := table();
local T := table();
Line 3,921: Line 3,921:
end do;
end do;
{seq}( T[ i ], i = 1 .. count )
{seq}( T[ i ], i = 1 .. count )
end proc:</lang>
end proc:</syntaxhighlight>
With input equal to 8, we get
With input equal to 8, we get
<syntaxhighlight lang="maple">
<lang Maple>
> FindHappiness( 8 );
> FindHappiness( 8 );
{1, 7, 10, 13, 19, 23, 28, 31}
{1, 7, 10, 13, 19, 23, 28, 31}
</syntaxhighlight>
</lang>
For completeness, here is an implementation of the cycle detection algorithm for recognizing happy numbers. It is much slower, however.
For completeness, here is an implementation of the cycle detection algorithm for recognizing happy numbers. It is much slower, however.
<lang Maple>Happy? := proc( n :: posint )
<syntaxhighlight lang="maple">Happy? := proc( n :: posint )
local a, b;
local a, b;
a, b := n, SumSqDigits( n );
a, b := n, SumSqDigits( n );
Line 3,936: Line 3,936:
end do;
end do;
evalb( a = 1 )
evalb( a = 1 )
end proc:</lang>
end proc:</syntaxhighlight>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Custom function HappyQ:
Custom function HappyQ:
<lang Mathematica>AddSumSquare[input_]:=Append[input,Total[IntegerDigits[Last[input]]^2]]
<syntaxhighlight lang="mathematica">AddSumSquare[input_]:=Append[input,Total[IntegerDigits[Last[input]]^2]]
NestUntilRepeat[a_,f_]:=NestWhile[f,{a},!MemberQ[Most[Last[{##}]],Last[Last[{##}]]]&,All]
NestUntilRepeat[a_,f_]:=NestWhile[f,{a},!MemberQ[Most[Last[{##}]],Last[Last[{##}]]]&,All]
HappyQ[a_]:=Last[NestUntilRepeat[a,AddSumSquare]]==1</lang>
HappyQ[a_]:=Last[NestUntilRepeat[a,AddSumSquare]]==1</syntaxhighlight>
Examples for a specific number:
Examples for a specific number:
<lang Mathematica>HappyQ[1337]
<syntaxhighlight lang="mathematica">HappyQ[1337]
HappyQ[137]</lang>
HappyQ[137]</syntaxhighlight>
gives back:
gives back:
<syntaxhighlight lang="mathematica">True
<lang Mathematica>True
False</lang>
False</syntaxhighlight>
Example finding the first 8:
Example finding the first 8:
<lang Mathematica>m = 8;
<syntaxhighlight lang="mathematica">m = 8;
n = 1;
n = 1;
i = 0;
i = 0;
Line 3,961: Line 3,961:
]
]
]
]
happynumbers</lang>
happynumbers</syntaxhighlight>
gives back:
gives back:
<lang Mathematica>{1, 7, 10, 13, 19, 23, 28, 31}</lang>
<syntaxhighlight lang="mathematica">{1, 7, 10, 13, 19, 23, 28, 31}</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Recursive version:
Recursive version:
<lang MATLAB>function findHappyNumbers
<syntaxhighlight lang="matlab">function findHappyNumbers
nHappy = 0;
nHappy = 0;
k = 1;
k = 1;
Line 3,988: Line 3,988:
hap = isHappyNumber(sum((sprintf('%d', k)-'0').^2), [prev k]);
hap = isHappyNumber(sum((sprintf('%d', k)-'0').^2), [prev k]);
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31 </pre>
<pre>1 7 10 13 19 23 28 31 </pre>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
fn isHappyNumber n =
fn isHappyNumber n =
(
(
Line 4,020: Line 4,020:
)
)
</syntaxhighlight>
</lang>
Output:
Output:
<syntaxhighlight lang="maxscript">
<lang MAXScript>
1
1
7
7
Line 4,031: Line 4,031:
28
28
31
31
</syntaxhighlight>
</lang>


=={{header|Mercury}}==
=={{header|Mercury}}==
<lang Mercury>:- module happy.
<syntaxhighlight lang="mercury">:- module happy.
:- interface.
:- interface.
:- import_module io.
:- import_module io.
Line 4,073: Line 4,073:
:- func sqr(int) = int.
:- func sqr(int) = int.


sqr(X) = X * X.</lang>
sqr(X) = X * X.</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
Line 4,079: Line 4,079:
=={{header|MiniScript}}==
=={{header|MiniScript}}==
This solution uses the observation that any infinite cycle of this algorithm hits the number 89, and so that can be used to know when we've found an unhappy number.
This solution uses the observation that any infinite cycle of this algorithm hits the number 89, and so that can be used to know when we've found an unhappy number.
<lang MiniScript>isHappy = function(x)
<syntaxhighlight lang="miniscript">isHappy = function(x)
while true
while true
if x == 89 then return false
if x == 89 then return false
Line 4,098: Line 4,098:
i = i + 1
i = i + 1
end while
end while
print "First 8 happy numbers: " + found</lang>
print "First 8 happy numbers: " + found</syntaxhighlight>
{{out}}
{{out}}
<pre>First 8 happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>First 8 happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]</pre>
Line 4,104: Line 4,104:
=={{header|ML}}==
=={{header|ML}}==
==={{header|mLite}}===
==={{header|mLite}}===
<lang ocaml>(*
<syntaxhighlight lang="ocaml">(*
A happy number is defined by the following process. Starting with any positive integer, replace the number
A happy number is defined by the following process. Starting with any positive integer, replace the number
by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will
by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will
Line 4,139: Line 4,139:


foreach (fn n = (print n; print " is "; println ` happy n)) ` iota 10;
foreach (fn n = (print n; print " is "; println ` happy n)) ` iota 10;
</syntaxhighlight>
</lang>
Output:
Output:
<pre>1 is happy
<pre>1 is happy
Line 4,153: Line 4,153:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE HappyNumbers;
<syntaxhighlight lang="modula2">MODULE HappyNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
FROM InOut IMPORT WriteCard, WriteLn;


Line 4,196: Line 4,196:
INC(num);
INC(num);
END;
END;
END HappyNumbers.</lang>
END HappyNumbers.</syntaxhighlight>
{{out}}
{{out}}
<pre> 1
<pre> 1
Line 4,208: Line 4,208:


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>ISHAPPY(N)
<syntaxhighlight lang="mumps">ISHAPPY(N)
;Determines if a number N is a happy number
;Determines if a number N is a happy number
;Note that the returned strings do not have a leading digit unless it is a happy number
;Note that the returned strings do not have a leading digit unless it is a happy number
Line 4,231: Line 4,231:
FOR I=1:1 QUIT:C<1 SET Q=+$$ISHAPPY(I) WRITE:Q !,I SET:Q C=C-1
FOR I=1:1 QUIT:C<1 SET Q=+$$ISHAPPY(I) WRITE:Q !,I SET:Q C=C-1
KILL I
KILL I
QUIT</lang>
QUIT</syntaxhighlight>
Output:<pre>
Output:<pre>
USER>D HAPPY^ROSETTA(8)
USER>D HAPPY^ROSETTA(8)
Line 4,253: Line 4,253:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
{{trans|REXX}}
{{trans|REXX}}
<lang netrexx>/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
<syntaxhighlight lang="netrexx">/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
limit = arg[0] /*get argument for LIMIT. */
limit = arg[0] /*get argument for LIMIT. */
say limit
say limit
Line 4,280: Line 4,280:
q=sum /*now, lets try the Q sum. */
q=sum /*now, lets try the Q sum. */
end
end
end</lang>
end</syntaxhighlight>
;Output
;Output
<pre>
<pre>
Line 4,398: Line 4,398:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import intsets
<syntaxhighlight lang="nim">import intsets


proc happy(n: int): bool =
proc happy(n: int): bool =
Line 4,417: Line 4,417:
for x in 0..31:
for x in 0..31:
if happy(x):
if happy(x):
echo x</lang>
echo x</syntaxhighlight>
Output:
Output:
<pre>1
<pre>1
Line 4,429: Line 4,429:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>use IO;
<syntaxhighlight lang="objeck">use IO;
use Structure;
use Structure;


Line 4,475: Line 4,475:
}
}
}
}
}</lang>
}</syntaxhighlight>
output:
output:
<pre>First 8 happy numbers: 1,7,10,13,19,23,28,31,</pre>
<pre>First 8 happy numbers: 1,7,10,13,19,23,28,31,</pre>
Line 4,481: Line 4,481:
=={{header|OCaml}}==
=={{header|OCaml}}==
Using [[wp:Cycle detection|Floyd's cycle-finding algorithm]].
Using [[wp:Cycle detection|Floyd's cycle-finding algorithm]].
<lang ocaml>open Num
<syntaxhighlight lang="ocaml">open Num


let step =
let step =
Line 4,505: Line 4,505:


List.iter print_endline (
List.iter print_endline (
List.rev_map string_of_num (first 8)) ;;</lang>
List.rev_map string_of_num (first 8)) ;;</syntaxhighlight>
Output:
Output:
<pre>$ ocaml nums.cma happy_numbers.ml
<pre>$ ocaml nums.cma happy_numbers.ml
Line 4,519: Line 4,519:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>: isHappy(n)
<syntaxhighlight lang="oforth">: isHappy(n)
| cycle |
| cycle |
ListBuffer new ->cycle
ListBuffer new ->cycle
Line 4,534: Line 4,534:
ListBuffer new ->numbers
ListBuffer new ->numbers
1 while(numbers size N <>) [ dup isHappy ifTrue: [ dup numbers add ] 1+ ]
1 while(numbers size N <>) [ dup isHappy ifTrue: [ dup numbers add ] 1+ ]
numbers println ;</lang>
numbers println ;</syntaxhighlight>


Output:
Output:
Line 4,543: Line 4,543:


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
count = 0
count = 0
say "First 8 happy numbers are:"
say "First 8 happy numbers are:"
Line 4,572: Line 4,572:
number = next
number = next
end
end
</syntaxhighlight>
</lang>
<pre>
<pre>
First 8 happy numbers are:
First 8 happy numbers are:
Line 4,586: Line 4,586:


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>functor
<syntaxhighlight lang="oz">functor
import
import
System
System
Line 4,629: Line 4,629:
in
in
{System.show {List.take HappyNumbers 8}}
{System.show {List.take HappyNumbers 8}}
end</lang>
end</syntaxhighlight>
Output:
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
<pre>[1 7 10 13 19 23 28 31]</pre>
Line 4,636: Line 4,636:
{{PARI/GP select}}
{{PARI/GP select}}
If the number has more than three digits, the sum of the squares of its digits has fewer digits than the number itself. If the number has three digits, the sum of the squares of its digits is at most 3 * 9^2 = 243. A simple solution is to look up numbers up to 243 and calculate the sum of squares only for larger numbers.
If the number has more than three digits, the sum of the squares of its digits has fewer digits than the number itself. If the number has three digits, the sum of the squares of its digits is at most 3 * 9^2 = 243. A simple solution is to look up numbers up to 243 and calculate the sum of squares only for larger numbers.
<lang parigp>H=[1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100,103,109,129,130,133,139,167,176,188,190,192,193,203,208,219,226,230,236,239];
<syntaxhighlight lang="parigp">H=[1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100,103,109,129,130,133,139,167,176,188,190,192,193,203,208,219,226,230,236,239];
isHappy(n)={
isHappy(n)={
if(n<262,
if(n<262,
Line 4,645: Line 4,645:
)
)
};
};
select(isHappy, vector(31,i,i))</lang>
select(isHappy, vector(31,i,i))</syntaxhighlight>
Output:
Output:
<pre>%1 = [1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>%1 = [1, 7, 10, 13, 19, 23, 28, 31]</pre>


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>Program HappyNumbers (output);
<syntaxhighlight lang="pascal">Program HappyNumbers (output);


uses
uses
Line 4,710: Line 4,710:
end;
end;
writeln;
writeln;
end.</lang>
end.</syntaxhighlight>
Output:
Output:
<pre>:> ./HappyNumbers
<pre>:> ./HappyNumbers
Line 4,721: Line 4,721:
Extended to 10e18
Extended to 10e18
Tested with Free Pascal 3.0.4
Tested with Free Pascal 3.0.4
<lang pascal>Program HappyNumbers (output);
<syntaxhighlight lang="pascal">Program HappyNumbers (output);
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI}
Line 4,970: Line 4,970:
writeln('Total time counting ',FormatDateTime('HH:NN:SS.ZZZ',now-T0));
writeln('Total time counting ',FormatDateTime('HH:NN:SS.ZZZ',now-T0));
end.
end.
</syntaxhighlight>
</lang>
;output:
;output:
<pre>
<pre>
Line 5,022: Line 5,022:
=={{header|Perl}}==
=={{header|Perl}}==
Since all recurrences end with 1 or repeat (37,58,89,145,42,20,4,16), we can do this test very quickly without having to make hashes of seen numbers.
Since all recurrences end with 1 or repeat (37,58,89,145,42,20,4,16), we can do this test very quickly without having to make hashes of seen numbers.
<lang perl>use List::Util qw(sum);
<syntaxhighlight lang="perl">use List::Util qw(sum);


sub ishappy {
sub ishappy {
Line 5,033: Line 5,033:


my $n = 0;
my $n = 0;
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";</lang>
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>
Line 5,039: Line 5,039:
Or we can solve using only the rudimentary task knowledge as below. Note the slightly different ways of doing the digit sum and finding the first 8 numbers where ishappy(n) is true -- this shows there's more than one way to do even these small sub-tasks.
Or we can solve using only the rudimentary task knowledge as below. Note the slightly different ways of doing the digit sum and finding the first 8 numbers where ishappy(n) is true -- this shows there's more than one way to do even these small sub-tasks.
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use List::Util qw(sum);
<syntaxhighlight lang="perl">use List::Util qw(sum);
sub is_happy {
sub is_happy {
my ($n) = @_;
my ($n) = @_;
Line 5,051: Line 5,051:


my $n;
my $n;
is_happy( ++$n ) and print "$n " or redo for 1..8;</lang>
is_happy( ++$n ) and print "$n " or redo for 1..8;</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>
Line 5,057: Line 5,057:
=={{header|Phix}}==
=={{header|Phix}}==
Copy of [[Happy_numbers#Euphoria|Euphoria]] tweaked to give a one-line output
Copy of [[Happy_numbers#Euphoria|Euphoria]] tweaked to give a one-line output
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">is_happy</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">is_happy</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">seen</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">seen</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 5,084: Line 5,084:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 5,092: Line 5,092:
=={{header|PHP}}==
=={{header|PHP}}==
{{trans|D}}
{{trans|D}}
<lang php>function isHappy($n) {
<syntaxhighlight lang="php">function isHappy($n) {
while (1) {
while (1) {
$total = 0;
$total = 0;
Line 5,115: Line 5,115:
}
}
$i++;
$i++;
}</lang>
}</syntaxhighlight>
<pre>1 7 10 13 19 23 28 31 </pre>
<pre>1 7 10 13 19 23 28 31 </pre>


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
println(happy_len(8)).
println(happy_len(8)).


Line 5,143: Line 5,143:
end,
end,
N := N + 1
N := N + 1
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 5,149: Line 5,149:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de happy? (N)
<syntaxhighlight lang="picolisp">(de happy? (N)
(let Seen NIL
(let Seen NIL
(loop
(loop
Line 5,161: Line 5,161:
(do 8
(do 8
(until (happy? (inc 'H)))
(until (happy? (inc 'H)))
(printsp H) ) )</lang>
(printsp H) ) )</syntaxhighlight>
Output:
Output:
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>


=={{header|PILOT}}==
=={{header|PILOT}}==
<lang pilot>C :max=8
<syntaxhighlight lang="pilot">C :max=8
:n=0
:n=0
:i=0
:i=0
Line 5,202: Line 5,202:
:x=y
:x=y
J (x):*digit
J (x):*digit
E :</lang>
E :</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 5,214: Line 5,214:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>test: proc options (main); /* 19 November 2011 */
<syntaxhighlight lang="pl/i">test: proc options (main); /* 19 November 2011 */
declare (i, j, n, m, nh initial (0) ) fixed binary (31);
declare (i, j, n, m, nh initial (0) ) fixed binary (31);


Line 5,238: Line 5,238:
end;
end;
end test;
end test;
</syntaxhighlight>
</lang>
OUTPUT:
OUTPUT:
<pre>
<pre>
Line 5,252: Line 5,252:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H:
<syntaxhighlight lang="plm">100H:


/* FIND SUM OF SQUARE OF DIGITS OF NUMBER */
/* FIND SUM OF SQUARE OF DIGITS OF NUMBER */
Line 5,322: Line 5,322:


CALL BDOS(0,0);
CALL BDOS(0,0);
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 5,334: Line 5,334:


=={{header|Potion}}==
=={{header|Potion}}==
<lang potion>sqr = (n): n * n.
<syntaxhighlight lang="potion">sqr = (n): n * n.


isHappy = (n) :
isHappy = (n) :
Line 5,353: Line 5,353:
if (isHappy(i)): firstEight append(i).
if (isHappy(i)): firstEight append(i).
.
.
firstEight string print</lang>
firstEight string print</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang PowerShell>function happy([int] $n) {
<syntaxhighlight lang="powershell">function happy([int] $n) {
$a=@()
$a=@()
for($i=2;$a.count -lt $n;$i++) {
for($i=2;$a.count -lt $n;$i++) {
Line 5,375: Line 5,375:
}
}
$a -join ','
$a -join ','
}</lang>
}</syntaxhighlight>
Output :
Output :
<lang PowerShell>happy(8)
<syntaxhighlight lang="powershell">happy(8)
7,10,13,19,23,28,31,32</lang>
7,10,13,19,23,28,31,32</syntaxhighlight>


=={{header|Prolog}}==
=={{header|Prolog}}==
{{Works with|SWI-Prolog}}
{{Works with|SWI-Prolog}}
<lang Prolog>happy_numbers(L, Nb) :-
<syntaxhighlight lang="prolog">happy_numbers(L, Nb) :-
% creation of the list
% creation of the list
length(L, Nb),
length(L, Nb),
Line 5,428: Line 5,428:


square(N, SN) :-
square(N, SN) :-
SN is N * N.</lang>
SN is N * N.</syntaxhighlight>
Output :
Output :
<lang Prolog> ?- happy_numbers(L, 8).
<syntaxhighlight lang="prolog"> ?- happy_numbers(L, 8).
L = [1,7,10,13,19,23,28,31].</lang>
L = [1,7,10,13,19,23,28,31].</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#ToFind=8
<syntaxhighlight lang="purebasic">#ToFind=8
#MaxTests=100
#MaxTests=100
#True = 1: #False = 0
#True = 1: #False = 0
Line 5,467: Line 5,467:
Until i>#MaxTests
Until i>#MaxTests
ProcedureReturn #False
ProcedureReturn #False
EndProcedure</lang>
EndProcedure</syntaxhighlight>
Sample output:
Sample output:
<pre>#1 1
<pre>#1 1
Line 5,480: Line 5,480:
=={{header|Python}}==
=={{header|Python}}==
===Procedural===
===Procedural===
<lang python>>>> def happy(n):
<syntaxhighlight lang="python">>>> def happy(n):
past = set()
past = set()
while n != 1:
while n != 1:
Line 5,490: Line 5,490:


>>> [x for x in xrange(500) if happy(x)][:8]
>>> [x for x in xrange(500) if happy(x)][:8]
[1, 7, 10, 13, 19, 23, 28, 31]</lang>
[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>


===Composition of pure functions===
===Composition of pure functions===


Drawing 8 terms from a non finite stream, rather than assuming prior knowledge of the finite sample size required:
Drawing 8 terms from a non finite stream, rather than assuming prior knowledge of the finite sample size required:
<lang python>'''Happy numbers'''
<syntaxhighlight lang="python">'''Happy numbers'''


from itertools import islice
from itertools import islice
Line 5,573: Line 5,573:


if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
Line 5,579: Line 5,579:
=={{header|Quackery}}==
=={{header|Quackery}}==


<syntaxhighlight lang="quackery">
<lang Quackery>
[ 0 swap
[ 0 swap
[ 10 /mod 2 **
[ 10 /mod 2 **
Line 5,601: Line 5,601:
drop nip ] is happies ( n --> [ )
drop nip ] is happies ( n --> [ )


8 happies echo</lang>
8 happies echo</syntaxhighlight>


{{Out}}
{{Out}}
Line 5,608: Line 5,608:


=={{header|R}}==
=={{header|R}}==
<lang R>is.happy <- function(n)
<syntaxhighlight lang="r">is.happy <- function(n)
{
{
stopifnot(is.numeric(n) && length(n)==1)
stopifnot(is.numeric(n) && length(n)==1)
Line 5,636: Line 5,636:
}
}
happy
happy
}</lang>
}</syntaxhighlight>
Example usage
Example usage
<lang R>is.happy(2)</lang>
<syntaxhighlight lang="r">is.happy(2)</syntaxhighlight>
[1] FALSE
[1] FALSE
attr(,"cycle")
attr(,"cycle")
[1] 4 16 37 58 89 145 42 20
[1] 4 16 37 58 89 145 42 20
<lang R>#Find happy numbers between 1 and 50
<syntaxhighlight lang="r">#Find happy numbers between 1 and 50
which(apply(rbind(1:50), 2, is.happy))</lang>
which(apply(rbind(1:50), 2, is.happy))</syntaxhighlight>
1 7 10 13 19 23 28 31 32 44 49
1 7 10 13 19 23 28 31 32 44 49
<lang R>#Find the first 8 happy numbers
<syntaxhighlight lang="r">#Find the first 8 happy numbers
happies <- c()
happies <- c()
i <- 1L
i <- 1L
Line 5,653: Line 5,653:
i <- i + 1L
i <- i + 1L
}
}
happies</lang>
happies</syntaxhighlight>
1 7 10 13 19 23 28 31
1 7 10 13 19 23 28 31


=={{header|Racket}}==
=={{header|Racket}}==
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (sum-of-squared-digits number (result 0))
(define (sum-of-squared-digits number (result 0))
(if (zero? number)
(if (zero? number)
Line 5,678: Line 5,678:
x))
x))


(display (take (get-happys 100) 8)) ;displays (1 7 10 13 19 23 28 31)</lang>
(display (take (get-happys 100) 8)) ;displays (1 7 10 13 19 23 28 31)</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{works with|rakudo|2015-09-13}}
{{works with|rakudo|2015-09-13}}
<lang perl6>sub happy (Int $n is copy --> Bool) {
<syntaxhighlight lang="raku" line>sub happy (Int $n is copy --> Bool) {
loop {
loop {
state %seen;
state %seen;
Line 5,692: Line 5,692:
}
}


say join ' ', grep(&happy, 1 .. *)[^8];</lang>
say join ' ', grep(&happy, 1 .. *)[^8];</syntaxhighlight>
{{out}}
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
<pre>1 7 10 13 19 23 28 31</pre>
Here's another approach that uses a different set of tricks including lazy lists, gather/take, repeat-until, and the cross metaoperator X.
Here's another approach that uses a different set of tricks including lazy lists, gather/take, repeat-until, and the cross metaoperator X.
<lang perl6>my @happy = lazy gather for 1..* -> $number {
<syntaxhighlight lang="raku" line>my @happy = lazy gather for 1..* -> $number {
my %stopper = 1 => 1;
my %stopper = 1 => 1;
my $n = $number;
my $n = $number;
Line 5,705: Line 5,705:
}
}


say ~@happy[^8];</lang>
say ~@happy[^8];</syntaxhighlight>
Output is the same as above.
Output is the same as above.


Here is a version using a subset and an anonymous recursion (we cheat a little bit by using the knowledge that 7 is the second happy number):
Here is a version using a subset and an anonymous recursion (we cheat a little bit by using the knowledge that 7 is the second happy number):
<lang perl6>subset Happy of Int where sub ($n) {
<syntaxhighlight lang="raku" line>subset Happy of Int where sub ($n) {
$n == 1 ?? True !!
$n == 1 ?? True !!
$n < 7 ?? False !!
$n < 7 ?? False !!
Line 5,715: Line 5,715:
}
}
say (grep Happy, 1 .. *)[^8];</lang>
say (grep Happy, 1 .. *)[^8];</syntaxhighlight>
Again, output is the same as above. It is not clear whether this version returns in finite time for any integer, though.
Again, output is the same as above. It is not clear whether this version returns in finite time for any integer, though.


Line 5,721: Line 5,721:


=={{header|Relation}}==
=={{header|Relation}}==
<syntaxhighlight lang="relation">
<lang Relation>
function happy(x)
function happy(x)
set y = x
set y = x
Line 5,753: Line 5,753:
set i = i + 1
set i = i + 1
end while
end while
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 5,768: Line 5,768:
=={{header|REXX}}==
=={{header|REXX}}==
===unoptimized===
===unoptimized===
<lang REXX>/*REXX program computes and displays a specified amount of happy numbers. */
<syntaxhighlight lang="rexx">/*REXX program computes and displays a specified amount of happy numbers. */
parse arg limit . /*obtain optional argument from the CL.*/
parse arg limit . /*obtain optional argument from the CL.*/
if limit=='' | limit=="," then limit=8 /*Not specified? Then use the default.*/
if limit=='' | limit=="," then limit=8 /*Not specified? Then use the default.*/
Line 5,786: Line 5,786:
haps=haps+1 /*bump the count of happy numbers. */
haps=haps+1 /*bump the count of happy numbers. */
end /*n*/
end /*n*/
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 8 </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 8 </tt>}}
<pre>
<pre>
Line 5,804: Line 5,804:
<br><br>This REXX version also accepts a &nbsp; ''range'' &nbsp; of happy numbers to be shown, &nbsp; that is,
<br><br>This REXX version also accepts a &nbsp; ''range'' &nbsp; of happy numbers to be shown, &nbsp; that is,
<br>it can show the 2000<sup>th</sup> through the 2032<sup>nd</sup> (inclusive) happy numbers &nbsp; (as shown below).
<br>it can show the 2000<sup>th</sup> through the 2032<sup>nd</sup> (inclusive) happy numbers &nbsp; (as shown below).
<lang rexx>/*REXX program computes and displays a specified range of happy numbers. */
<syntaxhighlight lang="rexx">/*REXX program computes and displays a specified range of happy numbers. */
parse arg L H . /*obtain optional arguments from the CL*/
parse arg L H . /*obtain optional arguments from the CL*/
if L=='' | L=="," then L=8 /*Not specified? Then use the default.*/
if L=='' | L=="," then L=8 /*Not specified? Then use the default.*/
Line 5,830: Line 5,830:
say right(n, 30) /*display right justified happy number.*/
say right(n, 30) /*display right justified happy number.*/
end /*n*/
end /*n*/
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 2000 &nbsp; 2032 </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 2000 &nbsp; 2032 </tt>}}
<pre>
<pre>
Line 5,870: Line 5,870:
===optimized, horizontal list===
===optimized, horizontal list===
This REXX version is identical to the optimized version, &nbsp; but displays the numbers in a horizontal list.
This REXX version is identical to the optimized version, &nbsp; but displays the numbers in a horizontal list.
<lang rexx>/*REXX program computes and displays a specified range of happy numbers. */
<syntaxhighlight lang="rexx">/*REXX program computes and displays a specified range of happy numbers. */
sw=linesize() - 1 /*obtain the screen width (less one). */
sw=linesize() - 1 /*obtain the screen width (less one). */
parse arg limit . /*obtain optional argument from the CL.*/
parse arg limit . /*obtain optional argument from the CL.*/
Line 5,903: Line 5,903:
end /*n*/
end /*n*/
if $\='' then say strip($) /*display any residual happy numbers. */
if $\='' then say strip($) /*display any residual happy numbers. */
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>
This REXX program makes use of &nbsp; '''linesize''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
This REXX program makes use of &nbsp; '''linesize''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).


Line 5,957: Line 5,957:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>n = 1
<syntaxhighlight lang="ring">n = 1
found = 0
found = 0
Line 5,981: Line 5,981:
End
End
Return True
Return True
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 5,997: Line 5,997:
{{works with|Ruby|2.1}}
{{works with|Ruby|2.1}}


<lang ruby>require 'set' # Set: Fast array lookup / Simple existence hash
<syntaxhighlight lang="ruby">require 'set' # Set: Fast array lookup / Simple existence hash


@seen_numbers = Set.new
@seen_numbers = Set.new
Line 6,015: Line 6,015:
false # Return false
false # Return false
end
end
end</lang>
end</syntaxhighlight>


Helper method to produce output:
Helper method to produce output:
<lang ruby>def print_happy
<syntaxhighlight lang="ruby">def print_happy
happy_numbers = []
happy_numbers = []


Line 6,029: Line 6,029:
end
end


print_happy</lang>
print_happy</syntaxhighlight>


{{out}}
{{out}}
<lang ruby>[1, 7, 10, 13, 19, 23, 28, 31]</lang>
<syntaxhighlight lang="ruby">[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>


===Alternative version===
===Alternative version===
<lang ruby>@memo = [0,1]
<syntaxhighlight lang="ruby">@memo = [0,1]
def happy(n)
def happy(n)
sum = n.to_s.chars.map{|c| c.to_i**2}.inject(:+)
sum = n.to_s.chars.map{|c| c.to_i**2}.inject(:+)
Line 6,052: Line 6,052:
for i in 99999999999900..99999999999999
for i in 99999999999900..99999999999999
puts i if happy(i)==1
puts i if happy(i)==1
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 6,081: Line 6,081:
===Simpler Alternative===
===Simpler Alternative===
{{trans|Python}}
{{trans|Python}}
<lang ruby>def happy?(n)
<syntaxhighlight lang="ruby">def happy?(n)
past = []
past = []
until n == 1
until n == 1
Line 6,094: Line 6,094:
until count == 8; puts i or count += 1 if happy?(i += 1) end
until count == 8; puts i or count += 1 if happy?(i += 1) end
puts
puts
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }</lang>
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,120: Line 6,120:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>for i = 1 to 100
<syntaxhighlight lang="runbasic">for i = 1 to 100
if happy(i) = 1 then
if happy(i) = 1 then
cnt = cnt + 1
cnt = cnt + 1
Line 6,138: Line 6,138:
num = happy
num = happy
wend
wend
end function</lang>
end function</syntaxhighlight>
<pre>1. 1 is a happy number
<pre>1. 1 is a happy number
2. 7 is a happy number
2. 7 is a happy number
Line 6,151: Line 6,151:
=={{header|Rust}}==
=={{header|Rust}}==
In Rust, using a tortoise/hare cycle detection algorithm (generic for integer types)
In Rust, using a tortoise/hare cycle detection algorithm (generic for integer types)
<lang rust>#![feature(core)]
<syntaxhighlight lang="rust">#![feature(core)]


fn sumsqd(mut n: i32) -> i32 {
fn sumsqd(mut n: i32) -> i32 {
Line 6,186: Line 6,186:


println!("{:?}", happy)
println!("{:?}", happy)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,193: Line 6,193:


=={{header|Salmon}}==
=={{header|Salmon}}==
<lang Salmon>variable happy_count := 0;
<syntaxhighlight lang="salmon">variable happy_count := 0;
outer:
outer:
iterate(x; [1...+oo])
iterate(x; [1...+oo])
Line 6,221: Line 6,221:
now := new;
now := new;
};
};
};</lang>
};</syntaxhighlight>
This Salmon program produces the following output:
This Salmon program produces the following output:
<pre>1 is happy.
<pre>1 is happy.
Line 6,233: Line 6,233:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>scala> def isHappy(n: Int) = {
<syntaxhighlight lang="scala">scala> def isHappy(n: Int) = {
| new Iterator[Int] {
| new Iterator[Int] {
| val seen = scala.collection.mutable.Set[Int]()
| val seen = scala.collection.mutable.Set[Int]()
Line 6,257: Line 6,257:
28
28
31
31
</syntaxhighlight>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>(define (number->list num)
<syntaxhighlight lang="scheme">(define (number->list num)
(do ((num num (quotient num 10))
(do ((num num (quotient num 10))
(lst '() (cons (remainder num 10) lst)))
(lst '() (cons (remainder num 10) lst)))
Line 6,276: Line 6,276:
(cond ((= more 0) (newline))
(cond ((= more 0) (newline))
((happy? n) (display " ") (display n) (loop (+ n 1) (- more 1)))
((happy? n) (display " ") (display n) (loop (+ n 1) (- more 1)))
(else (loop (+ n 1) more))))</lang>
(else (loop (+ n 1) more))))</syntaxhighlight>
The output is:
The output is:
<pre>happy numbers: 1 7 10 13 19 23 28 31</pre>
<pre>happy numbers: 1 7 10 13 19 23 28 31</pre>
Line 6,288: Line 6,288:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const type: cacheType is hash [integer] boolean;
const type: cacheType is hash [integer] boolean;
Line 6,329: Line 6,329:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


Output:
Output:
Line 6,347: Line 6,347:


=={{header|SequenceL}}==
=={{header|SequenceL}}==
<lang sequencel>import <Utilities/Math.sl>;
<syntaxhighlight lang="sequencel">import <Utilities/Math.sl>;
import <Utilities/Conversion.sl>;
import <Utilities/Conversion.sl>;


Line 6,373: Line 6,373:
true when n = 1
true when n = 1
else
else
isHappyHelper(newN, cache ++ [n]);</lang>
isHappyHelper(newN, cache ++ [n]);</syntaxhighlight>


{{out}}
{{out}}
Line 6,382: Line 6,382:


=={{header|SETL}}==
=={{header|SETL}}==
<lang SETL>proc is_happy(n);
<syntaxhighlight lang="setl">proc is_happy(n);
s := [n];
s := [n];
while n > 1 loop
while n > 1 loop
Line 6,391: Line 6,391:
end while;
end while;
return true;
return true;
end proc;</lang>
end proc;</syntaxhighlight>
<lang SETL>happy := [];
<syntaxhighlight lang="setl">happy := [];
n := 1;
n := 1;
until #happy = 8 loop
until #happy = 8 loop
Line 6,399: Line 6,399:
end loop;
end loop;


print(happy);</lang>
print(happy);</syntaxhighlight>
Output:
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
<pre>[1 7 10 13 19 23 28 31]</pre>
Alternative version:
Alternative version:
<lang SETL>print([n : n in [1..100] | is_happy(n)](1..8));</lang>
<syntaxhighlight lang="setl">print([n : n in [1..100] | is_happy(n)](1..8));</syntaxhighlight>
Output:
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
<pre>[1 7 10 13 19 23 28 31]</pre>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func happy(n) is cached {
<syntaxhighlight lang="ruby">func happy(n) is cached {
static seen = Hash()
static seen = Hash()


Line 6,418: Line 6,418:
}
}


say happy.first(8)</lang>
say happy.first(8)</syntaxhighlight>


{{out}}
{{out}}
Line 6,429: Line 6,429:
{{trans|Python}}
{{trans|Python}}
In addition to the "Python's cache mechanism", the use of a Bag assures that found e.g. the happy 190, we already have in cache also the happy 910 and 109, and so on.
In addition to the "Python's cache mechanism", the use of a Bag assures that found e.g. the happy 190, we already have in cache also the happy 910 and 109, and so on.
<lang smalltalk>Object subclass: HappyNumber [
<syntaxhighlight lang="smalltalk">Object subclass: HappyNumber [
|cache negativeCache|
|cache negativeCache|
HappyNumber class >> new [ |me|
HappyNumber class >> new [ |me|
Line 6,491: Line 6,491:
]
]
]
]
].</lang>
].</syntaxhighlight>
<lang smalltalk>|happy|
<syntaxhighlight lang="smalltalk">|happy|
happy := HappyNumber new.
happy := HappyNumber new.


Line 6,498: Line 6,498:
(happy isHappy: i)
(happy isHappy: i)
ifTrue: [ i displayNl ]
ifTrue: [ i displayNl ]
].</lang>
].</syntaxhighlight>
Output:
Output:
1
1
Line 6,511: Line 6,511:
an alternative version is:
an alternative version is:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang smalltalk>|next isHappy happyNumbers|
<syntaxhighlight lang="smalltalk">|next isHappy happyNumbers|


next :=
next :=
Line 6,535: Line 6,535:
try := try + 1
try := try + 1
].
].
happyNumbers printCR</lang>
happyNumbers printCR</syntaxhighlight>
Output:
Output:
OrderedCollection(1 7 10 13 19 23 28 31)
OrderedCollection(1 7 10 13 19 23 28 31)


=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>func isHappyNumber(var n:Int) -> Bool {
<syntaxhighlight lang="swift">func isHappyNumber(var n:Int) -> Bool {
var cycle = [Int]()
var cycle = [Int]()
Line 6,564: Line 6,564:
}
}
count++
count++
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,578: Line 6,578:
=={{header|Tcl}}==
=={{header|Tcl}}==
using code from [[Sum of squares#Tcl]]
using code from [[Sum of squares#Tcl]]
<lang tcl>proc is_happy n {
<syntaxhighlight lang="tcl">proc is_happy n {
set seen [list]
set seen [list]
while {$n > 1 && [lsearch -exact $seen $n] == -1} {
while {$n > 1 && [lsearch -exact $seen $n] == -1} {
Line 6,593: Line 6,593:
incr n
incr n
}
}
puts "the first 8 happy numbers are: [list $happy]"</lang>
puts "the first 8 happy numbers are: [list $happy]"</syntaxhighlight>
<pre>the first 8 happy numbers are: {1 7 10 13 19 23 28 31}</pre>
<pre>the first 8 happy numbers are: {1 7 10 13 19 23 28 31}</pre>


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>$$ MODE TUSCRIPT
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
SECTION check
SECTION check
IF (n!=1) THEN
IF (n!=1) THEN
Line 6,632: Line 6,632:
DO check
DO check
ENDLOOP
ENDLOOP
ENDLOOP</lang>
ENDLOOP</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 6,646: Line 6,646:


=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
<lang>
<syntaxhighlight lang="text">
' ************************
' ************************
' MAIN
' MAIN
Line 6,709: Line 6,709:
' END SUBS & FUNCTIONS
' END SUBS & FUNCTIONS
' ************************
' ************************
</syntaxhighlight>
</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Bourne Again SHell}}
<lang bash>#!/bin/bash
<syntaxhighlight lang="bash">#!/bin/bash
function sum_of_square_digits
function sum_of_square_digits
{
{
Line 6,752: Line 6,752:
}
}


first_n_happy 8</lang>
first_n_happy 8</syntaxhighlight>
Output:<pre>1
Output:<pre>1
7
7
Line 6,766: Line 6,766:
and first(p) defines a function mapping a number n to the first n
and first(p) defines a function mapping a number n to the first n
positive naturals having property p.
positive naturals having property p.
<lang Ursala>#import std
<syntaxhighlight lang="ursala">#import std
#import nat
#import nat


Line 6,775: Line 6,775:
#cast %nL
#cast %nL


main = (first happy) 8</lang>
main = (first happy) 8</syntaxhighlight>
output:
output:
<pre><1,7,10,13,19,23,28,31></pre>
<pre><1,7,10,13,19,23,28,31></pre>
Line 6,781: Line 6,781:
=={{header|Vala}}==
=={{header|Vala}}==
{{libheader|Gee}}
{{libheader|Gee}}
<lang vala>using Gee;
<syntaxhighlight lang="vala">using Gee;


/* function to sum the square of the digits */
/* function to sum the square of the digits */
Line 6,826: Line 6,826:
stdout.printf("%d ", num);
stdout.printf("%d ", num);
stdout.printf("\n");
stdout.printf("\n");
} // end main</lang>
} // end main</syntaxhighlight>
The output is:
The output is:
<pre>
<pre>
Line 6,834: Line 6,834:
=={{header|VBA}}==
=={{header|VBA}}==


<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
Option Explicit


Line 6,864: Line 6,864:
Is_Happy_Number = True
Is_Happy_Number = True
End Function
End Function
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>Is Happy : 1
<pre>Is Happy : 1
Line 6,876: Line 6,876:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
count = 0
count = 0
firsteigth=""
firsteigth=""
Line 6,907: Line 6,907:
Loop
Loop
End Function
End Function
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 6,914: Line 6,914:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
This version uses Linq to carry out the calculations.
This version uses Linq to carry out the calculations.
<lang vbnet>Module HappyNumbers
<syntaxhighlight lang="vbnet">Module HappyNumbers
Sub Main()
Sub Main()
Dim n As Integer = 1
Dim n As Integer = 1
Line 6,942: Line 6,942:
Return True
Return True
End Function
End Function
End Module</lang>
End Module</syntaxhighlight>
The output is:
The output is:
<pre>1: 1
<pre>1: 1
Line 6,955: Line 6,955:
{{trans|C#}}
{{trans|C#}}
Curiously, this runs in about two thirds of the time of the cacheless C# version on Tio.run.
Curiously, this runs in about two thirds of the time of the cacheless C# version on Tio.run.
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Dim sq As Integer() = {1, 4, 9, 16, 25, 36, 49, 64, 81}
Dim sq As Integer() = {1, 4, 9, 16, 25, 36, 49, 64, 81}
Line 6,994: Line 6,994:
Console.WriteLine(vbLf & "Computation time {0} seconds.", (DateTime.Now - st).TotalSeconds)
Console.WriteLine(vbLf & "Computation time {0} seconds.", (DateTime.Now - st).TotalSeconds)
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>---Happy Numbers---
<pre>---Happy Numbers---
Line 7,009: Line 7,009:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>fn happy(h int) bool {
<syntaxhighlight lang="vlang">fn happy(h int) bool {
mut m := map[int]bool{}
mut m := map[int]bool{}
mut n := h
mut n := h
Line 7,034: Line 7,034:
}
}
println('')
println('')
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 7,042: Line 7,042:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>var happy = Fn.new { |n|
<syntaxhighlight lang="ecmascript">var happy = Fn.new { |n|
var m = {}
var m = {}
while (n > 1) {
while (n > 1) {
Line 7,067: Line 7,067:
n = n + 1
n = n + 1
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}
Line 7,081: Line 7,081:
numbers.
numbers.


<lang XPL0>int List(810); \list of numbers in a cycle
<syntaxhighlight lang="xpl0">int List(810); \list of numbers in a cycle
int Inx; \index for List
int Inx; \index for List
include c:\cxpl\codes;
include c:\cxpl\codes;
Line 7,125: Line 7,125:
N0:= N0+1; \next starting number
N0:= N0+1; \next starting number
until C=8; \done when 8 happy numbers have been found
until C=8; \done when 8 happy numbers have been found
]</lang>
]</syntaxhighlight>


Output:
Output:
Line 7,140: Line 7,140:


=={{header|Zig}}==
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
const std = @import("std");
const stdout = std.io.getStdOut().outStream();
const stdout = std.io.getStdOut().outStream();
Line 7,177: Line 7,177:
return s;
return s;
}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 7,185: Line 7,185:
Here is a function that generates a continuous stream of happy numbers. Given that there are lots of happy numbers, caching them doesn't seem like a good idea memory wise. Instead, a num of squared digits == 4 is used as a proxy for a cycle (see the Wikipedia article, there are several number that will work).
Here is a function that generates a continuous stream of happy numbers. Given that there are lots of happy numbers, caching them doesn't seem like a good idea memory wise. Instead, a num of squared digits == 4 is used as a proxy for a cycle (see the Wikipedia article, there are several number that will work).
{{trans|Icon and Unicon}}
{{trans|Icon and Unicon}}
<lang zkl>fcn happyNumbers{ // continously spew happy numbers
<syntaxhighlight lang="zkl">fcn happyNumbers{ // continously spew happy numbers
foreach N in ([1..]){
foreach N in ([1..]){
n:=N; while(1){
n:=N; while(1){
Line 7,193: Line 7,193:
}
}
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>h:=Utils.Generator(happyNumbers);
<syntaxhighlight lang="zkl">h:=Utils.Generator(happyNumbers);
h.walk(8).println();</lang>
h.walk(8).println();</syntaxhighlight>
{{out}}
{{out}}
<pre>L(1,7,10,13,19,23,28,31)</pre>
<pre>L(1,7,10,13,19,23,28,31)</pre>
Get the one million-th happy number. Nobody would call this quick.
Get the one million-th happy number. Nobody would call this quick.
<lang zkl>Utils.Generator(happyNumbers).drop(0d1_000_000-1).next().println();</lang>
<syntaxhighlight lang="zkl">Utils.Generator(happyNumbers).drop(0d1_000_000-1).next().println();</syntaxhighlight>
{{out}}<pre>7105849</pre>
{{out}}<pre>7105849</pre>


=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
{{trans|Run_BASIC}}
{{trans|Run_BASIC}}
<lang zxbasic>10 FOR i=1 TO 100
<syntaxhighlight lang="zxbasic">10 FOR i=1 TO 100
20 GO SUB 1000
20 GO SUB 1000
30 IF isHappy=1 THEN PRINT i;" is a happy number"
30 IF isHappy=1 THEN PRINT i;" is a happy number"
Line 7,219: Line 7,219:
1080 NEXT j
1080 NEXT j
1090 LET num=isHappy
1090 LET num=isHappy
1100 GO TO 1020</lang>
1100 GO TO 1020</syntaxhighlight>

Revision as of 14:51, 27 August 2022

Task
Happy numbers
You are encouraged to solve this task according to the task description, using any language you may know.

From Wikipedia, the free encyclopedia:

A happy number is defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not include   1.  


Those numbers for which this process end in   1   are       happy   numbers,  
while   those numbers   that   do   not   end in   1   are   unhappy   numbers.


Task

Find and print the first   8   happy numbers.

Display an example of your output here on this page.


See also



11l

Translation of: Python
F happy(=n)
   Set[Int] past
   L n != 1
      n = sum(String(n).map(с -> Int(с)^2))
      I n C past
         R 0B
      past.add(n)
   R 1B

print((0.<500).filter(x -> happy(x))[0.<8])
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

8080 Assembly

This is not just a demonstration of 8080 assembly, but also of why it pays to look closely at the problem domain. The following program only does 8-bit unsigned integer math, which not only fits the 8080's instruction set very well, it also means the cycle detection can be done using only an array of 256 flags, and all other state fits in the registers. This makes the program a good deal simpler than it would've been otherwise.

In general, 8-bit math is not good enough for numerical problems, but in this particular case, the problem only asks for the first eight happy numbers, none of which (nor any of the unhappy numbers in between) have a cycle that ever goes above 145, so eight bits is good enough. In fact, for any input under 256, the cycle never goes above 163; this program could be trivially changed to print up to 39 happy numbers.

flags:	equ	2	; 256-byte page in which to keep track of cycles
puts:	equ	9	; CP/M print string
bdos:	equ	5 	; CP/M entry point
	org	100h
	lxi	d,0108h	; D=current number to test, E=amount of numbers
	;;;	Is D happy?
number:	mvi	a,1	; We haven't seen any numbers yet, set flags to 1
	lxi	h,256*flags
init:	mov	m,a
	inr	l
	jnz	init
	mov	a,d	; Get digits
step:	call	digits
	mov	l,a	; L = D1 * D1
	mov	h,a
	xra	a
sqr1:	add	h
	dcr	l
	jnz	sqr1
	mov	l,a
	mov	h,b	; L += D10 * D10
	xra	a
sqr10:	add	h
	dcr 	b
	jnz	sqr10
	add	l
	mov	l,a
	mov	h,c	; L += D100 * D100
	xra	a
sqr100:	add	h
	dcr	c
	jnz	sqr100
	add	l
	mov	l,a
	mvi	h,flags	; Look up corresponding flag
	dcr	m 	; Will give 0 the first time and not-0 afterwards
	mov	a,l	; If we haven't seen the number before, another step
	jz 	step 
	dcr	l	; If we _had_ seen it, then is it 1?
	jz	happy	; If so, it is happy
next:	inr	d	; Afterwards, try next number
	jmp	number
happy:	mov	a,d	; D is happy - get its digits (for output)
	lxi	h,string+3
	call	digits	; Write digits into string for output
	call	sdgt	; Ones digit,
	mov	a,b	; Tens digit,
	call	sdgt
	mov	a,c	; Hundreds digit
	call	sdgt
	push	d	; Keep counters on stack
	mvi	c,puts	; Print string using CP/M call
	xchg	
	call	bdos
	pop	d	; Restore counters
	dcr	e	; One fewer happy number left
	jnz	next	; If we need more, do the next one
	ret
	;;;	Store A as ASCII digit in [HL] and go to previous digit
sdgt:	adi	'0'
	dcx	h
	mov	m,a
	ret
	;;;	Get digits of 8-bit number in A.
	;;;	Input: A = number
	;;;	Output: C=100s digit, B=10s digit, A=1s digit
digits:	lxi	b,-1	; Set B and C to -1 (correct for extra loop cycle)
d100:	inr	c	; Calculate hundreds digit
	sui	100	; By trial subtraction of 100
	jnc	d100	; Until underflow occurs
	adi	100	; Loop runs one cycle too many, so add 100 back
d10:	inr	b	; Calculate 10s digit in the same way
	sui	10
	jnc	d10
	adi	10
	ret		; 1s digit is left in A afterwards
string:	db	'000',13,10,'$'
Output:
001
007
010
013
019
023
028
031


8th

: until!  "not while!" eval i;

with: w
with: n

: sumsqd  \ n -- n
    0 swap repeat
        0; 10 /mod -rot sqr + swap
    again ;

: cycle \ n xt -- n
    >r
    dup r@ exec  \ -- tortoise, hare
    repeat
        swap r@ exec
        swap r@ exec r@ exec
    2dup = until!
    rdrop drop ;

: happy?  ' sumsqd cycle 1 = ;

: .happy \ n --
    1 repeat
        dup happy? if  dup . space  swap 1- swap  then 1+
    over 0 > while!
    2drop cr ;

;with
;with
Output:
ok> 8 .happy
1 7 10 13 19 23 28 31

ACL2

(include-book "arithmetic-3/top" :dir :system)

(defun sum-of-digit-squares (n)
   (if (zp n)
       0
       (+ (expt (mod n 10) 2)
          (sum-of-digit-squares (floor n 10)))))

(defun is-happy-r (n seen)
   (let ((next (sum-of-digit-squares n)))
        (cond ((= next 1) t)
              ((member next seen) nil)
              (t (is-happy-r next (cons next seen))))))

(defun is-happy (n)
   (is-happy-r n nil))

(defun first-happy-nums-r (n i)
   (cond ((zp n) nil)
         ((is-happy i)
          (cons i (first-happy-nums-r (1- n) (1+ i))))
         (t (first-happy-nums-r n (1+ i)))))

(defun first-happy-nums (n)
   (first-happy-nums-r n 1))

Output:

(1 7 10 13 19 23 28 31)

Action!

BYTE FUNC SumOfSquares(BYTE x)
  BYTE sum,d

  sum=0
  WHILE x#0
  DO
    d=x MOD 10
    d==*d
    sum==+d
    x==/10
  OD
RETURN (sum)

BYTE FUNC Contains(BYTE ARRAY a BYTE count,x)
  BYTE i

  FOR i=0 TO count-1
  DO
    IF a(i)=x THEN RETURN (1) FI
  OD
RETURN (0)

BYTE FUNC IsHappyNumber(BYTE x)
  BYTE ARRAY cache(100)
  BYTE count

  count=0
  WHILE x#1
  DO
    cache(count)=x
    count==+1
    x=SumOfSquares(x)
    IF Contains(cache,count,x) THEN
      RETURN (0)
    FI
  OD
RETURN (1)

PROC Main()
  BYTE x,count

  x=1 count=0
  WHILE count<8
  DO
    IF IsHappyNumber(x) THEN
      count==+1
      PrintF("%I: %I%E",count,x)
    FI
    x==+1
  OD
RETURN
Output:

Screenshot from Atari 8-bit computer

1: 1
2: 7
3: 10
4: 13
5: 19
6: 23
7: 28
8: 31

ActionScript

function sumOfSquares(n:uint)
{
	var sum:uint = 0;
	while(n != 0)
	{
		sum += (n%10)*(n%10);
		n /= 10;
	}
	return sum;
}
function isInArray(n:uint, array:Array)
{
	for(var k = 0; k < array.length; k++)
		if(n == array[k]) return true;
	return false;
}
function isHappy(n)
{
	var sequence:Array = new Array();
	while(n != 1)
	{
		sequence.push(n);
		n = sumOfSquares(n);
		if(isInArray(n,sequence))return false;
	}
	return true;
}
function printHappy()
{
	var numbersLeft:uint = 8;
	var numberToTest:uint = 1;
	while(numbersLeft != 0)
	{
		if(isHappy(numberToTest))
		{
			trace(numberToTest);
			numbersLeft--;
		}
		numberToTest++;
	}
}
printHappy();

Sample output:

1
7
10
13
19
23
28
31

Ada

with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Ordered_Sets;

procedure Test_Happy_Digits is
   function Is_Happy (N : Positive) return Boolean is
      package Sets_Of_Positive is new Ada.Containers.Ordered_Sets (Positive);
      use Sets_Of_Positive;
      function Next (N : Positive) return Natural is
         Sum   : Natural := 0;
         Accum : Natural := N;
      begin
         while Accum > 0 loop
            Sum   := Sum + (Accum mod 10) ** 2;
            Accum := Accum / 10;
         end loop;
         return Sum;
      end Next;
      Current : Positive := N;
      Visited : Set;
   begin
      loop
         if Current = 1 then
            return True;
         elsif Visited.Contains (Current) then
            return False;
         else
            Visited.Insert (Current);
            Current := Next (Current);
         end if;
      end loop;
   end Is_Happy;
   Found : Natural := 0;
begin
   for N in Positive'Range loop
      if Is_Happy (N) then
         Put (Integer'Image (N));
         Found := Found + 1;
         exit when Found = 8;
      end if;
   end loop;
end Test_Happy_Digits;

Sample output:

 1 7 10 13 19 23 28 31

ALGOL 68

Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386
INT base10 = 10, num happy = 8;

PROC next = (INT in n)INT: (
  INT n := in n;
  INT out := 0;
  WHILE n NE 0 DO
    out +:= ( n MOD base10 ) ** 2;
    n := n OVER base10
  OD;
  out
);

PROC is happy = (INT in n)BOOL: (
  INT n := in n;
  FOR i WHILE n NE 1 AND n NE 4 DO n := next(n) OD;
  n=1
);

INT count := 0;
FOR i WHILE count NE num happy DO
  IF is happy(i) THEN
    count +:= 1;
    print((i, new line))
  FI
OD

Output:

         +1
         +7
        +10
        +13
        +19
        +23
        +28
        +31

ALGOL-M

begin
integer function mod(a,b);
integer a,b;
mod := a-(a/b)*b;

integer function sumdgtsq(n);
integer n;
sumdgtsq :=
    if n = 0 then 0
    else mod(n,10)*mod(n,10) + sumdgtsq(n/10);

integer function happy(n);
integer n;
begin
    integer i;
    integer array seen[0:200];
    for i := 0 step 1 until 200 do seen[i] := 0;
    
    while seen[n] = 0 do
    begin
        seen[n] := 1;
        n := sumdgtsq(n);
    end;
    happy := if n = 1 then 1 else 0;
end;

integer i, n;
i := n := 0;
while n < 8 do
begin
    if happy(i) = 1 then
    begin
        write(i);
        n := n + 1;
    end;
    i := i + 1;
end;
end
Output:
     1
     7
    10
    13
    19
    23
    28
    31

ALGOL W

begin
    % find some happy numbers                                      %
    % returns true if n is happy, false otherwise; n must be >= 0  %
    logical procedure isHappy( integer value n ) ;
        if n < 2 then true
        else begin
            % seen is used to hold the values of the cycle of the  %
            % digit square sums, as noted in the Batch File        %
            % version, we do not need a large array. The digit     %
            % square sum of 9 999 999 999 is 810...                %
            integer array seen( 0 :: 32 );
            integer number, trys;
            number             := n;
            trys               := -1;
            while begin
                logical terminated;
                integer tPos;
                terminated     := false;
                tPos           := 0;
                while not terminated and tPos <= trys do begin
                    terminated := seen( tPos ) = number;
                    tPos       := tPos + 1
                end while_not_terminated_and_tPos_lt_trys ;
                number > 1 and not terminated
            end do begin
                integer sum;
                trys           := trys + 1;
                seen( trys )   := number;
                sum            := 0;
                while number > 0 do begin
                    integer digit;
                    digit      := number rem 10;
                    number     := number div 10;
                    sum        := sum + ( digit * digit )
                end while_number_gt_0 ;
                number         := sum
            end while_number_gt_1_and_not_terminated ;
            number = 1
        end isHappy ;
    % print the first 8 happy numbers                               %
    begin
        integer       happyCount, n;
        happyCount := 0;
        n          := 1;
        write( "first 8 happy numbers: " );
        while happyCount < 8 do begin
            if isHappy( n ) then begin
                writeon( i_w := 1, " ", n );
                happyCount := happyCount + 1
            end if_isHappy_n ;
            n := n + 1
        end while_happyCount_lt_8
    end
end.
Output:
first 8 happy numbers:  1   7   10   13   19   23   28   31

APL

Tradfn

      HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
[1]   ⍝0: Happy number
[2]   ⍝1: http://rosettacode.org/wiki/Happy_numbers
[3]    ⎕IO1                              ⍝ Index origin
[4]    ∆roof ∆first2arg,10              
[5]
[6]    bin{
[7]                                    ⍝ Default left arg
[8]        =1:1                          ⍝ Always happy!
[9]
[10]       numbers¨1⊂⍕                 ⍝ Split numbers into parts
[11]       next+/{*2}¨numbers           ⍝ Sum and square of numbers
[12]
[13]       next⍺:0                       ⍝ Return 0, if already exists
[14]       (,next) next                 ⍝ Check next number (recursive)
[15]
[16]   }¨iroof∆roof                     ⍝ Does all numbers upto ∆root smiles?
[17]
[18]   ~0¨∆firstbin/iroof             ⍝ Show ∆first numbers, but not 0
     
      HappyNumbers 100 8
 1  7  10  13  19  23  28  31 

Dfn

 HappyNumbers{          ⍝ return the first ⍵ Happy Numbers
                      ⍝ initial list
     =+/⍺:            ⍝ 1's mark happy numbers
     sq×               ⍝ square function (times selfie)
     isHappy{           ⍝ is ⍵ a happy number?
                      ⍝ previous sums
         =1:1           ⍝ if we get to 1, it's happy
         n+/sq¨     ⍝ sum of the square of the digits
         n⍺:0           ⍝ if we hit this sum before, it's not happy
         (,n) n}       ⍝ recurse until it's happy or not
     (,isHappy 1+≢)  ⍝ recurse until we have ⍵ happy numbers
 }
      HappyNumbers 8
1 7 10 13 19 23 28 31

AppleScript

Iteration

on run
    set howManyHappyNumbers to 8
    set happyNumberList to {}
    set globalCounter to 1
    
    repeat howManyHappyNumbers times
        repeat while not isHappy(globalCounter)
            set globalCounter to globalCounter + 1
        end repeat
        set end of happyNumberList to globalCounter
        set globalCounter to globalCounter + 1
    end repeat
    log happyNumberList
end run

on isHappy(numberToCheck)
    set localCycle to {}
    repeat while (numberToCheck  1)
        if localCycle contains numberToCheck then
            exit repeat
        end if
        set end of localCycle to numberToCheck
        set tempNumber to 0
        repeat while (numberToCheck > 0)
            set digitOfNumber to numberToCheck mod 10
            set tempNumber to tempNumber + (digitOfNumber ^ 2)
            set numberToCheck to (numberToCheck - digitOfNumber) / 10
        end repeat
        set numberToCheck to tempNumber
    end repeat
    return (numberToCheck = 1)
end isHappy
Result: (*1, 7, 10, 13, 19, 23, 28, 31*)

Functional composition

Translation of: JavaScript
Translation of: Haskell
---------------------- HAPPY NUMBERS -----------------------

-- isHappy :: Int -> Bool
on isHappy(n)
    
    -- endsInOne :: [Int] -> Int -> Bool
    script endsInOne
        
        -- sumOfSquaredDigits :: Int -> Int
        script sumOfSquaredDigits
            
            -- digitSquared :: Int -> Int -> Int
            script digitSquared
                on |λ|(a, x)
                    (a + (x as integer) ^ 2) as integer
                end |λ|
            end script
            
            on |λ|(n)
                foldl(digitSquared, 0, splitOn("", n as string))
            end |λ|
        end script
        
        -- [Int] -> Int -> Bool
        on |λ|(s, n)
            if n = 1 then
                true
            else
                if s contains n then
                    false
                else
                    |λ|(s & n, |λ|(n) of sumOfSquaredDigits)
                end if
            end if
        end |λ|
    end script
    
    endsInOne's |λ|({}, n)
end isHappy

--------------------------- TEST ---------------------------
on run
    
    -- seriesLength :: {n:Int, xs:[Int]} -> Bool
    script seriesLength
        property target : 8
        
        on |λ|(rec)
            length of xs of rec = target of seriesLength
        end |λ|
    end script
    
    -- succTest :: {n:Int, xs:[Int]} -> {n:Int, xs:[Int]}
    script succTest
        on |λ|(rec)
            tell rec to set {xs, n} to {its xs, its n}
            
            script testResult
                on |λ|(x)
                    if isHappy(x) then
                        xs & x
                    else
                        xs
                    end if
                end |λ|
            end script
            
            {n:n + 1, xs:testResult's |λ|(n)}
        end |λ|
    end script
    
    xs of |until|(seriesLength, succTest, {n:1, xs:{}})
    
    --> {1, 7, 10, 13, 19, 23, 28, 31}
end run


-------------------- GENERIC FUNCTIONS ---------------------

-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
        end repeat
        return v
    end tell
end foldl


-- Lift 2nd class handler function into 1st class script wrapper 
-- mReturn :: Handler -> Script
on mReturn(f)
    if class of f is script then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn


-- splitOn :: String -> String -> [String]
on splitOn(pat, src)
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, pat}
    set xs to text items of src
    set my text item delimiters to dlm
    return xs
end splitOn


-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
    set mp to mReturn(p)
    set v to x
    
    tell mReturn(f)
        repeat until mp's |λ|(v)
            set v to |λ|(v)
        end repeat
    end tell
    return v
end |until|
Output:
{1, 7, 10, 13, 19, 23, 28, 31}

Applesoft BASIC

 0 C = 8: DIM S(16):B = 10: PRINT "THE FIRST "C" HAPPY NUMBERS": FOR R = C TO 0 STEP 0:N = H: GOSUB 1: PRINT  MID$ (" " +  STR$ (H),1 + (R = C),255 * I);:R = R - I:H = H + 1: NEXT R: END 
 1 S = 0: GOSUB 3:I = N = 1: IF  NOT Q THEN  RETURN 
 2  FOR Q = 1 TO 0 STEP 0:S(S) = N:S = S + 1: GOSUB 6:N = T: GOSUB 3: NEXT Q:I = N = 1: RETURN 
 3 Q = N > 1: IF  NOT Q OR  NOT S THEN  RETURN 
 4 Q = 0: FOR I = 0 TO S - 1: IF N = S(I) THEN  RETURN 
 5  NEXT I:Q = 1: RETURN 
 6 T = 0: FOR I = N TO 0 STEP 0:M =  INT (I / B):T =  INT (T + (I - M * B) ^ 2):I = M: NEXT I: RETURN
Output:
THE FIRST 8 HAPPY NUMBERS
1 7 10 13 19 23 28 31

Arturo

Translation of: Nim
ord0: to :integer `0`
happy?: function [x][
    n: x
    past: new []

    while [n <> 1][
        s: to :string n
        n: 0
        loop s 'c [
            i: (to :integer c) - ord0
            n: n + i * i
        ]
        if contains? past n -> return false
        'past ++ n
    ]
    return true
]

loop 0..31 'x [
    if happy? x -> print x
]
Output:
1
7
10
13
19
23
28
31

AutoHotkey

Loop {
  If isHappy(A_Index) {
    out .= (out="" ? "" : ",") . A_Index
    i ++
    If (i = 8) {
      MsgBox, The first 8 happy numbers are: %out%
      ExitApp
    }
  }
}

isHappy(num, list="") {
  list .= (list="" ? "" : ",") . num
  Loop, Parse, num
    sum += A_LoopField ** 2
  If (sum = 1)
    Return true
  Else If sum in %list%
    Return false
  Else Return isHappy(sum, list)
}
The first 8 happy numbers are: 1,7,10,13,19,23,28,31

Alternative version

while h < 8
    if (Happy(A_Index)) {
        Out .= A_Index A_Space
        h++
    }
MsgBox, % Out

Happy(n) {
    Loop, {
        Loop, Parse, n
            t += A_LoopField ** 2
        if (t = 89)
            return, 0
        if (t = 1)
            return, 1
        n := t, t := 0
    }
}
1 7 10 13 19 23 28 31

AutoIt

$c = 0
$k = 0
While $c < 8
	$k += 1
	$n = $k
	While $n <> 1
		$s = StringSplit($n, "")
		$t = 0
		For $i = 1 To $s[0]
			$t += $s[$i] ^ 2
		Next
		$n = $t
		Switch $n
			Case 4,16,37,58,89,145,42,20
			ExitLoop
		EndSwitch
	WEnd
	If $n = 1 Then
		ConsoleWrite($k & " is Happy" & @CRLF)
		$c += 1
	EndIf
WEnd
Use a set of numbers (4,16,37,58,89,145,42,20) to indicate a loop and exit.
Output:
1 is Happy
7 is Happy
10 is Happy
13 is Happy
19 is Happy
23 is Happy
28 is Happy
31 is Happy

Alternative version

$c = 0
$k = 0
While $c < 8
	$a = ObjCreate("System.Collections.ArrayList")
	$k += 1
	$n = $k
	While $n <> 1
		If $a.Contains($n) Then
			ExitLoop
		EndIf
		$a.add($n)
		$s = StringSplit($n, "")
		$t = 0
		For $i = 1 To $s[0]
			$t += $s[$i] ^ 2
		Next
		$n = $t
	WEnd
	If $n = 1 Then
		ConsoleWrite($k & " is Happy" & @CRLF)
		$c += 1
	EndIf
	$a.Clear
WEnd
Saves all numbers in a list, duplicate entry indicates a loop.
Output:
1 is Happy
7 is Happy
10 is Happy
13 is Happy
19 is Happy
23 is Happy
28 is Happy
31 is Happy

AWK

function is_happy(n)
{
  if ( n in happy ) return 1;
  if ( n in unhappy ) return 0;
  cycle[""] = 0
  while( (n!=1) && !(n in cycle) ) {
    cycle[n] = n
    new_n = 0
    while(n>0) {
      d = n % 10
      new_n += d*d
      n = int(n/10)
    }
    n = new_n
  }
  if ( n == 1 ) {
    for (i_ in cycle) {
      happy[cycle[i_]] = 1
      delete cycle[i_]
    }
    return 1
  } else {
    for (i_ in cycle) {
      unhappy[cycle[i_]] = 1
      delete cycle[i_]
    }
    return 0
  }
}

BEGIN {
  cnt = 0
  happy[""] = 0
  unhappy[""] = 0
  for(j=1; (cnt < 8); j++) {
    if ( is_happy(j) == 1 ) {
      cnt++
      print j
    }
  }
}

Result:

1
7
10
13
19
23
28
31

Alternative version

Alternately, for legibility one might write:

BEGIN {
    for (i = 1; i < 50; ++i){
        if (isHappy(i)) {
            print i;
        }
    }
    exit
}

function isHappy(n,    seen) {
    delete seen;
    while (1) {
        n = sumSqrDig(n)
        if (seen[n]) {
            return n == 1
        }
        seen[n] = 1
    }
}

function sumSqrDig(n,     d, tot) {
    while (n) {
        d = n % 10
        tot += d * d
        n = int(n/10)
    }
    return tot
}

BASIC256

n = 1 : cnt = 0
print "The first 8 isHappy numbers are:"
print

while cnt < 8
    if isHappy(n) = 1 then
        cnt += 1
        print cnt; " => "; n
    end if
    n += 1
end while

function isHappy(num)
    isHappy = 0
    cont = 0
    while cont < 50 and isHappy <> 1
        num$ = string(num)
        cont += 1
        isHappy	= 0
        for i = 1 to length(num$)
            isHappy += int(mid(num$,i,1)) ^ 2
        next i
        num = isHappy
    end while
end function

Batch File

happy.bat

@echo off
setlocal enableDelayedExpansion
::Define a list with 10 terms as a convenience for defining a loop
set "L10=0 1 2 3 4 5 6 7 8 9"
shift /1 & goto %1
exit /b


:list min count
:: This routine prints all happy numbers > min (arg1)
:: until it finds count (arg2) happy numbers.
set /a "n=%~1, cnt=%~2"
call :listInternal
exit /b


:test min [max]
:: This routine sequentially tests numbers between min (arg1) and max (arg2)
:: to see if they are happy. If max is not specified then it defaults to min.
set /a "min=%~1"
if "%~2" neq "" (set /a "max=%~2") else set max=%min%
::The FOR /L loop does not detect integer overflow, so must protect against
::an infinite loop when max=0x7FFFFFFFF
set end=%max%
if %end% equ 2147483647 set /a end-=1
for /l %%N in (%min% 1 %end%) do (
  call :testInternal %%N && (echo %%N is happy :^)) || echo %%N is sad :(
)
if %end% neq %max% call :testInternal %max% && (echo %max% is happy :^)) || echo %max% is sad :(
exit /b


  :listInternal
  :: This loop sequentially tests each number >= n. The loop conditionally
  :: breaks within the body once cnt happy numbers have been found, or if
  :: the max integer value is reached. Performance is improved by using a
  :: FOR loop to perform most of the looping, with a GOTO only needed once
  :: per 100 iterations.
  for %%. in (
    %L10% %L10% %L10% %L10% %L10% %L10% %L10% %L10% %L10% %L10%
  ) do (
    call :testInternal !n! && (
      echo !n!
      set /a cnt-=1
      if !cnt! leq 0 exit /b 0
    )
    if !n! equ 2147483647 (
      >&2 echo ERROR: Maximum integer value reached
      exit /b 1
    )
    set /a n+=1
  )
  goto :listInternal


  :testInternal n
  :: This routine loops until the sum of squared digits converges on 1 (happy)
  :: or it detects a cycle (sad). It exits with errorlevel 0 for happy and 1 for sad.
  :: Performance is improved by using a FOR loop for the looping instead of a GOTO.
  :: Numbers less than 1000 never neeed more than 20 iterations, and any number
  :: with 4 or more digits shrinks by at least one digit each iteration.
  :: Since Windows batch can't handle more than 10 digits, allowance for 27
  :: iterations is enough, and 30 is more than adequate.
  setlocal
  set n=%1
  for %%. in (%L10% %L10% %L10%) do (
    if !n!==1 exit /b 0
    %= Only numbers < 1000 can cycle =%
    if !n! lss 1000 (
      if defined t.!n! exit /b 1
      set t.!n!=1
    )
    %= Sum the squared digits                                          =%
    %= Batch can't handle numbers greater than 10 digits so we can use =%
    %= a constrained FOR loop and avoid a slow goto                    =%
    set sum=0
    for /l %%N in (1 1 10) do (
      if !n! gtr 0 set /a "sum+=(n%%10)*(n%%10), n/=10"
    )
    set /a n=sum
  )

Sample usage and output

>happy list 1 8
1
7
10
13
19
23
28
31

>happy list 1000000000 10
1000000000
1000000003
1000000009
1000000029
1000000030
1000000033
1000000039
1000000067
1000000076
1000000088

>happy test 30
30 is sad :(

>happy test 31
31 is happy :)

>happy test 1 10
1 is happy :)
2 is sad :(
3 is sad :(
4 is sad :(
5 is sad :(
6 is sad :(
7 is happy :)
8 is sad :(
9 is sad :(
10 is happy :)

>happy test "50 + 10 * 5"
100 is happy :)

>happy test 0x7fffffff
2147483647 is sad :(

>happy test 0x7ffffffd
2147483645 is happy :)

>happy list 0x7ffffff0 10
2147483632
2147483645
ERROR: Maximum integer value reached

BBC BASIC

      number% = 0
      total% = 0
      REPEAT
        number% += 1
        IF FNhappy(number%) THEN
          PRINT number% " is a happy number"
          total% += 1
        ENDIF
      UNTIL total% = 8
      END
      
      DEF FNhappy(num%)
      LOCAL digit&()
      DIM digit&(10)
      REPEAT
        digit&() = 0
        $$^digit&(0) = STR$(num%)
        digit&() AND= 15
        num% = MOD(digit&())^2 + 0.5
      UNTIL num% = 1 OR num% = 4
      = (num% = 1)

Output:

         1 is a happy number
         7 is a happy number
        10 is a happy number
        13 is a happy number
        19 is a happy number
        23 is a happy number
        28 is a happy number
        31 is a happy number

BCPL

get "libhdr"

let sumdigitsq(n) = 
    n=0 -> 0, (n rem 10)*(n rem 10)+sumdigitsq(n/10)
    
let happy(n) = valof
$(  let seen = vec 255
    for i = 0 to 255 do i!seen := false
    $(  n!seen := true
        n := sumdigitsq(n)
    $) repeatuntil n!seen
    resultis 1!seen
$)

let start() be
$(  let n, i = 0, 0
    while n < 8 do
    $(  if happy(i) do
        $(  n := n + 1
            writef("%N ",i)
        $)
        i := i + 1
    $)
    wrch('*N')
$)
Output:
1 7 10 13 19 23 28 31

Bori

bool isHappy (int n)
{
   ints cache;
	
   while (n != 1)
   {
      int sum = 0;
	
      if (cache.contains(n))
         return false;
		
      cache.add(n);
      while (n != 0)
      {
         int digit = n % 10;
         sum += (digit * digit);
         n = (int)(n / 10);
      }
      n = sum;
   }
   return true;
}

void test ()
{
   int num = 1;            
   ints happynums;
	
   while (happynums.count() < 8)            
   {
      if (isHappy(num))
         happynums.add(num);
      num++;
   }
   puts("First 8 happy numbers : " + str.newline + happynums);
}

Output:

First 8 happy numbers : 
[1, 7, 10, 13, 19, 23, 28, 31]

BQN

SumSqDgt  +´2˜ •Fmt-'0'˙
Happy  ⟨⟩{𝕨((⊑∊˜ )∾𝕊(SumSqDgt⊢),1=⊢)𝕩}
8↑Happy¨/↕50
Output:
⟨ 1 7 10 13 19 23 28 31 ⟩

Brat

include :set

happiness = set.new 1
sadness = set.new

sum_of_squares_of_digits = { num |
  num.to_s.dice.reduce 0 { sum, n | sum = sum + n.to_i ^ 2 }
}

happy? = { n, seen = set.new |
  when {true? happiness.include? n } { happiness.merge seen << n; true }
    { true? sadness.include? n } { sadness.merge seen; false }
    { true? seen.include? n } { sadness.merge seen; false }
    { true } { seen << n; happy? sum_of_squares_of_digits(n), seen }
}

num = 1
happies = []

while { happies.length < 8 } {
  true? happy?(num)
    { happies << num }

  num = num + 1
}

p "First eight happy numbers: #{happies}"
p "Happy numbers found: #{happiness.to_array.sort}"
p "Sad numbers found: #{sadness.to_array.sort}"

Output:

First eight happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]
Happy numbers found: [1, 7, 10, 13, 19, 23, 28, 31, 49, 68, 82, 97, 100, 130]
Sad numbers found: [2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15, 16, 17, 18, 20, 21, 22, 24, 25, 26, 27, 29, 30, 34, 36, 37, 40, 41, 42, 45, 50, 52, 53, 58, 61, 64, 65, 81, 85, 89, 145]

C

Recursively look up if digit square sum is happy.

#include <stdio.h>

#define CACHE 256
enum { h_unknown = 0, h_yes, h_no };
unsigned char buf[CACHE] = {0, h_yes, 0};

int happy(int n)
{
	int sum = 0, x, nn;
	if (n < CACHE) {
		if (buf[n]) return 2 - buf[n];
		buf[n] = h_no;
	}

	for (nn = n; nn; nn /= 10) x = nn % 10, sum += x * x;

	x = happy(sum);
	if (n < CACHE) buf[n] = 2 - x;
	return x;
}

int main()
{
	int i, cnt = 8;
	for (i = 1; cnt || !printf("\n"); i++)
		if (happy(i)) --cnt, printf("%d ", i);

	printf("The %dth happy number: ", cnt = 1000000);
	for (i = 1; cnt; i++)
		if (happy(i)) --cnt || printf("%d\n", i);

	return 0;
}

output

1 7 10 13 19 23 28 31 
The 1000000th happy number: 7105849

Without caching, using cycle detection:

#include <stdio.h>

int dsum(int n)
{
	int sum, x;
	for (sum = 0; n; n /= 10) x = n % 10, sum += x * x;
	return sum;
}

int happy(int n)
{
	int nn;
	while (n > 999) n = dsum(n); /* 4 digit numbers can't cycle */
	nn = dsum(n);
	while (nn != n && nn != 1)
		n = dsum(n), nn = dsum(dsum(nn));
	return n == 1;
}

int main()
{
	int i, cnt = 8;
	for (i = 1; cnt || !printf("\n"); i++)
		if (happy(i)) --cnt, printf("%d ", i);

	printf("The %dth happy number: ", cnt = 1000000);
	for (i = 1; cnt; i++)
		if (happy(i)) --cnt || printf("%d\n", i);

	return 0;
}

Output is same as above, but much slower.

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HappyNums
{
    class Program
    {
        public static bool ishappy(int n)
        {
            List<int> cache = new List<int>();
            int sum = 0;
            while (n != 1)
            {
                if (cache.Contains(n))
                {
                    return false;
                }
                cache.Add(n);
                while (n != 0)
                {
                    int digit = n % 10;
                    sum += digit * digit;
                    n /= 10;
                }
                n = sum;
                sum = 0;
            }
           return true;            
        }

        static void Main(string[] args)
        {
            int num = 1;
            List<int> happynums = new List<int>();

            while (happynums.Count < 8)
            {
                if (ishappy(num))
                {
                    happynums.Add(num);
                }
                num++;
            }
            Console.WriteLine("First 8 happy numbers : " + string.Join(",", happynums));
        }
    }
}
First 8 happy numbers : 1,7,10,13,19,23,28,31

Alternate (cacheless)

Instead of caching and checking for being stuck in a loop, one can terminate on the "unhappy" endpoint of 89. One might be temped to try caching the so-far-found happy and unhappy numbers and checking the cache to speed things up. However, I have found that the cache implementation overhead reduces performance compared to this cacheless version.

Reaching 10 million, the <34 second computation time was from Tio.run. It takes under 5 seconds on a somewhat modern CPU. If you edit it to max out at 100 million, it takes about 50 seconds (on the somewhat modern CPU).

using System;
using System.Collections.Generic;
class Program
{

    static int[] sq = { 1, 4, 9, 16, 25, 36, 49, 64, 81 };

    static bool isOne(int x)
    {
        while (true)
        {
            if (x == 89) return false;
            int s = 0, t;
            do if ((t = (x % 10) - 1) >= 0) s += sq[t]; while ((x /= 10) > 0);
            if (s == 1) return true;
            x = s;
        }
    }

    static void Main(string[] args)
    {
        const int Max = 10_000_000; DateTime st = DateTime.Now;
        Console.Write("---Happy Numbers---\nThe first 8:");
        int c = 0, i; for (i = 1; c < 8; i++)
            if (isOne(i)) Console.Write("{0} {1}", c == 0 ? "" : ",", i, ++c);
        for (int m = 10; m <= Max; m *= 10)
        {
            Console.Write("\nThe {0:n0}th: ", m);
            for (; c < m; i++) if (isOne(i)) c++;
            Console.Write("{0:n0}", i - 1);
        }
        Console.WriteLine("\nComputation time {0} seconds.", (DateTime.Now - st).TotalSeconds);
    }
}
Output:
---Happy Numbers---
The first 8: 1, 7, 10, 13, 19, 23, 28, 31
The 10th: 44
The 100th: 694
The 1,000th: 6,899
The 10,000th: 67,169
The 100,000th: 692,961
The 1,000,000th: 7,105,849
The 10,000,000th: 71,313,350
Computation time 33.264518 seconds.

C++

Translation of: Python
#include <map>
#include <set>

bool happy(int number) {
  static std::map<int, bool> cache;

  std::set<int> cycle;
  while (number != 1 && !cycle.count(number)) {
    if (cache.count(number)) {
      number = cache[number] ? 1 : 0;
      break;
    }
    cycle.insert(number);
    int newnumber = 0;
    while (number > 0) {
      int digit = number % 10;
      newnumber += digit * digit;
      number /= 10;
    }
    number = newnumber;
  }
  bool happiness = number == 1;
  for (std::set<int>::const_iterator it = cycle.begin();
       it != cycle.end(); it++)
    cache[*it] = happiness;
  return happiness;
}

#include <iostream>

int main() {
  for (int i = 1; i < 50; i++)
    if (happy(i))
      std::cout << i << std::endl;
  return 0;
}

Output:

1
7
10
13
19
23
28
31
32
44
49

Alternative version without caching:

unsigned int happy_iteration(unsigned int n)
{
  unsigned int result = 0;
  while (n > 0)
  {
    unsigned int lastdig = n % 10;
    result += lastdig*lastdig;
    n /= 10;
  }
  return result;
}

bool is_happy(unsigned int n)
{
  unsigned int n2 = happy_iteration(n);
  while (n != n2)
  {
    n = happy_iteration(n);
    n2 = happy_iteration(happy_iteration(n2));
  }
  return n == 1;
}

#include <iostream>

int main()
{
  unsigned int current_number = 1;

  unsigned int happy_count = 0;
  while (happy_count != 8)
  {
    if (is_happy(current_number))
    {
      std::cout << current_number << " ";
      ++happy_count;
    }
    ++current_number;
  }
  std::cout << std::endl;
}

Output:

1 7 10 13 19 23 28 31 

Cycle detection in is_happy() above is done using Floyd's cycle-finding algorithm.

Clojure

(defn happy? [n]
  (loop [n n, seen #{}]
    (cond 
      (= n 1)  true
      (seen n) false
      :else
        (recur (->> (str n)
                    (map #(Character/digit % 10))
                    (map #(* % %))
                    (reduce +))
               (conj seen n)))))

(def happy-numbers (filter happy? (iterate inc 1)))

(println (take 8 happy-numbers))

Output:

(1 7 10 13 19 23 28 31)

Alternate Version (with caching)

(require '[clojure.set :refer [union]])

(def ^{:private true} cache {:happy (atom #{}) :sad (atom #{})})

(defn break-apart [n]
  (->> (str n)
       (map str)
       (map #(Long/parseLong %))))

(defn next-number [n]
  (->> (break-apart n)
       (map #(* % %))
       (apply +)))

(defn happy-or-sad? [prev n]
  (cond (or (= n 1) ((deref (:happy cache)) n)) :happy
	(or ((deref (:sad cache)) n) (some #(= % n) prev)) :sad
	:else :unknown))

(defn happy-algo [n]
  (let [get-next (fn [[prev n]] [(conj prev n) (next-number n)])
	my-happy-or-sad? (fn [[prev n]] [(happy-or-sad? prev n) (conj prev n)])
	unknown? (fn [[res nums]] (= res :unknown))
	[res nums] (->> [#{} n]
			(iterate get-next)
			(map my-happy-or-sad?)
			(drop-while unknown?)
			first)
	_ (swap! (res cache) union nums)]
    res))

(def happy-numbers (->> (iterate inc 1) 
                        (filter #(= :happy (happy-algo %)))))

(println (take 8 happy-numbers))

Same output.

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
Output:
1
7
10
13
19
23
28
31

CoffeeScript

happy = (n) ->
  seen = {}
  while true
    n = sum_digit_squares(n)
    return true if n == 1
    return false if seen[n]
    seen[n] = true
    
sum_digit_squares = (n) ->
  sum = 0
  for c in n.toString()
    d = parseInt(c)
    sum += d*d
  sum
    
i = 1
cnt = 0
while cnt < 8
  if happy(i)
    console.log i
    cnt += 1
  i += 1

output

> coffee happy.coffee 
1
7
10
13
19
23
28
31

Common Lisp

(defun sqr (n)
  (* n n))

(defun sum-of-sqr-dgts (n)
  (loop for i = n then (floor i 10)
        while (plusp i)
        sum (sqr (mod i 10))))
 
(defun happy-p (n &optional cache)
  (or (= n 1) 
      (unless (find n cache)
        (happy-p (sum-of-sqr-dgts n)
                 (cons n cache)))))
 
(defun happys (&aux (happys 0))
  (loop for i from 1
        while (< happys 8)
        when (happy-p i)
        collect i and do (incf happys)))
 
(print (happys))

Output:

(1 7 10 13 19 23 28 31)

Cowgol

include "cowgol.coh";

sub sumDigitSquare(n: uint8): (s: uint8) is
    s := 0;
    while n != 0 loop
        var d := n % 10;
        s := s + d * d;
        n := n / 10;
    end loop;
end sub;

sub isHappy(n: uint8): (h: uint8) is
    var seen: uint8[256];
    MemZero(&seen[0], @bytesof seen);

    while seen[n] == 0 loop
        seen[n] := 1;
        n := sumDigitSquare(n);
    end loop;

    if n == 1 then
        h := 1;
    else
        h := 0;
    end if;
end sub;

var n: uint8 := 1;
var seen: uint8 := 0;

while seen < 8 loop
    if isHappy(n) != 0 then
        print_i8(n);
        print_nl();
        seen := seen + 1;
    end if;
    n := n + 1;
end loop;
Output:
1
7
10
13
19
23
28
31

Crystal

Translation of: Ruby
def happy?(n)
  past = [] of Int32 | Int64
  until n == 1
    sum = 0; while n > 0; sum += (n % 10) ** 2; n //= 10 end
    return false if past.includes? (n = sum)
    past << n
  end
  true
end

i = count = 0
until count == 8; (puts i; count += 1) if happy?(i += 1) end
puts
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }
Output:
1
7
10
13
19
23
28
31

99999999999901
99999999999910
99999999999914
99999999999915
99999999999916
99999999999937
99999999999941
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973

D

bool isHappy(int n) pure nothrow {
    int[int] past;

    while (true) {
        int total = 0;
        while (n > 0) {
            total += (n % 10) ^^ 2;
            n /= 10;
        }
        if (total == 1)
            return true;
        if (total in past)
            return false;
        n = total;
        past[total] = 0;
    }
}

void main() {
    import std.stdio, std.algorithm, std.range;

    int.max.iota.filter!isHappy.take(8).writeln;
}
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Alternative Version

import std.stdio, std.algorithm, std.range, std.conv, std.string;

bool isHappy(int n) pure nothrow {
    int[int] seen;

    while (true) {
        immutable t = n.text.representation.map!q{(a - '0') ^^ 2}.sum;
        if (t == 1)
            return true;
        if (t in seen)
            return false;
        n = t;
        seen[t] = 0;
    }
}

void main() {
    int.max.iota.filter!isHappy.take(8).writeln;
}

Same output.

Dart

main() {
  HashMap<int,bool> happy=new HashMap<int,bool>();
  happy[1]=true;

  int count=0;
  int i=0;

  while(count<8) {
    if(happy[i]==null) {
      int j=i;
      Set<int> sequence=new Set<int>();
      while(happy[j]==null && !sequence.contains(j)) {
        sequence.add(j);
        int sum=0;
        int val=j;
        while(val>0) {
          int digit=val%10;
          sum+=digit*digit;
          val=(val/10).toInt();
        }
        j=sum;
      }
      bool sequenceHappy=happy[j];
      Iterator<int> it=sequence.iterator();
      while(it.hasNext()) {
        happy[it.next()]=sequenceHappy;
      }
    }
    if(happy[i]) {
      print(i);
      count++;
    }
    i++;
  }
}

dc

[lcI~rscd*+lc0<H]sH
[0rsclHxd4<h]sh
[lIp]s_
0sI[lI1+dsIlhx2>_z8>s]dssx

Output:

1
7
10
13
19
23
28
31

DCL

$ happy_1 = 1
$ found = 0
$ i = 1
$ loop1:
$  n = i
$  seen_list = ","
$  loop2:
$   if f$type( happy_'n ) .nes. "" then $ goto happy
$   if f$type( unhappy_'n ) .nes. "" then $ goto unhappy
$   if f$locate( "," + n + ",", seen_list ) .eq. f$length( seen_list )
$   then
$    seen_list = seen_list + f$string( n ) + ","
$   else
$    goto unhappy
$   endif
$   ns = f$string( n )
$   nl = f$length( ns )
$   j = 0
$   sumsq = 0
$   loop3:
$    digit = f$integer( f$extract( j, 1, ns ))
$    sumsq = sumsq + digit * digit
$    j = j + 1
$    if j .lt. nl then $ goto loop3
$    n = sumsq
$   goto loop2
$  unhappy:
$  j = 1
$  loop4:
$   x = f$element( j, ",", seen_list )
$   if x .eqs. "" then $ goto continue
$   unhappy_'x = 1
$   j = j + 1
$   goto loop4
$  happy:
$  found = found + 1
$  found_'found = i
$  if found .eq. 8 then $ goto done
$  j = 1
$  loop5:
$   x = f$element( j, ",", seen_list )
$   if x .eqs. "" then $ goto continue
$   happy_'x = 1
$   j = j + 1
$   goto loop5
$  continue:
$  i = i + 1
$  goto loop1
$ done:
$ show symbol found*
Output:
  FOUND = 8   Hex = 00000008  Octal = 00000000010
  FOUND_1 = 1   Hex = 00000001  Octal = 00000000001
  FOUND_2 = 7   Hex = 00000007  Octal = 00000000007
  FOUND_3 = 10   Hex = 0000000A  Octal = 00000000012
  FOUND_4 = 13   Hex = 0000000D  Octal = 00000000015
  FOUND_5 = 19   Hex = 00000013  Octal = 00000000023
  FOUND_6 = 23   Hex = 00000017  Octal = 00000000027
  FOUND_7 = 28   Hex = 0000001C  Octal = 00000000034
  FOUND_8 = 31   Hex = 0000001F  Octal = 00000000037

Delphi

Library: Boost.Int

Adaptation of #Pascal. The lib Boost.Int can be found here [1]

program Happy_numbers;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  Boost.Int;

type
  TIntegerDynArray = TArray<Integer>;

  TIntHelper = record helper for Integer
    function IsHappy: Boolean;
    procedure Next;
  end;

{ TIntHelper }

function TIntHelper.IsHappy: Boolean;
var
  cache: TIntegerDynArray;
  sum, n: integer;
begin
  n := self;
  repeat
    sum := 0;
    while n > 0 do
    begin
      sum := sum + (n mod 10) * (n mod 10);
      n := n div 10;
    end;
    if sum = 1 then
      exit(True);

    if cache.Has(sum) then
      exit(False);
    n := sum;
    cache.Add(sum);
  until false;
end;

procedure TIntHelper.Next;
begin
  inc(self);
end;

var
  count, n: integer;

begin
  n := 1;
  count := 0;
  while count < 8 do
  begin
    if n.IsHappy then
    begin
      count.Next;
      write(n, ' ');
    end;
    n.Next;
  end;
  writeln;
  readln;
end.
Output:
1 7 10 13 19 23 28 31

Draco

proc nonrec dsumsq(byte n) byte:
    byte r, d;
    r := 0;
    while n~=0 do
        d := n % 10;
        n := n / 10;
        r := r + d * d
    od;
    r
corp

proc nonrec happy(byte n) bool:
    [256] bool seen;
    byte i;
    for i from 0 upto 255 do seen[i] := false od;
    while not seen[n] do
        seen[n] := true;
        n := dsumsq(n)
    od;
    seen[1]
corp

proc nonrec main() void:
    byte n, seen;
    n := 1;
    seen := 0;
    while seen < 8 do
        if happy(n) then
            writeln(n:3);
            seen := seen + 1
        fi;
        n := n + 1
    od
corp
Output:
  1
  7
 10
 13
 19
 23
 28
 31

DWScript

function IsHappy(n : Integer) : Boolean;
var
   cache : array of Integer;
   sum : Integer;
begin
   while True do begin
      sum := 0;
      while n>0 do begin
         sum += Sqr(n mod 10);
         n := n div 10;
      end;
      if sum = 1 then
         Exit(True);
      if sum in cache then
         Exit(False);
      n := sum;
      cache.Add(sum);
   end;
end;

var n := 8;
var i : Integer;

while n>0 do begin
   Inc(i);
   if IsHappy(i) then begin
      PrintLn(i);
      Dec(n);
   end;
end;

Output:

1
7
10
13
19
23
28
31

Dyalect

func happy(n) {
    var m = []
    while n > 1 {
        m.Add(n)
        var x = n
        n = 0
        while x > 0 {
            var d = x % 10
            n += d * d
            x /= 10
        }
        if m.IndexOf(n) != -1 {
            return false
        }
    }
    return true
}
 
var (n, found) = (1, 0)
while found < 8 {
    if happy(n) {
        print("\(n) ", terminator: "")
        found += 1
    }
    n += 1
}
print()
Output:
1 7 10 13 19 23 28 31

Déjà Vu

next-num:
	0
	while over:
		over
		* dup % swap 10
		+
		swap floor / swap 10 swap
	drop swap

is-happy happies n:
	if has happies n:
		return happies! n
	local :seq set{ n }
	n
	while /= 1 dup:
		next-num
		if has seq dup:
			drop
			set-to happies n false
			return false
		if has happies dup:
			set-to happies n dup happies!
			return
		set-to seq over true
	drop
	set-to happies n true
	true

local :h {}
1 0
while > 8 over:
	if is-happy h dup:
		!print( "A happy number: " over )
		swap ++ swap
	++
drop
drop
Output:
A happy number: 1
A happy number: 7
A happy number: 10
A happy number: 13
A happy number: 19
A happy number: 23
A happy number: 28
A happy number: 31

E

This example does not show the output mentioned in the task description on this page (or a page linked to from here). Please ensure that it meets all task requirements and remove this message.
Note that phrases in task descriptions such as "print and display" and "print and show" for example, indicate that (reasonable length) output be a part of a language's solution.


def isHappyNumber(var x :int) {
  var seen := [].asSet()
  while (!seen.contains(x)) {
    seen with= x
    var sum := 0
    while (x > 0) {
      sum += (x % 10) ** 2
      x //= 10
    }
    x := sum
    if (x == 1) { return true }
  }
  return false
}

var count := 0
for x ? (isHappyNumber(x)) in (int >= 1) {
  println(x)
  if ((count += 1) >= 8) { break }
}

Eiffel

class
	APPLICATION

create
	make

feature {NONE} -- Initialization

	make
			-- Run application.
		local
			l_val: INTEGER
		do
			from
				l_val := 1
			until
				l_val > 100
			loop
				if is_happy_number (l_val) then
					print (l_val.out)
					print ("%N")
				end
				l_val := l_val + 1
			end
		end

feature -- Happy number

	is_happy_number (a_number: INTEGER): BOOLEAN
			-- Is `a_number' a happy number?
		require
			positive_number: a_number > 0
		local
			l_number: INTEGER
			l_set: ARRAYED_SET [INTEGER]
		do
			from
				l_number := a_number
				create l_set.make (10)
			until
				l_number = 1 or l_set.has (l_number)
			loop
				l_set.put (l_number)
				l_number := square_sum_of_digits (l_number)
			end

			Result := (l_number = 1)
		end

feature{NONE} -- Implementation

	square_sum_of_digits (a_number: INTEGER): INTEGER
			-- Sum of the sqares of digits of `a_number'.
		require
			positive_number: a_number > 0
		local
			l_number, l_digit: INTEGER
		do
			from
				l_number := a_number
			until
				l_number = 0
			loop
				l_digit := l_number \\ 10
				Result := Result + l_digit * l_digit
				l_number := l_number // 10
			end
		end

end

Elena

Translation of: C#

ELENA 4.x :

import extensions;
import system'collections;
import system'routines;
 
isHappy(int n)
{
    auto cache := new List<int>(5);
    int sum := 0;
    int num := n;
    while (num != 1)
    {
        if (cache.indexOfElement:num != -1)
        {
            ^ false
        };
        cache.append(num);
        while (num != 0)
        {
            int digit := num.mod:10;
            sum += (digit*digit);
            num /= 10
        };
        num := sum;
        sum := 0
    };
 
    ^ true
}
 
public program()
{
    auto happynums  := new List<int>(8);
    int num := 1;
    while (happynums.Length < 8)
    {
        if (isHappy(num))
        {
            happynums.append(num)
        };
 
        num += 1
    };
    console.printLine("First 8 happy numbers: ", happynums.asEnumerable())
}
Output:
First 8 happy numbers: 1,7,10,13,19,23,28,31

Elixir

defmodule Happy do
  def task(num) do
    Process.put({:happy, 1}, true)
    Stream.iterate(1, &(&1+1))
    |> Stream.filter(fn n -> happy?(n) end)
    |> Enum.take(num)
  end
  
  defp happy?(n) do
    sum = square_sum(n, 0)
    val = Process.get({:happy, sum})
    if val == nil do
      Process.put({:happy, sum}, false)
      val = happy?(sum)
      Process.put({:happy, sum}, val)
    end
    val
  end
  
  defp square_sum(0, sum), do: sum
  defp square_sum(n, sum) do
    r = rem(n, 10)
    square_sum(div(n, 10), sum + r*r)
  end
end

IO.inspect Happy.task(8)
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Erlang

-module(tasks).
-export([main/0]).
-import(lists, [map/2, member/2, sort/1, sum/1]).

is_happy(X, XS) ->
    if
	X == 1 ->
	    true;
	X < 1 ->
	    false;
	true ->
	    case member(X, XS) of
		true -> false;
		false ->
		    is_happy(sum(map(fun(Z) -> Z*Z end, 
				     [Y - 48 || Y <- integer_to_list(X)])),
			     [X|XS])
	    end
    end.

main(X, XS) ->
    if
	length(XS) == 8 ->
	    io:format("8 Happy Numbers: ~w~n", [sort(XS)]);
	true ->
	    case is_happy(X, []) of
		true -> main(X + 1, [X|XS]);
		false -> main(X + 1, XS)
	    end
    end.
main() ->
    main(0, []).

Command:

erl -run tasks main -run init stop -noshell

Output:

8 Happy Numbers: [1,7,10,13,19,23,28,31]

In a more functional style (assumes integer_to_list/1 will convert to the ASCII value of a number, which then has to be converted to the integer value by subtracting 48):

-module(tasks).

-export([main/0]).

main() -> io:format("~w ~n", [happy_list(1, 8, [])]).

happy_list(_, N, L) when length(L) =:= N -> lists:reverse(L);
happy_list(X, N, L) -> 
	Happy = is_happy(X),
	if Happy -> happy_list(X + 1, N, [X|L]);
	true -> happy_list(X + 1, N, L) end.

is_happy(1) -> true;
is_happy(4) -> false;
is_happy(N) when N > 0 ->
	N_As_Digits = [Y - 48 || Y <- integer_to_list(N)],
	is_happy(lists:foldl(fun(X, Sum) -> (X * X) + Sum end, 0, N_As_Digits));
is_happy(_) -> false.

Output:

[1,7,10,13,19,23,28,31]

Euphoria

function is_happy(integer n)
    sequence seen
    integer k
    seen = {}
    while n > 1 do
        seen &= n
        k = 0
        while n > 0 do
            k += power(remainder(n,10),2)
            n = floor(n/10)
        end while
        n = k
        if find(n,seen) then
            return 0
        end if
    end while
    return 1
end function

integer n,count
n = 1
count = 0
while count < 8 do
    if is_happy(n) then
        ? n
        count += 1
    end if
    n += 1
end while

Output:

1
7
10
13
19
23
28
31

F#

This requires the F# power pack to be referenced and the 2010 beta of F#

open System.Collections.Generic
open Microsoft.FSharp.Collections

let answer =
    let sqr x = x*x                                                 // Classic square definition
    let rec AddDigitSquare n =
        match n with
        | 0 -> 0                                                    // Sum of squares for 0 is 0
        | _ -> sqr(n % 10) + (AddDigitSquare (n / 10))              // otherwise add square of bottom digit to recursive call
    let dict = new Dictionary<int, bool>()                          // Dictionary to memoize values
    let IsHappy n =
        if dict.ContainsKey(n) then                                 // If we've already discovered it
            dict.[n]                                                // Return previously discovered value
        else
            let cycle = new HashSet<_>(HashIdentity.Structural)     // Set to keep cycle values in
            let rec isHappyLoop n =
                if cycle.Contains n then n = 1                      // If there's a loop, return true if it's 1
                else
                    cycle.Add n |> ignore                           // else add this value to the cycle
                    isHappyLoop (AddDigitSquare n)                  // and check the next number in the cycle
            let f = isHappyLoop n                                   // Keep track of whether we're happy or not
            cycle |> Seq.iter (fun i -> dict.[i] <- f)              // and apply it to all the values in the cycle
            f                                                       // Return the boolean

    1                                                               // Starting with 1,
    |> Seq.unfold (fun i -> Some (i, i + 1))                        // make an infinite sequence of consecutive integers 
    |> Seq.filter IsHappy                                           // Keep only the happy ones
    |> Seq.truncate 8                                               // Stop when we've found 8
    |> Seq.iter (Printf.printf "%d\n")				    // Print results

Output:

1
7
10
13
19
23
28
31

Factor

USING: combinators kernel make math sequences ;

: squares ( n -- s )
    0 [ over 0 > ] [ [ 10 /mod sq ] dip + ] while nip ;

: (happy?) ( n1 n2 -- ? )
    [ squares ] [ squares squares ] bi* {
        { [ dup 1 = ] [ 2drop t ] }
        { [ 2dup = ] [ 2drop f ] }
        [ (happy?) ]
    } cond ;

: happy? ( n -- ? )
    dup (happy?) ;

: happy-numbers ( n -- seq )
    [
        0 [ over 0 > ] [
            dup happy? [ dup , [ 1 - ] dip ] when 1 +
        ] while 2drop
    ] { } make ;
Output:
8 happy-numbers ! { 1 7 10 13 19 23 28 31 }

FALSE

[$10/$10*@\-$*\]m:             {modulo squared and division}
[$m;![$9>][m;!@@+\]#$*+]s:     {sum of squares}
[$0[1ø1>][1ø3+ø3ø=|\1-\]#\%]f: {look for duplicates}

{check happy number}
[
  $1[f;!~2ø1=~&][1+\s;!@]#     {loop over sequence until 1 or duplicate}
  1ø1=                         {return value}
  \[$0=~][@%1-]#%              {drop sequence and counter}
]h:

0 1
"Happy numbers:"
[1ø8=~][h;![" "$.\1+\]?1+]#
%%
Output:
Happy numbers: 1 7 10 13 19 23 28 31

Fantom

class Main
{
  static Bool isHappy (Int n)
  {
    Int[] record := [,]
    while (n != 1 && !record.contains(n))
    { 
      record.add (n)
      // find sum of squares of digits
      newn := 0
      while (n > 0)
      { 
        newn += (n.mod(10) * n.mod(10))
        n = n.div(10)
      }
      n = newn
    }
    return (n == 1)
  }

  public static Void main ()
  {
    i := 1
    count := 0
    while (count < 8)
    {
      if (isHappy (i)) 
      {
        echo (i)
        count += 1
      }
      i += 1 
    }
  }
}

Output:

1
7
10
13
19
23
28
31

FOCAL

01.10 S J=0;S N=1;T %2
01.20 D 3;I (K-2)1.5
01.30 S N=N+1
01.40 I (J-8)1.2;Q
01.50 T N,!
01.60 S J=J+1
01.70 G 1.3

02.10 S A=K;S R=0
02.20 S B=FITR(A/10)
02.30 S R=R+(A-10*B)^2
02.40 S A=B
02.50 I (-A)2.2

03.10 F X=0,162;S S(X)=-1
03.20 S K=N
03.30 S S(K)=0
03.40 D 2;S K=R
03.50 I (S(K))3.3
Output:
=  1
=  7
= 10
= 13
= 19
= 23
= 28
= 31

Forth

: next ( n -- n )
  0 swap begin 10 /mod >r  dup * +  r> ?dup 0= until ;

: cycle? ( n -- ? )
  here dup @ cells +
  begin dup here >
  while 2dup @ = if 2drop true exit then
        1 cells -
  repeat
  1 over +!  dup @ cells + !  false ;

: happy? ( n -- ? )
  0 here !  begin next dup cycle? until  1 = ;

: happy-numbers ( n -- )
  0 swap 0 do
    begin 1+ dup happy? until dup .
  loop drop ;

8 happy-numbers  \ 1 7 10 13 19 23 28 31

Lookup Table

Every sequence either ends in 1, or contains a 4 as part of a cycle. Extending the table through 9 is a (modest) optimization/memoization. This executes '500000 happy-numbers' about 5 times faster than the above solution.

CREATE HAPPINESS 0 C, 1 C, 0 C, 0 C, 0 C, 0 C, 0 C, 1 C, 0 C, 0 C,
: next ( n -- n')
   0 swap BEGIN dup WHILE 10 /mod >r  dup * +  r> REPEAT drop ;
: happy? ( n -- t|f)
   BEGIN dup 10 >= WHILE next REPEAT  chars HAPPINESS + C@ 0<> ;
: happy-numbers ( n --)  >r 0
   BEGIN r@ WHILE
     BEGIN 1+ dup happy? UNTIL dup . r> 1- >r
   REPEAT r> drop drop ;
8 happy-numbers
Output:
1 7 10 13 19 23 28 31

Produces the 1 millionth happy number with:

: happy-number ( n -- n')  \ produce the nth happy number 
   >r 0  BEGIN r@ WHILE
     BEGIN 1+ dup happy? UNTIL  r> 1- >r        
   REPEAT r> drop ;
1000000 happy-number .  \ 7105849

in about 9 seconds.

Fortran

program happy

  implicit none
  integer, parameter :: find = 8
  integer :: found
  integer :: number

  found = 0
  number = 1
  do
    if (found == find) then
      exit
    end if
    if (is_happy (number)) then
      found = found + 1
      write (*, '(i0)') number
    end if
    number = number + 1
  end do

contains

  function sum_digits_squared (number) result (result)

    implicit none
    integer, intent (in) :: number
    integer :: result
    integer :: digit
    integer :: rest
    integer :: work

    result = 0
    work = number
    do
      if (work == 0) then
        exit
      end if
      rest = work / 10
      digit = work - 10 * rest
      result = result + digit * digit
      work = rest
    end do

  end function sum_digits_squared

  function is_happy (number) result (result)

    implicit none
    integer, intent (in) :: number
    logical :: result
    integer :: turtoise
    integer :: hare

    turtoise = number
    hare = number
    do
      turtoise = sum_digits_squared (turtoise)
      hare = sum_digits_squared (sum_digits_squared (hare))
      if (turtoise == hare) then
        exit
      end if
    end do
    result = turtoise == 1

  end function is_happy

end program happy

Output:

1
7
10
13
19
23
28
31

FreeBASIC

' FB 1.05.0 Win64

Function isHappy(n As Integer) As Boolean
  If n < 0 Then Return False
  ' Declare a dynamic array to store previous sums.
  ' If a previous sum is duplicated before a sum of 1 is reached
  ' then the number can't be "happy" as the cycle will just repeat
  Dim prevSums() As Integer 
  Dim As Integer digit, ub, sum = 0
  Do
    While n > 0
      digit = n Mod 10
      sum += digit * digit
      n \= 10
    Wend
    If sum = 1 Then Return True
    ub = UBound(prevSums)
    If ub > -1 Then
      For i As Integer = 0 To ub
         If sum = prevSums(i) Then Return False
      Next
    End If
    ub += 1 
    Redim Preserve prevSums(0 To ub)
    prevSums(ub) = sum
    n = sum
    sum  = 0
  Loop  
End Function
 
Dim As Integer n = 1, count = 0

Print "The first 8 happy numbers are : "
Print
While count < 8
  If isHappy(n) Then
    count += 1 
    Print count;" =>"; n
  End If
  n += 1
Wend 
Print
Print "Press any key to quit"
Sleep
Output:
 1 => 1
 2 => 7
 3 => 10
 4 => 13
 5 => 19
 6 => 23
 7 => 28
 8 => 31

Frege

Translation of: Haskell
Works with: Frege version 3.21.586-g026e8d7
module Happy where

import Prelude.Math
-- ugh, since Frege doesn't have Set, use Map instead
import Data.Map (member, insertMin, empty emptyMap)

digitToInteger :: Char -> Integer
digitToInteger c = fromInt $ (ord c) - (ord '0')

isHappy :: Integer -> Bool
isHappy = p emptyMap
  where p _ 1n = true
        p s n | n `member` s = false
              | otherwise  = p (insertMin n () s) (f n)
        f = sum . map (sqr . digitToInteger) . unpacked . show

main _ = putStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n
Output:
1 7 10 13 19 23 28 31
runtime 0.614 wallclock seconds.

Fōrmulæ

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its website, However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.

In this page you can see the program(s) related to this task and their results.

Go

package main

import "fmt"

func happy(n int) bool {
	m := make(map[int]bool)
	for n > 1 {
		m[n] = true
		var x int
		for x, n = n, 0; x > 0; x /= 10 {
			d := x % 10
			n += d * d
		}
		if m[n] {
			return false
		}
	}
	return true
}

func main() {
	for found, n := 0, 1; found < 8; n++ {
		if happy(n) {
			fmt.Print(n, " ")
			found++
		}
	}
	fmt.Println()
}
Output:
1 7 10 13 19 23 28 31

Groovy

Number.metaClass.isHappy = {
    def number = delegate as Long
    def cycle = new HashSet<Long>()
    while (number != 1 && !cycle.contains(number)) {
        cycle << number
        number = (number as String).collect { d = (it as Long); d * d }.sum()
    }
    number == 1
}

def matches = []
for (int i = 0; matches.size() < 8; i++) {
    if (i.happy) { matches << i }
}
println matches
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Harbour

PROCEDURE Main()
   LOCAL i := 8, nH := 0

   ? hb_StrFormat( "The first %d happy numbers are:", i )
   ?

   WHILE i > 0
      IF IsHappy( ++nH )
	?? hb_NtoS( nH ) + " "
	--i
      ENDIF
   END

   RETURN
	
STATIC FUNCTION IsHappy( nNumber )
   STATIC aUnhappy := {}
   LOCAL nDigit, nSum := 0, cNumber := hb_NtoS( nNumber )

   FOR EACH nDigit IN cNumber
      nSum += Val( nDigit ) ^ 2
   NEXT

   IF nSum == 1
      aUnhappy := {}
      RETURN .T.
   ELSEIF AScan( aUnhappy, nSum ) > 0
     RETURN .F.
   ENDIF

   AAdd( aUnhappy, nSum )

   RETURN IsHappy( nSum )

Output:

 The first 8 happy numbers are:
 1 7 10 13 19 23 28 31

Haskell

import Data.Char (digitToInt)
import Data.Set (member, insert, empty)

isHappy :: Integer -> Bool
isHappy = p empty
  where
    p _ 1 = True
    p s n
      | n `member` s = False
      | otherwise = p (insert n s) (f n)
    f = sum . fmap ((^ 2) . toInteger . digitToInt) . show

main :: IO ()
main = mapM_ print $ take 8 $ filter isHappy [1 ..]
Output:
1
7
10
13
19
23
28
31

We can create a cache for small numbers to greatly speed up the process:

import Data.Array (Array, (!), listArray)

happy :: Int -> Bool
happy x
  | xx <= 150 = seen ! xx
  | otherwise = happy xx
  where
    xx = dsum x
    seen :: Array Int Bool
    seen =
      listArray (1, 150) $ True : False : False : False : (happy <$> [5 .. 150])
    dsum n
      | n < 10 = n * n
      | otherwise =
        let (q, r) = n `divMod` 10
        in r * r + dsum q

main :: IO ()
main = print $ sum $ take 10000 $ filter happy [1 ..]
Output:
327604323

Icon and Unicon

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

Usage and Output:

| happynum.exe 

The first 8 happy numbers are: 1 7 10 13 19 23 28 31

J

   8{. (#~1=+/@(*:@(,.&.":))^:(1&~:*.4&~:)^:_ "0) 1+i.100
1 7 10 13 19 23 28 31

This is a repeat while construction

 f ^: cond ^: _   input

that produces an array of 1's and 4's, which is converted to 1's and 0's forming a binary array having a 1 for a happy number. Finally the happy numbers are extracted by a binary selector.

 (binary array) # 1..100

So for easier reading the solution could be expressed as:

   cond=: 1&~: *. 4&~:     NB. not equal to 1 and not equal to 4
   sumSqrDigits=: +/@(*:@(,.&.":))

   sumSqrDigits 123        NB. test sum of squared digits
14
   8{. (#~ 1 = sumSqrDigits ^: cond ^:_ "0) 1 + i.100
1 7 10 13 19 23 28 31

Java

Works with: Java version 1.5+
Translation of: JavaScript
import java.util.HashSet;
public class Happy{
   public static boolean happy(long number){
       long m = 0;
       int digit = 0;
       HashSet<Long> cycle = new HashSet<Long>();
       while(number != 1 && cycle.add(number)){
           m = 0;
           while(number > 0){
               digit = (int)(number % 10);
               m += digit*digit;
               number /= 10;
           }
           number = m;
       }
       return number == 1;
   }

   public static void main(String[] args){
       for(long num = 1,count = 0;count<8;num++){
           if(happy(num)){
               System.out.println(num);
               count++;
           }
       }
   }
}

Output:

1
7
10
13
19
23
28
31


Java 1.8

Works with: Java version 1.8
Translation of: Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class HappyNumbers {


    public static void main(String[] args) {

        for (int current = 1, total = 0; total < 8; current++)
            if (isHappy(current)) {
                System.out.println(current);
                total++;
            }
    }


    public static boolean isHappy(int number) {
        HashSet<Integer> cycle = new HashSet<>();
        while (number != 1 && cycle.add(number)) {
            List<String> numStrList = Arrays.asList(String.valueOf(number).split(""));
            number = numStrList.stream().map(i -> Math.pow(Integer.parseInt(i), 2)).mapToInt(i -> i.intValue()).sum();
        }
        return number == 1;
    }
}

Output:

1
7
10
13
19
23
28
31

JavaScript

ES5

Iteration

function happy(number) {
    var m, digit ;
    var cycle = [] ;
 
    while(number != 1 && cycle[number] !== true) {
        cycle[number] = true ;
        m = 0 ;
        while (number > 0) {
            digit = number % 10 ;
            m += digit * digit ;
            number = (number  - digit) / 10 ;
        }
        number = m ;
    }
    return (number == 1) ;
}
 
var cnt = 8 ;
var number = 1 ;
 
while(cnt-- > 0) {
    while(!happy(number))
        number++ ;
    document.write(number + " ") ;
    number++ ;
}

Output:

1 7 10 13 19 23 28 31 

ES6

Functional composition

Translation of: Haskell
(() => {

    // isHappy :: Int -> Bool
    const isHappy = n => {
        const f = n =>
            foldl(
                (a, x) => a + raise(read(x), 2), // ^2
                0,
                splitOn('', show(n))
            ),
            p = (s, n) => n === 1 ? (
                true
            ) : member(n, s) ? (
                false
            ) : p(
                insert(n, s), f(n)
            );
        return p(new Set(), n);
    };

    // GENERIC FUNCTIONS ------------------------------------------------------

    // enumFromTo :: Int -> Int -> [Int]
    const enumFromTo = (m, n) =>
        Array.from({
            length: Math.floor(n - m) + 1
        }, (_, i) => m + i);

    // filter :: (a -> Bool) -> [a] -> [a]
    const filter = (f, xs) => xs.filter(f);

    // foldl :: (b -> a -> b) -> b -> [a] -> b
    const foldl = (f, a, xs) => xs.reduce(f, a);

    // insert :: Ord a => a -> Set a -> Set a
    const insert = (e, s) => s.add(e);

    // member :: Ord a => a -> Set a -> Bool
    const member = (e, s) => s.has(e);

    // read :: Read a => String -> a
    const read = JSON.parse;

    // show :: a -> String
    const show = x => JSON.stringify(x);

    // splitOn :: String -> String -> [String]
    const splitOn = (cs, xs) => xs.split(cs);

    // raise :: Num -> Int -> Num
    const raise = (n, e) => Math.pow(n, e);

    // take :: Int -> [a] -> [a]
    const take = (n, xs) => xs.slice(0, n);

    // TEST -------------------------------------------------------------------
    return show(
        take(8, filter(isHappy, enumFromTo(1, 50)))
    );
})()
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Or, to stop immediately at the 8th member of the series, we can preserve functional composition while using an iteratively implemented until() function:

(() => {

    // isHappy :: Int -> Bool
    const isHappy = n => {
        const f = n =>
            foldl(
                (a, x) => a + raise(read(x), 2), // ^2
                0,
                splitOn('', show(n))
            ),
            p = (s, n) => n === 1 ? (
                true
            ) : member(n, s) ? (
                false
            ) : p(
                insert(n, s), f(n)
            );
        return p(new Set(), n);
    };

    // GENERIC FUNCTIONS ------------------------------------------------------

    // filter :: (a -> Bool) -> [a] -> [a]
    const filter = (f, xs) => xs.filter(f);

    // foldl :: (b -> a -> b) -> b -> [a] -> b
    const foldl = (f, a, xs) => xs.reduce(f, a);

    // insert :: Ord a => a -> Set a -> Set a
    const insert = (e, s) => s.add(e);

    // member :: Ord a => a -> Set a -> Bool
    const member = (e, s) => s.has(e);

    // read :: Read a => String -> a
    const read = JSON.parse;

    // show :: a -> String
    const show = x => JSON.stringify(x);

    // splitOn :: String -> String -> [String]
    const splitOn = (cs, xs) => xs.split(cs);

    // raise :: Num -> Int -> Num
    const raise = (n, e) => Math.pow(n, e);

    // until :: (a -> Bool) -> (a -> a) -> a -> a
    const until = (p, f, x) => {
        let v = x;
        while (!p(v)) v = f(v);
        return v;
    };

    // TEST -------------------------------------------------------------------
    return show(
        until(
            m => m.xs.length === 8,
            m => {
                const n = m.n;
                return {
                    n: n + 1,
                    xs: isHappy(n) ? m.xs.concat(n) : m.xs
                };
            }, {
                n: 1,
                xs: []
            }
        )
        .xs
    );
})();
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

jq

Works with: jq version 1.4
def is_happy_number:
  def next: tostring | explode | map( (. - 48) | .*.) | add;
  def last(g): reduce g as $i (null; $i);
  # state: either 1 or [i, o] 
  # where o is an an object with the previously encountered numbers as keys
  def loop:
   recurse( if      . == 1 then empty    # all done
            elif .[0] == 1 then 1        # emit 1
            else (.[0]| next) as $n
            | if $n == 1 then 1
              elif .[1]|has($n|tostring) then empty
              else [$n, (.[1] + {($n|tostring):true}) ]
              end
            end );
  1 == last( [.,{}] | loop );

Emit a stream of the first n happy numbers:

# Set n to -1 to continue indefinitely:
def happy(n):
  def subtask:  # state: [i, found]
    if .[1] == n then empty
    else .[0] as $n
    | if ($n | is_happy_number) then $n, ([ $n+1, .[1]+1 ] | subtask) 
      else  (.[0] += 1) | subtask
      end
    end;
    [0,0] | subtask;

happy($n|tonumber)
Output:
$ jq --arg n 8 -n -f happy.jq
1
7
10
13
19
23
28
31

Julia

function happy(x)
	happy_ints = ref(Int)
	int_try = 1
	while length(happy_ints) < x
		n = int_try
		past = ref(Int)
		while n != 1
	        	n = sum([y^2 for y in digits(n)])
	        	contains(past,n) ? break : push!(past,n)
	    	end
		n == 1 && push!(happy_ints,int_try)
		int_try += 1
	end
	return happy_ints
end

Output

 julia> happy(8)
8-element Int32 Array:
  1
  7
 10
 13
 19
 23
 28
 31

A recursive version:

sumhappy(n) = sum(x->x^2, digits(n))

function ishappy(x, mem = [])
  x == 1?   true :
  x in mem? false :
  ishappy(sumhappy(x),[mem ; x])
end

nexthappy (x) = ishappy(x+1) ? x+1 : nexthappy(x+1)

happy(n) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
Output:
julia> show(happy(8))
[1,7,10,13,19,23,28,31,32]

Alternate, Translation of C
Faster with use of cache

Translation of: C
const CACHE = 256
buf = zeros(Int,CACHE)
buf[1] = 1
#happy(n) returns 1 if happy, 0 if not
function happy(n)
	if n < CACHE
		buf[n] > 0 && return 2-buf[n]
		buf[n] = 2
	end
	sum = 0
	nn = n
	while nn != 0
		x = nn%10
		sum += x*x
		nn = int8(nn/10)
	end
	x = happy(sum)
	n < CACHE && (buf[n] = 2-x)
	return x
end
function main()
	i = 1; counter = 1000000
	while counter > 0
		if happy(i) == 1
			counter -= 1
		end
		i += 1
	end
	return i-1
end

K

  hpy: {x@&1={~|/x=1 4}{_+/_sqr 0$'$x}//:x}

  hpy 1+!100
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100

  8#hpy 1+!100
1 7 10 13 19 23 28 31

Another implementation which is easy to follow is given below:

/ happynum.k

/ sum of squares of digits of an integer
dgtsmsqr: {d::(); (0<){d::d,x!10; x%:10}/x; +/d*d}
/ Test if an integer is a Happy number
isHappy: {s::(); while[1<x;a:(dgtsmsqr x); :[(a _in s); :0; s::s,a]; x:a];:1} / Returns 1 if Happy
/ Generate first x Happy numbers and display the list
hnum: {[x]; h::();i:1;while[(#h)<x; :[(isHappy i); h::(h,i)]; i+:1]; `0: ,"List of ", ($x), " Happy Numbers"; h}

The output of a session with this implementation is given below:

Output:
K Console - Enter \ for help

  \l happynum
  hnum 8
List of 8 Happy Numbers
1 7 10 13 19 23 28 31

Kotlin

Translation of: C#
// version 1.0.5-2

fun isHappy(n: Int): Boolean {
    val cache = mutableListOf<Int>()
    var sum = 0
    var nn = n
    var digit: Int
    while (nn != 1) {
        if (nn in cache) return false
        cache.add(nn)
        while (nn != 0) {
            digit = nn % 10
            sum += digit * digit
            nn /= 10
        }
        nn = sum
        sum = 0
    }
    return true 
}

fun main(args: Array<String>) {
    var num = 1
    val happyNums = mutableListOf<Int>()
    while (happyNums.size < 8) {
        if (isHappy(num)) happyNums.add(num)
        num++
    }
    println("First 8 happy numbers : " + happyNums.joinToString(", "))
}
Output:
First 8 happy numbers : 1, 7, 10, 13, 19, 23, 28, 31

Lasso

#!/usr/bin/lasso9
 
define isHappy(n::integer) => {
  local(past = set)
  while(#n != 1) => {
    #n = with i in string(#n)->values sum math_pow(integer(#i), 2)
    #past->contains(#n) ? return false | #past->insert(#n)
  }
  return true
}

with x in generateSeries(1, 500)
  where isHappy(#x)
  take 8
select #x

Output:

1, 7, 10, 13, 19, 23, 28, 31

Liberty BASIC

    ct = 0
    n = 0
    DO
        n = n + 1
        IF HappyN(n, sqrInt$) = 1 THEN
            ct = ct + 1
            PRINT ct, n
        END IF
    LOOP UNTIL ct = 8
END

FUNCTION HappyN(n, sqrInts$)
    n$ = Str$(n)
    sqrInts = 0
    FOR i = 1 TO Len(n$)
        sqrInts = sqrInts + Val(Mid$(n$, i, 1)) ^ 2
    NEXT i
    IF sqrInts = 1 THEN
        HappyN = 1
        EXIT FUNCTION
    END IF
    IF Instr(sqrInts$, ":";Str$(sqrInts);":") > 0 THEN
        HappyN = 0
        EXIT FUNCTION
    END IF
    sqrInts$ = sqrInts$ + Str$(sqrInts) + ":"
    HappyN = HappyN(sqrInts, sqrInts$)
END FUNCTION

Output:-

1             1
2             7
3             10
4             13
5             19
6             23
7             28
8             31

Locomotive Basic

10 mode 1:defint a-z
20 for i=1 to 100
30 i2=i
40 for l=1 to 20
50 a$=str$(i2)
60 i2=0
70 for j=1 to len(a$)
80 d=val(mid$(a$,j,1))
90 i2=i2+d*d
100 next j
110 if i2=1 then print i;"is a happy number":n=n+1:goto 150
120 if i2=4 then 150 ' cycle found
130 next l
140 ' check if we have reached 8 numbers yet
150 if n=8 then end
160 next i

to sum_of_square_digits :number
  output (apply "sum (map [[d] d*d] ` :number))
end 

to is_happy? :number [:seen []]
  output cond [
    [ [:number = 1] "true ]
    [ [member? :number :seen] "false ]
    [ else (is_happy? (sum_of_square_digits :number) (lput :number :seen))]
  ]
end

to n_happy :count [:start 1] [:result []]
  output cond [
    [ [:count <= 0] :result ]
    [ [is_happy? :start]
      (n_happy (:count-1) (:start+1) (lput :start :result)) ]
    [ else
      (n_happy :count (:start+1) :result) ]
  ]
end

print n_happy 8
bye

Output:

1 7 10 13 19 23 28 31

LOLCODE

Works with: lci 0.10.3
OBTW 
  Happy Numbers Rosetta Code task in LOLCODE
  Requires 1.3 for BUKKIT availability
TLDR
HAI 1.3
CAN HAS STDIO?

BTW Simple list implementation.  
BTW Used for the list of numbers already seen in IZHAPPY

BTW Create a list
HOW IZ I MAEKLIST 
  I HAS A LIST ITZ A BUKKIT
  LIST HAS A LENGTH ITZ 0
  FOUND YR LIST
IF U SAY SO

BTW Append an item to list
HOW IZ I PUTIN YR LIST AN YR ITEM
  LIST HAS A SRS LIST'Z LENGTH ITZ ITEM
  LIST'Z LENGTH R SUM OF LIST'Z LENGTH AN 1
IF U SAY SO

BTW Check for presence of an item in the list
HOW IZ I DUZLISTHAS YR HAYSTACK AN YR NEEDLE
  IM IN YR BARN UPPIN YR INDEX WILE DIFFRINT INDEX AN HAYSTACK'Z LENGTH
    I HAS A ITEM ITZ HAYSTACK'Z SRS INDEX
    BOTH SAEM ITEM AN NEEDLE
    O RLY?
      YA RLY
        FOUND YR WIN
    OIC
  IM OUTTA YR BARN
  FOUND YR FAIL
IF U SAY SO

BTW Calculate the next number using the happy formula
HOW IZ I HAPPYSTEP YR NUM
  I HAS A NEXT ITZ 0
  IM IN YR LOOP 
    BOTH SAEM NUM AN 0
    O RLY?
      YA RLY
        GTFO
    OIC
    I HAS A DIGIT ITZ MOD OF NUM AN 10
    NUM R QUOSHUNT OF NUM AN 10
    I HAS A SQUARE ITZ PRODUKT OF DIGIT AN DIGIT
    NEXT R SUM OF NEXT AN SQUARE
  IM OUTTA YR LOOP
  FOUND YR NEXT
IF U SAY SO

BTW Check to see if a number is happy
HOW IZ I IZHAPPY YR NUM
  I HAS A SEENIT ITZ I IZ MAEKLIST MKAY
  IM IN YR LOOP 
    BOTH SAEM NUM AN 1
    O RLY?
      YA RLY   
        FOUND YR WIN
    OIC
    I IZ DUZLISTHAS YR SEENIT AN YR NUM MKAY
    O RLY?
      YA RLY
        FOUND YR FAIL
    OIC
    I IZ PUTIN YR SEENIT AN YR NUM MKAY
    NUM R I IZ HAPPYSTEP YR NUM MKAY
  IM OUTTA YR LOOP
IF U SAY SO

BTW Print out the first 8 happy numbers
I HAS A KOUNT ITZ 0
IM IN YR LOOP UPPIN YR NUM WILE DIFFRINT KOUNT AN 8
  I IZ IZHAPPY YR NUM MKAY
  O RLY?
    YA RLY
      KOUNT R SUM OF KOUNT AN 1
      VISIBLE NUM
  OIC
IM OUTTA YR LOOP
KTHXBYE

Output:

1
7
10
13
19
23
28
31

Lua

function digits(n)
  if n > 0 then return n % 10, digits(math.floor(n/10)) end
end
function sumsq(a, ...)
  return a and a ^ 2 + sumsq(...) or 0
end
local happy = setmetatable({true, false, false, false}, {
      __index = function(self, n)
         self[n] = self[sumsq(digits(n))]
         return self[n]
      end } )
i, j = 0, 1
repeat
   i, j = happy[j] and (print(j) or i+1) or i, j + 1
until i == 8

Output:

1
7
10
13
19
23
28
31

M2000 Interpreter

Translation of: ActionScript

Lambda Function PrintHappy has a closure another lambda function IsHappy which has a closure of another lambda function the sumOfSquares.


Function FactoryHappy {
      sumOfSquares= lambda (n) ->{
                  k$=str$(abs(n),"")
                  Sum=0
                  For i=1 to len(k$)
                        sum+=val(mid$(k$,i,1))**2
                  Next i
                  =sum
      }
      IsHappy=Lambda sumOfSquares (n) ->{
            Inventory sequence
            While n<>1 {
                  Append sequence, n
                  n=sumOfSquares(n)
                   if exist(sequence, n) then =false : Break
            }
            =True
      }
      =Lambda IsHappy ->{
                  numleft=8
                  numToTest=1
                  While numleft {
                        if ishappy(numToTest) Then {
                              Print numToTest
                              numleft--
                        }
                        numToTest++
                  }
      }
}
PrintHappy=factoryHappy()
Call PrintHappy()
Output:
 1
 7
10
13
19
23
28
31

MAD

            NORMAL MODE IS INTEGER
            BOOLEAN CYCLE
            DIMENSION CYCLE(200)
            VECTOR VALUES OUTFMT = $I2*$
            
            SEEN = 0
            I = 0
            
NEXNUM      THROUGH ZERO, FOR K=0, 1, K.G.200
ZERO        CYCLE(K) = 0B
            I = I + 1
            SUMSQR = I
CHKLP       N = SUMSQR
            SUMSQR = 0
SUMLP       DIG = N-N/10*10
            SUMSQR = SUMSQR + DIG*DIG
            N = N/10
            WHENEVER N.NE.0, TRANSFER TO SUMLP
            WHENEVER SUMSQR.E.1, TRANSFER TO HAPPY
            WHENEVER CYCLE(SUMSQR), TRANSFER TO NEXNUM
            CYCLE(SUMSQR) = 1B
            TRANSFER TO CHKLP

HAPPY       PRINT FORMAT OUTFMT,I
            SEEN = SEEN+1
            WHENEVER SEEN.L.8, TRANSFER TO NEXNUM
            
            END OF PROGRAM
Output:
 1
 7
10
13
19
23
28
31

Maple

To begin, here is a procedure to compute the sum of the squares of the digits of a positive integer. It uses the built-in procedure irem, which computes the integer remainder and, if passed a name as the optional third argument, assigns it the corresponding quotient. (In other words, it performs integer division with remainder. There is also a dual, companion procedure iquo, which returns the integer quotient and assigns the remainder to the (optional) third argument.)

SumSqDigits := proc( n :: posint )
        local s := 0;
        local m := n;
        while m <> 0 do
                s := s + irem( m, 10, 'm' )^2
        end do;
        s
end proc:

(Note that the unevaluation quotes on the third argument to irem are essential here, as that argument must be a name and, if m were passed without quotes, it would evaluate to a number.)

For example,

> SumSqDigits( 1234567890987654321 );
                                  570

We can check this by computing it another way (more directly).

> n := 1234567890987654321:
> `+`( op( map( parse, StringTools:-Explode( convert( n, 'string' ) ) )^~2) );
                                  570

The most straight-forward way to check whether a number is happy or sad seems also to be the fastest (that I could think of).

Happy? := proc( n )
        if n = 1 then
                true
        elif n = 4 then
                false
        else
                local s := SumSqDigits( n );
                while not ( s in { 1, 4 } ) do
                        s := SumSqDigits( s )
                end do;
                evalb( s = 1 )
        end if
end proc:

We can use this to determine the number of happy (H) and sad (S) numbers up to one million as follows.

> H, S := selectremove( Happy?, [seq]( 1 .. N ) ):
> nops( H ), nops( S );
                             143071, 856929

Finally, to solve the stated problem, here is a completely straight-forward routine to locate the first N happy numbers, returning them in a set.

FindHappiness := proc( N )
        local count := 0;
        local T := table();
        for local i while count < N do
                if Happy?( i ) then
                        count := 1 + count;
                        T[ count ] := i
                end if
        end do;
        {seq}( T[ i ], i = 1 .. count )
end proc:

With input equal to 8, we get

> FindHappiness( 8 );
                     {1, 7, 10, 13, 19, 23, 28, 31}

For completeness, here is an implementation of the cycle detection algorithm for recognizing happy numbers. It is much slower, however.

Happy? := proc( n :: posint )
        local a, b;
        a, b := n, SumSqDigits( n );
        while a <> b do
                a := SumSqDigits( a );
                b := (SumSqDigits@@2)( b )
        end do;
        evalb( a = 1 )
end proc:

Mathematica / Wolfram Language

Custom function HappyQ:

AddSumSquare[input_]:=Append[input,Total[IntegerDigits[Last[input]]^2]]
NestUntilRepeat[a_,f_]:=NestWhile[f,{a},!MemberQ[Most[Last[{##}]],Last[Last[{##}]]]&,All]
HappyQ[a_]:=Last[NestUntilRepeat[a,AddSumSquare]]==1

Examples for a specific number:

HappyQ[1337]
HappyQ[137]

gives back:

True
False

Example finding the first 8:

m = 8;
n = 1;
i = 0;
happynumbers = {};
While[n <= m,
 i++;
 If[HappyQ[i],
  n++;
  AppendTo[happynumbers, i]
  ]
 ]
happynumbers

gives back:

{1, 7, 10, 13, 19, 23, 28, 31}

MATLAB

Recursive version:

function findHappyNumbers
    nHappy = 0;
    k = 1;
    while nHappy < 8
        if isHappyNumber(k, [])
            fprintf('%d ', k)
            nHappy = nHappy+1;
        end
        k = k+1;
    end
    fprintf('\n')
end

function hap = isHappyNumber(k, prev)
    if k == 1
        hap = true;
    elseif ismember(k, prev)
        hap = false;
    else
        hap = isHappyNumber(sum((sprintf('%d', k)-'0').^2), [prev k]);
    end
end
Output:
1 7 10 13 19 23 28 31 

MAXScript

fn isHappyNumber n =
(
	local pastNumbers = #()
	while n != 1 do
	(
		n = n as string
		local newNumber = 0
		for i = 1 to n.count do
		(
			local digit = n[i] as integer
			newNumber += pow digit 2
		)
		n = newNumber 
		if (finditem pastNumbers n) != 0 do return false
		append pastNumbers newNumber
	)
	n == 1
)
printed = 0
for i in (for h in 1 to 500 where isHappyNumber h collect h) do
(
	if printed == 8 do exit
	print i as string
	printed += 1
	
)

Output:

1
7
10
13
19
23
28
31

Mercury

:- module happy.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module int, list, set_tree234.

main(!IO) :-
    print_line(get_n_happy_numbers(8, 1), !IO).

:- func get_n_happy_numbers(int, int) = list(int).

get_n_happy_numbers(NumToFind, N) =
    ( if NumToFind > 0 then
       ( if is_happy(N, init)
       then [N | get_n_happy_numbers(NumToFind - 1, N + 1)]
       else get_n_happy_numbers(NumToFind, N + 1)
       )
    else
       []
    ).

:- pred is_happy(int::in, set_tree234(int)::in) is semidet.

is_happy(1, _).
is_happy(N, !.Seen) :-
   not member(N, !.Seen),
   insert(N, !Seen),
   is_happy(sum_sqr_digits(N), !.Seen).

:- func sum_sqr_digits(int) = int.

sum_sqr_digits(N) =
   ( if N < 10 then sqr(N) else sqr(N mod 10) + sum_sqr_digits(N div 10) ).

:- func sqr(int) = int.

sqr(X) = X * X.
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

MiniScript

This solution uses the observation that any infinite cycle of this algorithm hits the number 89, and so that can be used to know when we've found an unhappy number.

isHappy = function(x)
    while true
        if x == 89 then return false
        sum = 0
        while x > 0
            sum = sum + (x % 10)^2
            x = floor(x / 10)
        end while
        if sum == 1 then return true
        x = sum
    end while
end function

found = []
i = 1
while found.len < 8
    if isHappy(i) then found.push i
    i = i + 1
end while
print "First 8 happy numbers: " + found
Output:
First 8 happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]

ML

mLite

(*
A happy number is defined by the following process. Starting with any positive integer, replace the number 
by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will 
stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends
 in 1 are happy numbers, while those that do not end in 1 are unhappy numbers. Display an example of your 
output here.
*)

local
	fun get_digits 
			(d, s) where (d = 0) = s
		| 	(d, s) = get_digits( d div 10, (d mod 10) :: s)
		| 	n = get_digits( n div 10, [n mod 10] )
	;
	fun mem 
			(x, []) = false
		| 	(x, a :: as) where (x = a) = true
		| 	(x, _ :: as) = mem (x, as)
in
	fun happy
			1 = "happy"
		|	n =
				let 
					val this = (fold (+,0) ` map (fn n = n ^ 2) ` get_digits n);
					val sads = [2, 4, 16, 37, 58, 89, 145, 42, 20]
				in
					if (mem (n,sads)) then
						"unhappy"
					else
						happy this
				end
end
;

foreach (fn n = (print n; print " is "; println ` happy n)) ` iota 10;

Output:

1 is happy
2 is unhappy
3 is unhappy
4 is unhappy
5 is unhappy
6 is unhappy
7 is happy
8 is unhappy
9 is unhappy
10 is happy

Modula-2

MODULE HappyNumbers;
FROM InOut IMPORT WriteCard, WriteLn;

CONST Amount = 8;
VAR seen, num: CARDINAL;

PROCEDURE SumDigitSquares(n: CARDINAL): CARDINAL;
VAR sum, digit: CARDINAL;
BEGIN
    sum := 0;
    WHILE n>0 DO
        digit := n MOD 10;
        n := n DIV 10;
        sum := sum + digit * digit;
    END;
    RETURN sum;
END SumDigitSquares;

PROCEDURE Happy(n: CARDINAL): BOOLEAN;
VAR i: CARDINAL;
    seen: ARRAY [0..255] OF BOOLEAN;
BEGIN
    FOR i := 0 TO 255 DO 
        seen[i] := FALSE; 
    END;
    REPEAT
        seen[n] := TRUE;
        n := SumDigitSquares(n);
    UNTIL seen[n];
    RETURN seen[1];
END Happy;

BEGIN
    seen := 0;
    num := 0;
    WHILE seen < Amount DO
        IF Happy(num) THEN
            INC(seen);
            WriteCard(num,2);
            WriteLn();
        END;
        INC(num);
    END;
END HappyNumbers.
Output:
 1
 7
10
13
19
23
28
31

MUMPS

ISHAPPY(N)
 ;Determines if a number N is a happy number
 ;Note that the returned strings do not have a leading digit unless it is a happy number
 IF (N'=N\1)!(N<0) QUIT "Not a positive integer"
 NEW SUM,I
 ;SUM is the sum of the square of each digit
 ;I is a loop variable
 ;SEQ is the sequence of previously checked SUMs from the original N
 ;If it isn't set already, initialize it to an empty string
 IF $DATA(SEQ)=0 NEW SEQ SET SEQ=""
 SET SUM=0
 FOR I=1:1:$LENGTH(N) DO
 .SET SUM=SUM+($EXTRACT(N,I)*$EXTRACT(N,I))
 QUIT:(SUM=1) SUM
 QUIT:$FIND(SEQ,SUM)>1 "Part of a sequence not containing 1"
 SET SEQ=SEQ_","_SUM
 QUIT $$ISHAPPY(SUM)
HAPPY(C) ;Finds the first C happy numbers
 NEW I
 ;I is a counter for what integer we're looking at
 WRITE !,"The first "_C_" happy numbers are:"
 FOR I=1:1 QUIT:C<1  SET Q=+$$ISHAPPY(I) WRITE:Q !,I SET:Q C=C-1
 KILL I
 QUIT

Output:

USER>D HAPPY^ROSETTA(8)
 
The first 8 happy numbers are:
1
7
10
13
19
23
28
31
USER>W:+$$ISHAPPY^ROSETTA(320) "Happy Number"
Happy Number
USER>W:+$$ISHAPPY^ROSETTA(321) "Happy Number"
 
USER>

NetRexx

Translation of: REXX
/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
limit	 = arg[0]                        /*get argument for  LIMIT.        */
say limit
if limit = null, limit ='' then limit=8  /*if not specified, set LIMIT to 8*/
haps	 = 0                             /*count of happy numbers so far.  */

loop n=1 while haps < limit              /*search integers starting at one.*/
  q=n                                    /*Q may or may not be "happy".    */
  a=0
  
  loop forever                           /*see if  Q  is a happy number.   */
    if q==1 then do                      /*if  Q  is unity, then it's happy*/
      haps = haps + 1                    /*bump the count of happy numbers.*/
      say n                              /*display the number.             */
      iterate n                          /*and then keep looking for more. */
    end
    
    sum=0                                /*initialize sum to zero.         */
    
    loop j=1 for q.length                /*add the squares of the numerals.*/
      sum = sum + q.substr(j,1) ** 2
    end
    
    if a[sum] then iterate n             /*if already summed, Q is unhappy.*/
    a[sum]=1                             /*mark the sum as being found.    */
    q=sum                                /*now, lets try the  Q  sum.      */
  end
end
Output
1
7
10
13
19
23
28
31

Sample output when 100 is specified as the program's argument.

1
7
10
13
19
23
28
31
32
44
49
68
70
79
82
86
91
94
97
100
103
109
129
130
133
139
167
176
188
190
192
193
203
208
219
226
230
236
239
262
263
280
291
293
301
302
310
313
319
320
326
329
331
338
356
362
365
367
368
376
379
383
386
391
392
397
404
409
440
446
464
469
478
487
490
496
536
556
563
565
566
608
617
622
623
632
635
637
638
644
649
653
655
656
665
671
673
680
683
694

Nim

Translation of: Python
import intsets

proc happy(n: int): bool =
  var
    n = n
    past = initIntSet()
  while n != 1:
    let s = $n
    n = 0
    for c in s:
      let i = ord(c) - ord('0')
      n += i * i
    if n in past:
      return false
    past.incl(n)
  return true

for x in 0..31:
  if happy(x):
    echo x

Output:

1
7
10
13
19
23
28
31

Objeck

use IO;
use Structure;

bundle Default {
  class HappyNumbers {
    function : native : IsHappy(n : Int) ~ Bool {
      cache := IntVector->New();
        sum := 0;
        while(n <> 1) {
          if(cache->Has(n)) {
            return false;
          };
              
          cache->AddBack(n);
          while(n <> 0) {
            digit := n % 10;
            sum += (digit * digit);
            n /= 10;
          };
          
          n := sum;
          sum := 0;
        };

        return true;            
      }

      function : Main(args : String[]) ~ Nil {
        num := 1;
        happynums := IntVector->New();

        while(happynums->Size() < 8) {
          if(IsHappy(num)) {
            happynums->AddBack(num);
        };
        
        num += 1;
      };
          
      Console->Print("First 8 happy numbers: ");
      each(i : happynums) {
        Console->Print(happynums->Get(i))->Print(",");
      };
      Console->PrintLine("");
    }
  }
}

output:

First 8 happy numbers: 1,7,10,13,19,23,28,31,

OCaml

Using Floyd's cycle-finding algorithm.

open Num

let step =
	let rec aux s n =
	if n =/ Int 0 then s else
		let q = quo_num n (Int 10)
		and r = mod_num n (Int 10)
		in aux (s +/ (r */ r)) q
	in aux (Int 0) ;;

let happy n =
	let rec aux x y =
		if x =/ y then x else aux (step x) (step (step y))
	in (aux n (step n)) =/ Int 1 ;;

let first n =
	let rec aux v x n =
		if n = 0 then v else
			if happy x
			then aux (x::v) (x +/ Int 1) (n - 1)
			else aux v (x +/ Int 1) n
	in aux [ ] (Int 1) n ;;

List.iter print_endline (
	List.rev_map string_of_num (first 8)) ;;

Output:

$ ocaml nums.cma happy_numbers.ml
1
7
10
13
19
23
28
31

Oforth

: isHappy(n)
| cycle |
   ListBuffer new ->cycle
 
   while(n 1 <>) [
      cycle include(n) ifTrue: [ false return ]
      cycle add(n)
      0 n asString apply(#[ asDigit sq + ]) ->n
      ]
   true ;
 
: happyNum(N)
| numbers |
   ListBuffer new ->numbers
   1 while(numbers size N <>) [ dup isHappy ifTrue: [ dup numbers add ] 1+ ]
   numbers println ;

Output:

>happyNum(8)
[1, 7, 10, 13, 19, 23, 28, 31]

ooRexx

count = 0
say "First 8 happy numbers are:"
loop i = 1 while count < 8
    if happyNumber(i) then do
        count += 1
        say i
    end
end

::routine happyNumber
  use strict arg number

  -- use to trace previous cycle results
  previous = .set~new
  loop forever
      -- stop when we hit the target
      if number = 1 then return .true
      -- stop as soon as we start cycling
      if previous[number] \== .nil then return .false
      previous~put(number)
      next = 0
      -- loop over all of the digits
      loop digit over number~makearray('')
          next += digit * digit
      end
      -- and repeat the cycle
      number = next
  end
First 8 happy numbers are:
1
7
10
13
19
23
28
31

Oz

functor
import
  System
define
  fun {IsHappy N}
     {IsHappy2 N nil}
  end
 
  fun {IsHappy2 N Seen}
     if     N == 1          then true
     elseif {Member N Seen} then false
     else
	Next = {Sum {Map {Digits N} Square}}
     in
	{IsHappy2 Next N|Seen}
     end
  end

  fun {Sum Xs}
     {FoldL Xs Number.'+' 0}
  end
  
  fun {Digits N}
     {Map {Int.toString N} fun {$ D} D - &0 end}
  end
 
  fun {Square N} N*N end

  fun lazy {Nat I}
     I|{Nat I+1}
  end
 
  %% List.filter is eager. But we need a lazy Filter:
  fun lazy {LFilter Xs P}
     case Xs of X|Xr andthen {P X} then X|{LFilter Xr P}
     [] _|Xr then {LFilter Xr P}
     [] nil then nil
     end
  end

  HappyNumbers = {LFilter {Nat 1} IsHappy}
in
  {System.show {List.take HappyNumbers 8}}
end

Output:

[1 7 10 13 19 23 28 31]

PARI/GP

Works with: PARI/GP version 2.4.3 and above
This code uses the select() function, which was added in PARI version 2.4.2. The order of the arguments changed between versions; to use in 2.4.2 change select(function, vector) to select(vector, function).

If the number has more than three digits, the sum of the squares of its digits has fewer digits than the number itself. If the number has three digits, the sum of the squares of its digits is at most 3 * 9^2 = 243. A simple solution is to look up numbers up to 243 and calculate the sum of squares only for larger numbers.

H=[1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100,103,109,129,130,133,139,167,176,188,190,192,193,203,208,219,226,230,236,239];
isHappy(n)={
  if(n<262,
    setsearch(H,n)>0
  ,
    n=eval(Vec(Str(n)));
    isHappy(sum(i=1,#n,n[i]^2))
  )
};
select(isHappy, vector(31,i,i))

Output:

%1 = [1, 7, 10, 13, 19, 23, 28, 31]

Pascal

Program HappyNumbers (output);

uses
  Math;

function find(n: integer; cache: array of integer): boolean;
  var
    i: integer;
  begin
    find := false;
    for i := low(cache) to high(cache) do
      if cache[i] = n then
        find := true;
  end;

function is_happy(n: integer): boolean;
  var
    cache: array of integer;
    sum: integer;
  begin
    setlength(cache, 1);
    repeat
      sum := 0;
      while n > 0 do
      begin
        sum := sum + (n mod 10)**2;
        n := n div 10;
      end;
      if sum = 1 then
      begin
        is_happy := true;
        break;
      end;
      if find(sum, cache) then
      begin
        is_happy := false;
        break;
      end;
      n := sum;
      cache[high(cache)]:= sum;
      setlength(cache, length(cache)+1);
    until false;
  end;
 
var
  n, count: integer;

begin
  n := 1;
  count := 0;
  while count < 8 do
  begin
    if is_happy(n) then
    begin
      inc(count);
      write(n, ' ');
    end;
    inc(n);
  end;
  writeln;
end.

Output:

:> ./HappyNumbers
1 7 10 13 19 23 28 31

alternative for counting fast

Works with: Free Pascal

The Cache is limited to maximum value of the sum of squared digits and filled up in a blink of an eye.Even for cDigit2=1e9 takes 0.7s.Calculation of sum of squared digits is improved.Saving this SqrdSumCache speeds up tremendous. So i am able to check if the 1'000'000 th happy number is 7105849 as stated in C language.This seems to be true. Extended to 10e18 Tested with Free Pascal 3.0.4

Program HappyNumbers (output);
{$IFDEF FPC}
  {$MODE DELPHI}
  {$OPTIMIZATION ON,All}
{$ELSE}
  {$APPLICATION CONSOLE}
{$ENDIF}
//{$DEFINE Use1E9}
uses
  sysutils,//Timing
  strutils;//Numb2USA

const
  base = 10;
  HighCache = 20*(sqr(base-1));//sum of sqr digit of Uint64
{$IFDEF Use1E9}
  cDigit1  = sqr(base)*sqr(base);//must be power of base
  cDigit2  = Base*sqr(cDigit1);// 1e9
  cMaxPot  = 18;
{$ELSE}
  cDigit1  = base*sqr(base);//must be power of base
  cDigit2  = sqr(cDigit1);// 1e6
  cMaxPot  = 14;
{$ENDIF}

type
  tSumSqrDgts    = array[0..cDigit2] of word;
  tCache         = array[0..2*HighCache] of word;
  tSqrdSumCache  = array[0..2*HighCache] of Uint32;

var
  SumSqrDgts :tSumSqrDgts;
  Cache : tCache;

  SqrdSumCache1,
  SqrdSumCache2 :tSqrdSumCache;

  T1,T0 : TDateTime;
  MAX2,Max1 : NativeInt;

procedure InitSumSqrDgts;
//calc all sum of squared digits 0..cDigits2
//using already calculated values
var
  i,j,n,sq,Base1: NativeInt;
begin
  For i := 0 to Base-1 do
    SumSqrDgts[i] := i*i;
  Base1 := Base;
  n := Base;
  repeat
    For i := 1 to base-1 do
    Begin
      sq := SumSqrDgts[i];
      For j := 0 to base1-1 do
      Begin
        SumSqrDgts[n] := sq+SumSqrDgts[j];
        inc(n);
      end;
    end;
    Base1 := Base1*base;
  until Base1 >= cDigit2;
  SumSqrDgts[n] := 1;
end;

function SumSqrdDgt(n: Uint64):NativeUint;inline;
var
  r: Uint64;
begin
  result := 0;
  while n>cDigit2 do
  Begin
    r := n;
    n := n div cDigit2;
    r := r-n*cDigit2;
    inc(result,SumSqrDgts[r]);
  end;
  inc(result,SumSqrDgts[n]);
end;

procedure CalcSqrdSumCache1;
var
  Count : tSqrdSumCache;
  i,sq,result : NativeInt;
begin
  For i :=High(Count) downto 0 do
    Count[i] := 0;
  //count the manifold
  For i := cDigit1-1 downto 0 do
    inc(count[SumSqrDgts[i]]);
  For i := High(Count) downto 0 do
    if count[i] <> 0 then
    Begin
      Max1 := i;
      BREAK;
    end;
  For sq := 0 to (20-3)*81 do
  Begin
    result := 0;
    For i := Max1 downto 0 do
      inc(result,Count[i]*Cache[sq+i]);
    SqrdSumCache1[sq] := result;
  end;
end;

procedure CalcSqrdSumCache2;
var
  Count : tSqrdSumCache;
  i,sq,result : NativeInt;
begin
  For i :=High(Count) downto 0 do
    Count[i] := 0;
  For i := cDigit2-1 downto 0 do
    inc(count[SumSqrDgts[i]]);
  For i := High(Count) downto 0 do
    if count[i] <> 0 then
    Begin
      Max2 := i;
      BREAK;
    end;
  For sq := 0 to (20-6)*81 do
  Begin
    result := 0;
    For i := Max2 downto 0 do
      inc(result,Count[i]*Cache[sq+i]);
    SqrdSumCache2[sq] := result;
  end;
end;

procedure Inithappy;
var
  n,s,p : NativeUint;
Begin
  fillchar(SqrdSumCache1,SizeOf(SqrdSumCache1),#0);
  fillchar(SqrdSumCache2,SizeOf(SqrdSumCache2),#0);
  InitSumSqrDgts;
  fillChar(Cache,SizeOf(Cache),#0);

  Cache[1] := 1;
  For n := 1 to High(Cache) do
  Begin
    If Cache[n] = 0 then
    Begin
      //start a linked list
      Cache[n] := n;
      p := n;
      s := SumSqrdDgt(p);
      while Cache[s] = 0 do
      Begin
        Cache[s] := p;
        p := s;
        s := SumSqrdDgt(p);
      end;
      //mark linked list backwards as happy number
      IF Cache[s] = 1 then
      Begin
        repeat
          s := Cache[p];
          Cache[p] := 1;
          p := s;
        until s = n;
        Cache[n] := 1;
      end;
    end;
  end;
  //mark all unhappy numbers with 0
  For n := 1 to High(Cache) do
    If Cache[n] <> 1 then
      Cache[n] := 0;
   CalcSqrdSumCache1;
   CalcSqrdSumCache2;
end;

function is_happy(n: NativeUint): boolean;inline;
begin
  is_happy := Boolean(Cache[SumSqrdDgt(n)])
end;

function nthHappy(Limit: Uint64):Uint64;
var
  d,e,sE: NativeUint;
begin
  result := 0;
  d := 0;
  e := 0;
  sE := SumSqrDgts[e];
  //big steps
  while Limit >= cDigit2 do
  begin
    dec(Limit,SqrdSumCache2[SumSqrDgts[d]+sE]);
    inc(result,cDigit2);
    inc(d);
    IF d >=cDigit2 then
    Begin
      inc(e);
      sE := SumSqrdDgt(e);//SumSqrDgts[e];
      d :=0;
    end;
  end;
  //small steps
  while Limit >= cDigit1 do
  Begin
    dec(Limit,SqrdSumCache1[SumSqrdDgt(result)]);
    inc(result,cDigit1);
  end;
  //ONE BY ONE
  while Limit > 0 do
  begin
    dec(Limit,Cache[SumSqrdDgt(result)]);
    inc(result);
  end;
  result -= 1;
end;

var
  n, count :Uint64;
  Limit: NativeUint;
begin
  write('cDigit1 = ',Numb2USA(IntToStr(cDigit1)));
  writeln('  cDigit2 = ',Numb2USA(IntToStr(cDigit2)));
  T0 := now;
  Inithappy;
  writeln('Init takes ',FormatDateTime(' HH:NN:SS.ZZZ',now-T0));
  n := 1;
  count := 0;
  while count < 10  do
  begin
    if is_happy(n) then
    begin
      inc(count);
      write(n, ' ');
    end;
    inc(n);
  end;
  writeln;

  T0 := now;
  T1 := T0;
  n := 1;
  Limit := 10;
  repeat
    writeln('1E',n:2,' n.th happy number ',Numb2USA(IntToStr(nthHappy(Limit))):26,
      FormatDateTime(' HH:NN:SS.ZZZ',now-T1));
    T1 := now;
    inc(n);
    Limit := limit*10;
  until n> cMaxPot;
  writeln('Total time counting ',FormatDateTime('HH:NN:SS.ZZZ',now-T0));
end.
output
cDigit1 = 1,000  cDigit2 = 1,000,000
Init takes  00:00:00.004
1 7 10 13 19 23 28 31 32 44
1E 1 n.th happy number                         44 00:00:00.000
1E 2 n.th happy number                        694 00:00:00.000
1E 3 n.th happy number                      6,899 00:00:00.000
1E 4 n.th happy number                     67,169 00:00:00.000
1E 5 n.th happy number                    692,961 00:00:00.000
1E 6 n.th happy number                  7,105,849 00:00:00.000
1E 7 n.th happy number                 71,313,350 00:00:00.000
1E 8 n.th happy number                698,739,425 00:00:00.000
1E 9 n.th happy number              6,788,052,776 00:00:00.000
1E10 n.th happy number             66,305,148,869 00:00:00.000
1E11 n.th happy number            660,861,957,662 00:00:00.001
1E12 n.th happy number          6,745,877,698,967 00:00:00.008
1E13 n.th happy number         70,538,879,028,725 00:00:00.059
1E14 n.th happy number        744,083,563,164,178 00:00:00.612
Total time counting 00:00:00.680

real    0m0,685s

cDigit1 = 10,000  cDigit2 = 1,000,000,000
Init takes  00:00:02.848
1 7 10 13 19 23 28 31 32 44
1E 1 n.th happy number                         44 00:00:00.000
1E 2 n.th happy number                        694 00:00:00.000
1E 3 n.th happy number                      6,899 00:00:00.000
1E 4 n.th happy number                     67,169 00:00:00.000
1E 5 n.th happy number                    692,961 00:00:00.000
1E 6 n.th happy number                  7,105,849 00:00:00.000
1E 7 n.th happy number                 71,313,350 00:00:00.000
1E 8 n.th happy number                698,739,425 00:00:00.001
1E 9 n.th happy number              6,788,052,776 00:00:00.008
1E10 n.th happy number             66,305,148,869 00:00:00.010
1E11 n.th happy number            660,861,957,662 00:00:00.009
1E12 n.th happy number          6,745,877,698,967 00:00:00.008
1E13 n.th happy number         70,538,879,028,725 00:00:00.008
1E14 n.th happy number        744,083,563,164,178 00:00:00.011
1E15 n.th happy number      7,888,334,045,397,315 00:00:00.019
1E16 n.th happy number     82,440,929,809,838,249 00:00:00.079
1E17 n.th happy number    845,099,936,580,193,833 00:00:00.698
1E18 n.th happy number  8,489,964,903,498,345,213 00:00:06.920
Total time counting 00:00:07.771

real    0m10,627s

Perl

Since all recurrences end with 1 or repeat (37,58,89,145,42,20,4,16), we can do this test very quickly without having to make hashes of seen numbers.

use List::Util qw(sum);

sub ishappy {
  my $s = shift;
  while ($s > 6 && $s != 89) {
    $s = sum(map { $_*$_ } split(//,$s));
  }
  $s == 1;
}

my $n = 0;
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";
Output:
1 7 10 13 19 23 28 31

Or we can solve using only the rudimentary task knowledge as below. Note the slightly different ways of doing the digit sum and finding the first 8 numbers where ishappy(n) is true -- this shows there's more than one way to do even these small sub-tasks.

Translation of: Raku
use List::Util qw(sum);
sub is_happy {
    my ($n) = @_;
    my %seen;
    while (1) {
        $n = sum map { $_ ** 2 } split //, $n;
        return 1 if $n == 1;
        return 0 if $seen{$n}++;
    }
}

my $n;
is_happy( ++$n ) and print "$n " or redo for 1..8;
Output:
1 7 10 13 19 23 28 31

Phix

Copy of Euphoria tweaked to give a one-line output

function is_happy(integer n)
    sequence seen = {}
    while n>1 do
        seen &= n
        integer k = 0
        while n>0 do
            k += power(remainder(n,10),2)
            n = floor(n/10)
        end while
        n = k
        if find(n,seen) then
            return false
        end if
    end while
    return true
end function
 
integer n = 1
sequence s = {}
while length(s)<8 do
    if is_happy(n) then
        s &= n
    end if
    n += 1
end while
?s
Output:
{1,7,10,13,19,23,28,31}

PHP

Translation of: D
function isHappy($n) {
    while (1) {
        $total = 0;
        while ($n > 0) {
            $total += pow(($n % 10), 2);
            $n /= 10;
        }
        if ($total == 1)
            return true;
        if (array_key_exists($total, $past))
            return false;
        $n = $total;
        $past[$total] = 0;
    }
}

$i = $cnt = 0;
while ($cnt < 8) {
    if (isHappy($i)) {
        echo "$i ";
        $cnt++;
    }
    $i++;
}
1 7 10 13 19 23 28 31 

Picat

go =>
   println(happy_len(8)).

happy(N) => 
   S = [N],
   Happy = 1,
   while (Happy == 1, N > 1)
     N := sum([to_integer(I)**2 : I in N.to_string()]),
     if member(N,S) then
       Happy := 0
     else
        S := S ++ [N]
     end
   end,
   Happy == 1.

happy_len(Limit) = S => 
   S = [],
   N = 1,
   while (S.length < Limit) 
      if happy(N) then
         S := S ++ [N]
      end,
      N := N + 1
   end.
Output:
[1,7,10,13,19,23,28,31]

PicoLisp

(de happy? (N)
   (let Seen NIL
      (loop
         (T (= N 1) T)
         (T (member N Seen))
         (setq N
            (sum '((C) (** (format C) 2))
               (chop (push 'Seen N)) ) ) ) ) )

(let H 0
   (do 8
      (until (happy? (inc 'H)))
      (printsp H) ) )

Output:

1 7 10 13 19 23 28 31

PILOT

C :max=8
  :n=0
  :i=0
*test
U :*happy
T (a=1):#n
C (a=1):i=i+1
C :n=n+1
J (i<max):*test
E :

*happy
C :a=n
  :x=n
U :*sumsq
C :b=s
*loop
C :x=a
U :*sumsq
C :a=s
C :x=b
U :*sumsq
C :x=s
U :*sumsq
C :b=s
J (a<>b):*loop
E :

*sumsq
C :s=0
*digit
C :y=x/10
  :z=x-y*10
  :s=s+z*#z
  :x=y
J (x):*digit
E :
Output:
1
7
10
13
19
23
28
31

PL/I

test: proc options (main); /* 19 November 2011 */
   declare (i, j, n, m, nh initial (0) ) fixed binary (31);

main_loop:
   do j = 1 to 100;
      n = j;
      do i = 1 to 100;
         m = 0;
         /* Form the sum of squares of the digits. */
         do until (n = 0);
            m = m + mod(n, 10)**2;
            n = n/10;
         end;
         if m = 1 then
            do;
               put skip list (j || ' is a happy number');
               nh = nh + 1;
               if nh = 8 then return;
               iterate main_loop;
            end;
         n = m; /* Replace n with the new number formed from digits. */
      end;
   end;
end test;

OUTPUT:

             1 is a happy number
             7 is a happy number 
            10 is a happy number 
            13 is a happy number 
            19 is a happy number 
            23 is a happy number 
            28 is a happy number 
            31 is a happy number 

PL/M

100H:

/* FIND SUM OF SQUARE OF DIGITS OF NUMBER */
DIGIT$SQUARE: PROCEDURE (N) BYTE;
    DECLARE (N, T, D) BYTE;
    T = 0;
    DO WHILE N > 0;
        D = N MOD 10;
        T = T + D * D;
        N = N / 10;
    END;
    RETURN T;
END DIGIT$SQUARE;

/* CHECK IF NUMBER IS HAPPY */
HAPPY: PROCEDURE (N) BYTE;
    DECLARE (N, I) BYTE;
    DECLARE FLAG (256) BYTE;
    
    DO I=0 TO 255;
        FLAG(I) = 0;
    END;
    
    DO WHILE NOT FLAG(N);
        FLAG(N) = 1;
        N = DIGIT$SQUARE(N);
    END;
    
    RETURN N = 1;
END HAPPY;

/* CP/M BDOS CALL */
BDOS: PROCEDURE (FN, ARG);
    DECLARE FN BYTE, ARG ADDRESS;
    GO TO 5;
END BDOS;

/* PRINT STRING */
PRINT: PROCEDURE (STR);
    DECLARE STR ADDRESS;
    CALL BDOS(9, STR);
END PRINT;

/* PRINT NUMBER */
PRINT$NUMBER: PROCEDURE (N);
    DECLARE S (6) BYTE INITIAL ('...',13,10,'$');
    DECLARE P ADDRESS;
    DECLARE (N, C BASED P) BYTE;
    P = .S(3);
DIGIT:
    P = P - 1;
    C = (N MOD 10) + '0';
    N = N / 10;
    IF N > 0 THEN GO TO DIGIT;
    CALL PRINT(P);
END PRINT$NUMBER;

/* FIND FIRST 8 HAPPY NUMBERS */
DECLARE SEEN BYTE INITIAL (0);
DECLARE N BYTE INITIAL (1);

DO WHILE SEEN < 8;
    IF HAPPY(N) THEN DO;
        CALL PRINT$NUMBER(N);
        SEEN = SEEN + 1;
    END;
    N = N + 1;
END;

CALL BDOS(0,0);
EOF
Output:
1
7
10
13
19
23
28
31

Potion

sqr = (n): n * n.

isHappy = (n) :
   loop :
      if (n == 1): return true.
      if (n == 4): return false.
      sum = 0
      n = n string
      n length times (i): sum = sum + sqr(n(i) number integer).
      n = sum
   .
.

firstEight = ()
i = 0
while (firstEight length < 8) :
   i++
   if (isHappy(i)): firstEight append(i).
.
firstEight string print

PowerShell

function happy([int] $n) {
    $a=@()
    for($i=2;$a.count -lt $n;$i++) {
        $sum=$i
        $hist=@{}
        while( $hist[$sum] -eq $null ) {
            if($sum -eq 1) {
                $a+=$i
            }
            $hist[$sum]=$sum
            $sum2=0
            foreach($j in $sum.ToString().ToCharArray()) {
                $k=([int]$j)-0x30
                $sum2+=$k*$k
            }
            $sum=$sum2
        } 
    }
    $a -join ','
}

Output :

happy(8)
7,10,13,19,23,28,31,32

Prolog

Works with: SWI-Prolog
happy_numbers(L, Nb) :-
    % creation of the list
    length(L, Nb),
    % Process of this list
    get_happy_number(L, 1).


% the game is over
get_happy_number([], _).

% querying the newt happy_number
get_happy_number([H | T], N) :-
     N1 is N+1,
    (is_happy_number(N) -> 
        H = N, 
        get_happy_number(T, N1); 
        get_happy_number([H | T], N1)).

% we must memorized the numbers reached 
is_happy_number(N) :-
    is_happy_number(N, [N]).

% a number is happy when we get 1
is_happy_number(N, _L) :-
    get_next_number(N, 1), !.

% or when this number is not already reached !
is_happy_number(N, L) :-
    get_next_number(N, NN),
    \+member(NN, L),
    is_happy_number(NN, [NN | L]).

% Process of the next number from N
get_next_number(N, NewN) :-
    get_list_digits(N, LD),
    maplist(square, LD, L),
    sumlist(L, NewN).

get_list_digits(N, LD) :-
	number_chars(N, LCD),
	maplist(number_chars_, LD, LCD).

number_chars_(D, CD) :-
	number_chars(D, [CD]).

square(N, SN) :-
	SN is N * N.

Output :

 ?- happy_numbers(L, 8).
L = [1,7,10,13,19,23,28,31].

PureBasic

#ToFind=8
#MaxTests=100
#True = 1: #False = 0
Declare is_happy(n)

If OpenConsole()
  Define i=1,Happy
  Repeat
    If is_happy(i)
      Happy+1
      PrintN("#"+Str(Happy)+RSet(Str(i),3))
    EndIf
    i+1
  Until Happy>=#ToFind
  ;
  Print(#CRLF$+#CRLF$+"Press ENTER to exit"): Input()
  CloseConsole()
EndIf

Procedure is_happy(n)
  Protected i,j=n,dig,sum
  Repeat
    sum=0
    While j
      dig=j%10
      j/10
      sum+dig*dig
    Wend
    If sum=1: ProcedureReturn #True: EndIf  
    j=sum
    i+1
  Until i>#MaxTests
  ProcedureReturn #False
EndProcedure

Sample output:

#1  1
#2  7
#3 10
#4 13
#5 19
#6 23
#7 28
#8 31

Python

Procedural

>>> def happy(n):
    past = set()			
    while n != 1:
        n = sum(int(i)**2 for i in str(n))
        if n in past:
            return False
        past.add(n)
    return True

>>> [x for x in xrange(500) if happy(x)][:8]
[1, 7, 10, 13, 19, 23, 28, 31]

Composition of pure functions

Drawing 8 terms from a non finite stream, rather than assuming prior knowledge of the finite sample size required:

'''Happy numbers'''

from itertools import islice


# main :: IO ()
def main():
    '''Test'''
    print(
        take(8)(
            happyNumbers()
        )
    )


# happyNumbers :: Gen [Int]
def happyNumbers():
    '''Generator :: non-finite stream of happy numbers.'''
    x = 1
    while True:
        x = until(isHappy)(succ)(x)
        yield x
        x = succ(x)


# isHappy :: Int -> Bool
def isHappy(n):
    '''Happy number sequence starting at n reaches 1 ?'''
    seen = set()

    # p :: Int -> Bool
    def p(x):
        if 1 == x or x in seen:
            return True
        else:
            seen.add(x)
            return False

    # f :: Int -> Int
    def f(x):
        return sum(int(d)**2 for d in str(x))

    return 1 == until(p)(f)(n)


# GENERIC -------------------------------------------------

# succ :: Int -> Int
def succ(x):
    '''The successor of an integer.'''
    return 1 + x


# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
    '''The prefix of xs of length n,
       or xs itself if n > length xs.'''
    return lambda xs: (
        xs[0:n]
        if isinstance(xs, list)
        else list(islice(xs, n))
    )


# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
    '''The result of repeatedly applying f until p holds.
       The initial seed value is x.'''
    def go(f, x):
        v = x
        while not p(v):
            v = f(v)
        return v
    return lambda f: lambda x: go(f, x)


if __name__ == '__main__':
    main()
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Quackery

  [ 0 swap
    [ 10 /mod 2 **
      rot + swap
      dup 0 = until ]
    drop ]                  is digitsquare ( n --> n )

  [ [ digitsquare
      dup  1 != while
      dup 42 != while
      again ]
    1 = ]                   is happy       ( n --> b )

  [ [] 1               
    [ dip 
      [ 2dup size > ]
      swap while
      dup happy if
        [ tuck join swap ]
      1+ again ] 
    drop nip ]             is happies      ( n --> [ )

  8 happies echo
Output:
[ 1 7 10 13 19 23 28 31 ]

R

is.happy <- function(n)
{
   stopifnot(is.numeric(n) && length(n)==1)
   getdigits <- function(n)
   {
      as.integer(unlist(strsplit(as.character(n), "")))
   }
   digits <- getdigits(n)
   previous <- c()
   repeat
   {
      sumsq <- sum(digits^2, na.rm=TRUE)
      if(sumsq==1L)
      {
         happy <- TRUE
         break      
      } else if(sumsq %in% previous)
      {
         happy <- FALSE
         attr(happy, "cycle") <- previous
         break
      } else
      {
         previous <- c(previous, sumsq)
         digits <- getdigits(sumsq)
      }
   }
   happy
}

Example usage

is.happy(2)
[1] FALSE
attr(,"cycle")
[1]   4  16  37  58  89 145  42  20
#Find happy numbers between 1 and 50
which(apply(rbind(1:50), 2, is.happy))
1  7 10 13 19 23 28 31 32 44 49
#Find the first 8 happy numbers
happies <- c()
i <- 1L
while(length(happies) < 8L)
{
   if(is.happy(i)) happies <- c(happies, i)
   i <- i + 1L
}
happies
1  7 10 13 19 23 28 31

Racket

#lang racket
(define (sum-of-squared-digits number (result 0))
  (if (zero? number)
      result
      (sum-of-squared-digits (quotient number 10)
                             (+ result (expt (remainder number 10) 2)))))

(define (happy-number? number (seen null))
  (define next (sum-of-squared-digits number))
  (cond ((= 1 next)
         #t)
        ((memq next seen)
         #f)
        (else
         (happy-number? next (cons number seen)))))

(define (get-happys max)
  (for/list ((x (in-range max))
             #:when (happy-number? x))
    x))

(display (take (get-happys 100) 8)) ;displays (1 7 10 13 19 23 28 31)

Raku

(formerly Perl 6)

Works with: rakudo version 2015-09-13
sub happy (Int $n is copy --> Bool) {
  loop {
      state %seen;
      $n = [+] $n.comb.map: { $_ ** 2 }
      return True  if $n == 1;
      return False if %seen{$n}++;
  }
}

say join ' ', grep(&happy, 1 .. *)[^8];
Output:
1 7 10 13 19 23 28 31

Here's another approach that uses a different set of tricks including lazy lists, gather/take, repeat-until, and the cross metaoperator X.

my @happy = lazy gather for 1..* -> $number {
    my %stopper = 1 => 1;
    my $n = $number;
    repeat until %stopper{$n}++ {
        $n = [+] $n.comb X** 2;
    }
    take $number if $n == 1;
}

say ~@happy[^8];

Output is the same as above.

Here is a version using a subset and an anonymous recursion (we cheat a little bit by using the knowledge that 7 is the second happy number):

subset Happy of Int where sub ($n) {
    $n == 1 ?? True  !!
    $n < 7  ?? False !!
    &?ROUTINE([+] $n.comb »**» 2);
}
 
say (grep Happy, 1 .. *)[^8];

Again, output is the same as above. It is not clear whether this version returns in finite time for any integer, though.

There's more than one way to do it...

Relation

function happy(x)
set y = x
set lasty = 0
set found = " "
while y !=  1 and not (found regex "\s".y."\s") 
set found = found . y . " "
set m = 0
while y  > 0
set digit = y mod 10
set m = m + digit * digit
set y = (y - digit) / 10
end while
set y = format(m,"%1d")
end while
set found = found . y . " "
if y = 1 
set result = 1
else
set result = 0
end if
end function

set c = 0
set i = 1
while c < 8 and i < 100
if happy(i)
echo i
set c = c + 1
end if
set i = i + 1
end while
1
7
10
13
19
23
28
31

REXX

unoptimized

/*REXX program  computes  and  displays  a  specified  amount  of   happy   numbers.    */
parse arg limit .                                /*obtain optional argument from the CL.*/
if limit=='' | limit==","  then limit=8          /*Not specified?  Then use the default.*/
haps=0                                           /*count of the happy numbers  (so far).*/

  do n=1  while haps<limit;    @.=0;  q=n        /*search the integers starting at unity*/
         do  until q==1                          /*determine if   Q   is a happy number.*/
         s=0                                     /*prepare to add squares of digits.    */
                 do j=1  for length(q)           /*sum the squares of the decimal digits*/
                 s=s + substr(q, j, 1) **2       /*add the square  of  a  decimal digit.*/
                 end   /*j*/

         if @.s  then iterate n                  /*if already summed,   Q   is unhappy. */
         @.s=1;  q=s                             /*mark the sum as found;   try  Q  sum.*/
         end   /*until*/
  say n                                          /*display the number    (N  is happy). */
  haps=haps+1                                    /*bump the  count  of  happy numbers.  */
  end          /*n*/
                                                 /*stick a fork in it,  we're all done. */
output   when using the input of:     8
1
7
10
13
19
23
28
31

optimized, vertical list

This REXX code uses additional memorization (by keeping track of happy and unhappy numbers),
it's about   2 1/2   times faster than the unoptimized version.

This REXX version also accepts a   range   of happy numbers to be shown,   that is,
it can show the 2000th through the 2032nd (inclusive) happy numbers   (as shown below).

/*REXX program   computes  and  displays   a specified  range  of   happy   numbers.    */
parse arg L H .                                  /*obtain optional arguments from the CL*/
if L=='' | L==","  then L=8                      /*Not specified?  Then use the default.*/
if H=='' | H==","  then do;  H=L; L=1;  end      /*use a range for the displaying of #s.*/
            do i=0  to 9;  #.i=i**2;  end /*i*/  /*build a squared decimal digit table. */
@.=0;   @.1=1;       !.=@.;    !.2=1;    !.4=1   /*sparse array:   @≡happy,  !≡unhappy. */
haps=0                                           /*count of the happy numbers  (so far).*/

    do n=1  while  haps<H                        /*search integers starting at unity.   */
    if !.n  then iterate                         /*if  N  is unhappy, then try another. */
    q=n                                          /* [↓]    Q  is the number being tested*/
          do  until q==1;    s=0                 /*see if  Q  is a  happy number.       */
          ?=q                                    /* [↓]    ?  is destructively parsed.  */
               do length(q)                      /*parse all the    decimal digits of ? */
               parse var  ?  _  +1 ?             /*obtain a  single decimal digit  of ? */
               s=s + #._                         /*add the square of that decimal digit.*/
               end   /*length(q)*/               /* [↑]  perform the    DO    W  times. */
          if !.s  then do; !.n=1; iterate n; end /*is  S  unhappy?    Then  Q  is also. */
          if @.s  then leave                     /*Have we found a  happy  number?      */
          q=s                                    /*try the  Q  sum to see if it's happy.*/
          end   /*until*/
    @.n=1                                        /*mark      N      as a   happy number.*/
    haps=haps+1                                  /*bump the counter of the happy numbers*/
    if haps<L  then iterate                      /*don't display  if    N    is too low.*/
    say  right(n, 30)                            /*display right justified happy number.*/
    end        /*n*/
                                                 /*stick a fork in it,  we're all done. */
output   when using the input of:     2000   2032
                         13141
                         13142
                         13148
                         13158
                         13177
                         13182
                         13184
                         13185
                         13188
                         13203
                         13212
                         13214
                         13218
                         13221
                         13228
                         13230
                         13233
                         13241
                         13247
                         13248
                         13258
                         13266
                         13274
                         13281
                         13282
                         13284
                         13285
                         13299
                         13300
                         13302
                         13303
                         13305
                         13307 

optimized, horizontal list

This REXX version is identical to the optimized version,   but displays the numbers in a horizontal list.

/*REXX program   computes  and  displays   a specified  range  of   happy   numbers.    */
sw=linesize() - 1                                /*obtain the screen width  (less one). */
parse arg limit .                                /*obtain optional argument from the CL.*/
if L=='' | L==","  then L=8                      /*Not specified?  Then use the default.*/
if H=='' | H==","  then do;  H=L; L=1;  end      /*use a range for the displaying of #s.*/
            do i=0  to 9;  #.i=i**2;  end /*i*/  /*build a squared decimal digit table. */
@.=0;   @.1=1;        !.=@.;    !.2=1;    !.4=1  /*sparse array:   @≡happy,  !≡unhappy. */
haps=0                                           /*count of the happy numbers  (so far).*/
$=
    do n=1  while  haps<H                        /*search integers starting at  unity.  */
    if !.n  then iterate                         /*if  N  is unhappy, then try another. */
    q=n                                          /*(below)  Q  is the number tested.    */
          do  until q==1;          s=0           /*see if   Q  is a happy number.       */
          ?=q                                    /* [↓]    ?  is destructively PARSEd.  */
               do length(q)                      /*parse all the    decimal digits of ? */
               parse var  ?  _  +1 ?             /*obtain a  single decimal digit  of ? */
               s=s + #._                         /*add the square of that decimal digit.*/
               end   /*length(q)*/               /* [↑]  perform the   DO    W   times. */

          if !.s  then do; !.n=1; iterate n; end /*is  S  unhappy?    Then  Q  is also. */
          if @.s  then leave                     /*Have we found a  happy  number?      */
           q=s                                   /*try the  Q  sum to see if it's happy.*/
           end   /*until*/
    @.n=1                                        /*mark     N     as a   happy number.  */
    haps=haps+1                                  /*bump the count of the happy numbers. */
    if haps<L  then iterate                      /*don't display it,   N   is too low.  */
    $=$ n                                        /*add   N   to the horizontal list.    */
    if length($ n)>sw  then do                   /*if the list is too long, then split  */
                            say strip($)         /*     ··· and display what we've got. */
                            $=n                  /*Set the next line to overflow.       */
                            end                  /* [↑]  new line now contains overflow.*/
    end     /*n*/
if $\=''  then say  strip($)                     /*display any residual happy numbers.  */
                                                 /*stick a fork in it,  we're all done. */

This REXX program makes use of   linesize   REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).

Some REXXes don't have this BIF, so the   linesize.rex   REXX program is included here   ──►   LINESIZE.REX.

output   when using the input of:     1   15002

(The   linesize   for the terminal being used for this example was   200.)


(Shown at two-thirds size.)

1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139 167 176 188 190 192 193 203 208 219 226 230 236 239 262 263 280 291 293 301 302 310 313 319 320 326 329 331 338 356
362 365 367 368 376 379 383 386 391 392 397 404 409 440 446 464 469 478 487 490 496 536 556 563 565 566 608 617 622 623 632 635 637 638 644 649 653 655 656 665 671 673 680 683 694 700 709 716 736 739
748 761 763 784 790 793 802 806 818 820 833 836 847 860 863 874 881 888 899 901 904 907 910 912 913 921 923 931 932 937 940 946 964 970 973 989 998 1000 1003 1009 1029 1030 1033 1039 1067 1076 1088
1090 1092 1093 1112 1114 1115 1121 1122 1125 1128 1141 1148 1151 1152 1158 1177 1182 1184 1185 1188 1209 1211 1212 1215 1218 1221 1222 1233 1247 1251 1257 1258 1274 1275 1277 1281 1285 1288 1290 1299
1300 1303 1309 1323 1330 1332 1333 1335 1337 1339 1353 1366 1373 1390 1393 1411 1418 1427 1444 1447 1448 1457 1472 1474 1475 1478 1481 1484 1487 1511 1512 1518 1521 1527 1528 1533 1547 1557 1572 1574
1575 1578 1581 1582 1587 1599 1607 1636 1663 1666 1670 1679 1697 1706 1717 1724 1725 1727 1733 1742 1744 1745 1748 1752 1754 1755 1758 1760 1769 1771 1772 1784 1785 1796 1808 1812 1814 1815 1818 1821
1825 1828 1841 1844 1847 1851 1852 1857 1874 1875 1880 1881 1882 1888 1900 1902 1903 1920 1929 1930 1933 1959 1967 1976 1992 1995 2003 2008 2019 2026 2030 2036 2039 2062 2063 2080 2091 2093 2109 2111
2112 2115 2118 2121 2122 2133 2147 2151 2157 2158 2174 2175 2177 2181 2185 2188 2190 2199 2206 2211 2212 2221 2224 2242 2245 2254 2257 2258 2260 2275 2285 2300 2306 2309 2313 2331 2333 2338 2339 2360
2369 2383 2390 2393 2396 2417 2422 2425 2448 2452 2455 2457 2458 2471 2475 2478 2484 2485 2487 2511 2517 2518 2524 2527 2528 2542 2545 2547 2548 2554 2555 2557 2568 2571 2572 2574 2575 2581 2582 2584
2586 2602 2603 2620 2630 2639 2658 2685 2693 2714 2715 2717 2725 2741 2745 2748 2751 2752 2754 2755 2771 2784 2800 2811 2815 2818 2825 2833 2844 2845 2847 2851 2852 2854 2856 2865 2874 2881 2899 2901
2903 2910 2919 2930 2933 2936 2963 2989 2991 2998 3001 3002 3010 3013 3019 3020 3026 3029 3031 3038 3056 3062 3065 3067 3068 3076 3079 3083 3086 3091 3092 3097 3100 3103 3109 3123 3130 3132 3133 3135
3137 3139 3153 3166 3173 3190 3193 3200 3206 3209 3213 3231 3233 3238 3239 3260 3269 3283 3290 3293 3296 3301 3308 3310 3312 3313 3315 3317 3319 3321 3323 3328 3329 3331 3332 3338 3346 3351 3355 3356
3364 3365 3367 3371 3376 3380 3382 3383 3391 3392 3436 3456 3463 3465 3466 3506 3513 3531 3535 3536 3546 3553 3560 3563 3564 3602 3605 3607 3608 3616 3620 3629 3634 3635 3637 3643 3645 3646 3650 3653
3654 3661 3664 3667 3670 3673 3676 3680 3689 3692 3698 3706 3709 3713 3731 3736 3760 3763 3766 3779 3789 3790 3797 3798 3803 3806 3823 3830 3832 3833 3860 3869 3879 3896 3897 3901 3902 3907 3910 3913
3920 3923 3926 3931 3932 3962 3968 3970 3977 3978 3986 3987 4004 4009 4040 4046 4064 4069 4078 4087 4090 4096 4111 4118 4127 4144 4147 4148 4157 4172 4174 4175 4178 4181 4184 4187 4217 4222 4225 4248
4252 4255 4257 4258 4271 4275 4278 4284 4285 4287 4336 4356 4363 4365 4366 4400 4406 4414 4417 4418 4428 4441 4447 4449 4455 4460 4471 4474 4477 4481 4482 4494 4517 4522 4525 4527 4528 4536 4545 4552
4554 4555 4558 4563 4571 4572 4577 4582 4585 4599 4604 4609 4633 4635 4636 4640 4653 4663 4690 4708 4712 4714 4715 4718 4721 4725 4728 4741 4744 4747 4751 4752 4757 4774 4775 4780 4781 4782 4788 4807
4811 4814 4817 4824 4825 4827 4841 4842 4852 4855 4870 4871 4872 4878 4887 4888 4900 4906 4944 4959 4960 4995 5036 5056 5063 5065 5066 5111 5112 5118 5121 5127 5128 5133 5147 5157 5172 5174 5175 5178
5181 5182 5187 5199 5211 5217 5218 5224 5227 5228 5242 5245 5247 5248 5254 5255 5257 5268 5271 5272 5274 5275 5281 5282 5284 5286 5306 5313 5331 5335 5336 5346 5353 5360 5363 5364 5417 5422 5425 5427
5428 5436 5445 5452 5454 5455 5458 5463 5471 5472 5477 5482 5485 5499 5506 5517 5524 5525 5527 5533 5542 5544 5545 5548 5552 5554 5555 5558 5560 5569 5571 5572 5584 5585 5596 5603 5605 5606 5628 5630
5633 5634 5643 5650 5659 5660 5666 5682 5695 5712 5714 5715 5718 5721 5722 5724 5725 5741 5742 5747 5751 5752 5774 5781 5789 5798 5799 5811 5812 5817 5821 5822 5824 5826 5842 5845 5854 5855 5862 5871
5879 5897 5919 5949 5956 5965 5978 5979 5987 5991 5994 5997 6008 6017 6022 6023 6032 6035 6037 6038 6044 6049 6053 6055 6056 6065 6071 6073 6080 6083 6094 6107 6136 6163 6166 6170 6179 6197 6202 6203
6220 6230 6239 6258 6285 6293 6302 6305 6307 6308 6316 6320 6329 6334 6335 6337 6343 6345 6346 6350 6353 6354 6361 6364 6367 6370 6373 6376 6380 6389 6392 6398 6404 6409 6433 6435 6436 6440 6453 6463
6490 6503 6505 6506 6528 6530 6533 6534 6543 6550 6559 6560 6566 6582 6595 6605 6613 6616 6631 6634 6637 6643 6650 6656 6661 6665 6673 6701 6703 6710 6719 6730 6733 6736 6763 6789 6791 6798 6800 6803
6825 6830 6839 6852 6879 6893 6897 6899 6904 6917 6923 6932 6938 6940 6955 6971 6978 6983 6987 6989 6998 7000 7009 7016 7036 7039 7048 7061 7063 7084 7090 7093 7106 7117 7124 7125 7127 7133 7142 7144
7145 7148 7152 7154 7155 7158 7160 7169 7171 7172 7184 7185 7196 7214 7215 7217 7225 7241 7245 7248 7251 7252 7254 7255 7271 7284 7306 7309 7313 7331 7336 7360 7363 7366 7379 7389 7390 7397 7398 7408
7412 7414 7415 7418 7421 7425 7428 7441 7444 7447 7451 7452 7457 7474 7475 7480 7481 7482 7488 7512 7514 7515 7518 7521 7522 7524 7525 7541 7542 7547 7551 7552 7574 7581 7589 7598 7599 7601 7603 7610
7619 7630 7633 7636 7663 7689 7691 7698 7711 7712 7721 7739 7744 7745 7754 7788 7793 7804 7814 7815 7824 7839 7840 7841 7842 7848 7851 7859 7869 7878 7884 7887 7893 7895 7896 7900 7903 7916 7930 7937
7938 7958 7959 7961 7968 7973 7983 7985 7986 7995 8002 8006 8018 8020 8033 8036 8047 8060 8063 8074 8081 8088 8099 8108 8112 8114 8115 8118 8121 8125 8128 8141 8144 8147 8151 8152 8157 8174 8175 8180
8181 8182 8188 8200 8211 8215 8218 8225 8233 8244 8245 8247 8251 8252 8254 8256 8265 8274 8281 8299 8303 8306 8323 8330 8332 8333 8360 8369 8379 8396 8397 8407 8411 8414 8417 8424 8425 8427 8441 8442
8452 8455 8470 8471 8472 8478 8487 8488 8511 8512 8517 8521 8522 8524 8526 8542 8545 8554 8555 8562 8571 8579 8597 8600 8603 8625 8630 8639 8652 8679 8693 8697 8699 8704 8714 8715 8724 8739 8740 8741
8742 8748 8751 8759 8769 8778 8784 8787 8793 8795 8796 8801 8808 8810 8811 8812 8818 8821 8847 8848 8874 8877 8880 8881 8884 8909 8929 8936 8937 8957 8963 8967 8969 8973 8975 8976 8990 8992 8996 9001
9004 9007 9010 9012 9013 9021 9023 9031 9032 9037 9040 9046 9064 9070 9073 9089 9098 9100 9102 9103 9120 9129 9130 9133 9159 9167 9176 9192 9195 9201 9203 9210 9219 9230 9233 9236 9263 9289 9291 9298
9301 9302 9307 9310 9313 9320 9323 9326 9331 9332 9362 9368 9370 9377 9378 9386 9387 9400 9406 9444 9459 9460 9495 9519 9549 9556 9565 9578 9579 9587 9591 9594 9597 9604 9617 9623 9632 9638 9640 9655
9671 9678 9683 9687 9689 9698 9700 9703 9716 9730 9737 9738 9758 9759 9761 9768 9773 9783 9785 9786 9795 9809 9829 9836 9837 9857 9863 9867 9869 9873 9875 9876 9890 9892 9896 9908 9912 9915 9921 9928
9945 9951 9954 9957 9968 9975 9980 9982 9986 10000 10003 10009 10029 10030 10033 10039 10067 10076 10088 10090 10092 10093 10112 10114 10115 10121 10122 10125 10128 10141 10148 10151 10152 10158
10177 10182 10184 10185 10188 10209 10211 10212 10215 10218 10221 10222 10233 10247 10251 10257 10258 10274 10275 10277 10281 10285 10288 10290 10299 10300 10303 10309 10323 10330 10332 10333 10335
10337

Ring

n = 1
found = 0
 
While found < 8
    If IsHappy(n) 
          found += 1
          see string(found) + " : " + string(n) + nl
    ok
    n += 1
End
 
Func IsHappy n 
    cache = []
    While n != 1
        Add(cache,n)
        t = 0
        strn = string(n)
        for e in strn
            t += pow(number(e),2)
        next
        n = t
        If find(cache,n) Return False ok
    End
    Return True
Output:
1 : 1
2 : 7
3 : 10
4 : 13
5 : 19
6 : 23
7 : 28
8 : 31

Ruby

Works with: Ruby version 2.1
require 'set' # Set: Fast array lookup / Simple existence hash

@seen_numbers = Set.new
@happy_numbers = Set.new

def happy?(n)
  return true if n == 1 # Base case
  return @happy_numbers.include?(n) if @seen_numbers.include?(n) # Use performance cache, and stop unhappy cycles

  @seen_numbers << n
  digit_squared_sum = n.to_s.each_char.inject(0) { |sum, c| sum + c.to_i**2 } # In Rails: n.to_s.each_char.sum { c.to_i**2 }

  if happy?(digit_squared_sum)
    @happy_numbers << n
    true # Return true
  else
    false # Return false
  end
end

Helper method to produce output:

def print_happy
  happy_numbers = []

  1.step do |i|
    break if happy_numbers.length >= 8
    happy_numbers << i if happy?(i)
  end

  p happy_numbers
end

print_happy
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Alternative version

@memo = [0,1]
def happy(n)
  sum = n.to_s.chars.map{|c| c.to_i**2}.inject(:+)
  return @memo[sum] if @memo[sum]==0 or @memo[sum]==1
  @memo[sum] = 0                        # for the cycle check
  @memo[sum] = happy(sum)               # return 1:Happy number, 0:other
end

i = count = 0
while count < 8
  i += 1
  puts i or count+=1 if happy(i)==1
end

puts
for i in 99999999999900..99999999999999
  puts i if happy(i)==1
end
Output:
1
7
10
13
19
23
28
31

99999999999901
99999999999910
99999999999914
99999999999915
99999999999916
99999999999937
99999999999941
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973

Simpler Alternative

Translation of: Python
def happy?(n)
  past = []			
  until n == 1
    n = n.digits.sum { |d| d * d }
    return false if past.include? n
    past << n
  end
  true
end
 
i = count = 0
until count == 8; puts i or count += 1 if happy?(i += 1) end
puts
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }
Output:
1
7
10
13
19
23
28
31

99999999999901
99999999999910
99999999999914
99999999999915
99999999999916
99999999999937
99999999999941
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973

Run BASIC

for i = 1 to 100
 if happy(i) = 1 then 
  cnt = cnt + 1
  PRINT cnt;". ";i;" is a happy number "
  if cnt = 8 then end
 end if
next i
 
FUNCTION happy(num)
while count < 50 and happy <> 1
  num$	= str$(num)
  count	= count + 1
  happy	= 0
  for i = 1 to len(num$)
    happy = happy + val(mid$(num$,i,1)) ^ 2
  next i
  num = happy
wend
end function
1. 1 is a happy number 
2. 7 is a happy number 
3. 10 is a happy number 
4. 13 is a happy number 
5. 19 is a happy number 
6. 23 is a happy number 
7. 28 is a happy number 
8. 31 is a happy number  

Rust

In Rust, using a tortoise/hare cycle detection algorithm (generic for integer types)

#![feature(core)]

fn sumsqd(mut n: i32) -> i32 {
    let mut sq = 0;
    while n > 0 {
        let d = n % 10;
        sq += d*d;
        n /= 10
    }
    sq
}

use std::num::Int;
fn cycle<T: Int>(a: T, f: fn(T) -> T) -> T {
    let mut t = a;
    let mut h = f(a);

    while t != h {
        t = f(t);
        h = f(f(h))
    }
    t
}

fn ishappy(n: i32) -> bool {
    cycle(n, sumsqd) == 1
}

fn main() {
    let happy = std::iter::count(1, 1)
                    .filter(|&n| ishappy(n))
                    .take(8)
                    .collect::<Vec<i32>>();

    println!("{:?}", happy)
}
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Salmon

variable happy_count := 0;
outer:
iterate(x; [1...+oo])
  {
    variable seen := <<(* --> false)>>;
    variable now := x;
    while (true)
      {
        if (seen[now])
          {
            if (now == 1)
              {
                ++happy_count;
                print(x, " is happy.\n");
                if (happy_count == 8)
                    break from outer;;
              };
            break;
          };
        seen[now] := true;
        variable new := 0;
        while (now != 0)
          {
            new += (now % 10) * (now % 10);
            now /::= 10;
          };
        now := new;
      };
  };

This Salmon program produces the following output:

1 is happy.
7 is happy.
10 is happy.
13 is happy.
19 is happy.
23 is happy.
28 is happy.
31 is happy.

Scala

scala> def isHappy(n: Int) = {
     |   new Iterator[Int] {
     |   val seen = scala.collection.mutable.Set[Int]()
     |   var curr = n
     |   def next = {
     |     val res = curr
     |     curr = res.toString.map(_.asDigit).map(n => n * n).sum
     |     seen += res
     |     res
     |   }
     |   def hasNext = !seen.contains(curr)
     | }.toList.last == 1
     | }
isHappy: (n: Int)Boolean

scala> Iterator from 1 filter isHappy take 8 foreach println
1
7
10
13
19
23
28
31

Scheme

(define (number->list num)
  (do ((num num (quotient num 10))
       (lst '() (cons (remainder num 10) lst)))
    ((zero? num) lst)))

(define (happy? num)
  (let loop ((num num) (seen '()))
    (cond ((= num 1) #t)
          ((memv num seen) #f)
          (else (loop (apply + (map (lambda (x) (* x x)) (number->list num)))
                      (cons num seen))))))

(display "happy numbers:")
(let loop ((n 1) (more 8))
  (cond ((= more 0) (newline))
        ((happy? n) (display " ") (display n) (loop (+ n 1) (- more 1)))
        (else (loop (+ n 1) more))))

The output is:

happy numbers: 1 7 10 13 19 23 28 31

Scratch

Scratch is a free visual programming language. Click the link, then "See inside" to view the code.

https://scratch.mit.edu/projects/78912620/

This code will allow you to check if a positive interger (<=9999) is a happy number. It will also output a list of the first 8 happy numbers. (1 7 10 13 19 23 28 31)

Seed7

$ include "seed7_05.s7i";

const type: cacheType is hash [integer] boolean;
var cacheType: cache is cacheType.value;

const func boolean: happy (in var integer: number) is func
  result
    var boolean: isHappy is FALSE;
  local
    var bitset: cycle is bitset.value;
    var integer: newnumber is 0;
    var integer: cycleNum is 0;
  begin
    while number > 1 and number not in cycle do
      if number in cache then
        number := ord(cache[number]);
      else
        incl(cycle, number);
        newnumber := 0;
        while number > 0 do
          newnumber +:= (number rem 10) ** 2;
          number := number div 10;
        end while;
        number := newnumber;
      end if;
    end while;
    isHappy := number = 1;
    for cycleNum range cycle do
      cache @:= [cycleNum] isHappy;
    end for;
  end func;

const proc: main is func
  local
    var integer: number is 0;
  begin
    for number range 1 to 50 do
      if happy(number) then
        writeln(number);
      end if;
    end for;
  end func;

Output:

1
7
10
13
19
23
28
31
32
44
49

SequenceL

import <Utilities/Math.sl>;
import <Utilities/Conversion.sl>;

main(argv(2)) := findHappys(stringToInt(head(argv)));

findHappys(count) := findHappysHelper(count, 1, []);

findHappysHelper(count, n, happys(1)) :=
        happys when size(happys) = count
    else
        findHappysHelper(count, n + 1, happys ++ [n]) when isHappy(n)
    else
        findHappysHelper(count, n + 1, happys);

isHappy(n) := isHappyHelper(n, []);

isHappyHelper(n, cache(1)) :=
    let
        digits[i] := (n / integerPower(10, i - 1)) mod 10 
                    foreach i within 1 ... ceiling(log(10, n + 1));
        newN := sum(integerPower(digits, 2));
    in 
        false when some(n = cache)
    else
        true when n = 1
    else
        isHappyHelper(newN, cache ++ [n]);
Output:
$>happy.exe 8
[1,7,10,13,19,23,28,31]

SETL

proc is_happy(n);
  s := [n];
  while n > 1 loop
    if (n := +/[val(i)**2: i in str(n)]) in s then 
       return false;
    end if;
    s with:= n;
  end while;
  return true;
end proc;
happy := [];
n := 1;
until #happy = 8 loop
  if is_happy(n) then happy with:= n; end if;
  n +:= 1;
end loop;

print(happy);

Output:

[1 7 10 13 19 23 28 31]

Alternative version:

print([n : n in [1..100] | is_happy(n)](1..8));

Output:

[1 7 10 13 19 23 28 31]

Sidef

func happy(n) is cached {
    static seen = Hash()

    return true  if n.is_one
    return false if seen.exists(n)

    seen{n} = 1
    happy(n.digits.sum { _*_ })
}

say happy.first(8)
Output:
[1, 7, 10, 13, 19, 23, 28, 31]

Smalltalk

Works with: GNU Smalltalk
Translation of: Python

In addition to the "Python's cache mechanism", the use of a Bag assures that found e.g. the happy 190, we already have in cache also the happy 910 and 109, and so on.

Object subclass: HappyNumber [
  |cache negativeCache|
  HappyNumber class >> new [ |me|
    me := super new.
    ^ me init
  ]
  init [ cache := Set new. negativeCache := Set new. ]

  hasSad: aNum [
    ^ (negativeCache includes: (self recycle: aNum))
  ]
  hasHappy: aNum [
    ^ (cache includes: (self recycle: aNum))
  ]
  addHappy: aNum [
    cache add: (self recycle: aNum)
  ]
  addSad: aNum [
    negativeCache add: (self recycle: aNum)
  ]

  recycle: aNum [ |r n| r := Bag new.
    n := aNum.
    [ n > 0 ]
    whileTrue: [ |d|
      d := n rem: 10.
      r add: d.
      n := n // 10.
    ].
    ^r
  ]

  isHappy: aNumber [ |cycle number newnumber|
    number := aNumber.
    cycle := Set new.
    [ (number ~= 1) & ( (cycle includes: number) not ) ]
    whileTrue: [
      (self hasHappy: number)
      ifTrue: [ ^true ]
      ifFalse: [
        (self hasSad: number) ifTrue: [ ^false ].
        cycle add: number.
        newnumber := 0.
        [ number > 0 ]
        whileTrue: [ |digit|
          digit := number rem: 10.
          newnumber := newnumber + (digit * digit).
 	  number := (number - digit) // 10.
        ].
        number := newnumber.
      ]
    ].
    (number = 1)
    ifTrue: [
      cycle do: [ :e | self addHappy: e ].
      ^true
    ]
    ifFalse: [ 
      cycle do: [ :e | self addSad: e ].
      ^false 
    ]
  ]
].
|happy|
happy := HappyNumber new.

1 to: 31 do: [ :i |
  (happy isHappy: i)
  ifTrue: [ i displayNl ]
].

Output:

1
7
10
13
19
23
28
31

an alternative version is:

Works with: Smalltalk/X
|next isHappy happyNumbers|

next := 
    [:n | 
        (n printString collect:[:ch | ch digitValue squared] as:Array) sum
    ].

isHappy := 
    [:n |  | t already | 
        already := Set new.
        t := n.
        [ t == 1 or:[ (already includes:t)]] whileFalse:[
            already add:t.
            t := next value:t.
        ].
        t == 1
    ].

happyNumbers := OrderedCollection new.
try := 1.
[happyNumbers size < 8] whileTrue:[
      (isHappy value:try) ifTrue:[ happyNumbers add:try].
      try := try + 1
].
happyNumbers printCR

Output: OrderedCollection(1 7 10 13 19 23 28 31)

Swift

func isHappyNumber(var n:Int) -> Bool {
    var cycle = [Int]()
    
    while n != 1 && !cycle.contains(n) {
        cycle.append(n)
        var m = 0
        while n > 0 {
            let d = n % 10
            m += d * d
            n = (n  - d) / 10
        }
        n = m
    }
    return n == 1
}

var found = 0
var count = 0
while found != 8 {
    if isHappyNumber(count) {
        print(count)
        found++
    }
    count++
}
Output:
1
7
10
13
19
23
28
31

Tcl

using code from Sum of squares#Tcl

proc is_happy n {
    set seen [list]
    while {$n > 1 && [lsearch -exact $seen $n] == -1} {
        lappend seen $n
        set n [sum_of_squares [split $n ""]]
    }
    return [expr {$n == 1}]
}

set happy [list]
set n -1
while {[llength $happy] < 8} {
    if {[is_happy $n]} {lappend happy $n}
    incr n
}
puts "the first 8 happy numbers are: [list $happy]"
the first 8 happy numbers are: {1 7 10 13 19 23 28 31}

TUSCRIPT

$$ MODE TUSCRIPT
SECTION check
  IF (n!=1) THEN
   n = STRINGS (n,":>/:")
    LOOP/CLEAR nr=n
     square=nr*nr
     n=APPEND (n,square)
    ENDLOOP
   n=SUM(n)
   r_table=QUOTES (n)
   BUILD R_TABLE/word/EXACT chk=r_table
   IF (seq.ma.chk) THEN
    status="next"
   ELSE
    seq=APPEND (seq,n)
   ENDIF
   RELEASE r_table chk
  ELSE
    PRINT checkednr," is a happy number"
    happynrs=APPEND (happynrs,checkednr)
    status="next"
  ENDIF
ENDSECTION

happynrs=""

LOOP n=1,100
sz_happynrs=SIZE(happynrs)
IF (sz_happynrs==8) EXIT
checkednr=VALUE(n)
status=seq=""
 LOOP
  IF (status=="next") EXIT
  DO check
 ENDLOOP
ENDLOOP

Output:

1 is a happy number
7 is a happy number
10 is a happy number
13 is a happy number
19 is a happy number
23 is a happy number
28 is a happy number
31 is a happy number 

uBasic/4tH

' ************************
' MAIN
' ************************

PROC _PRINT_HAPPY(20)
END

' ************************
' END MAIN
' ************************

' ************************
' SUBS & FUNCTIONS
' ************************

' --------------------
_is_happy PARAM(1)
' --------------------
LOCAL (5)
  f@ = 100
  c@ = a@
  b@ = 0

  DO WHILE b@ < f@
    e@ = 0

    DO WHILE c@
      d@ = c@ % 10
      c@ = c@ / 10
      e@ = e@ + (d@ * d@)
    LOOP

  UNTIL e@ = 1
    c@ = e@
    b@ = b@ + 1
  LOOP

RETURN(b@ < f@)

' --------------------
_PRINT_HAPPY PARAM(1)
' --------------------
LOCAL (2)
  b@ = 1
  c@ = 0

  DO

    IF FUNC (_is_happy(b@)) THEN
       c@ = c@ + 1
       PRINT b@
    ENDIF

    b@ = b@ + 1
    UNTIL c@ + 1 > a@
  LOOP

RETURN

' ************************
' END SUBS & FUNCTIONS
' ************************

UNIX Shell

Works with: Bourne Again SHell
#!/bin/bash
function sum_of_square_digits
{
  local -i n="$1" sum=0
  while (( n )); do
    local -i d=n%10
    let sum+=d*d
    let n=n/10
  done
  echo "$sum"
}

function is_happy?
{
   local -i n="$1"
   local seen=()
   while (( n != 1 )); do
     if [ -n "${seen[$n]}" ]; then
        return 1
     fi
     seen[n]=1
     let n="$(sum_of_square_digits "$n")"
   done
   return 0
}

function first_n_happy
{
  local -i count="$1"
  local -i n
  for (( n=0; count; n+=1 )); do
  if is_happy? "$n"; then
    echo "$n"
    let count-=1
  fi
  done
  return 0
}

first_n_happy 8

Output:

1
7
10
13
19
23
28
31

Ursala

The happy function is a predicate testing whether a given number is happy, and first(p) defines a function mapping a number n to the first n positive naturals having property p.

#import std
#import nat

happy = ==1+ ^== sum:-0+ product*iip+ %np*hiNCNCS+ %nP

first "p" = ~&i&& iota; ~&lrtPX/&; leql@lrPrX->lrx ^|\~& ^/successor@l ^|T\~& "p"&& ~&iNC

#cast %nL

main = (first happy) 8

output:

<1,7,10,13,19,23,28,31>

Vala

Library: Gee
using Gee;

/* function to sum the square of the digits */
int sum(int input){
	// convert input int to string
	string input_str = input.to_string();
	int total = 0;
	// read through each character in string, square them and add to total
	for (int x = 0; x < input_str.length; x++){
		// char.digit_value converts char to the decimal value the char it represents holds
		int digit = input_str[x].digit_value();
		total += (digit * digit);
	}

	return total;
} // end sum

/* function to decide if a number is a happy number */
bool is_happy(int total){
	var past = new HashSet<int>();
	while(true){
		total = sum(total);
		if (total == 1){
			return true;}
		
		if (total in past){
			return false;}
		
		past.add(total);
	} // end while loop
} // end happy

public static void main(){
	var happynums = new ArrayList<int>();
	int x = 1;
	
	while (happynums.size < 8){
		if (is_happy(x) == true)
			happynums.add(x);
		x++;
	}
	
	foreach(int num in happynums)
		stdout.printf("%d ", num);
	stdout.printf("\n");
} // end main

The output is:

1 7 10 13 19 23 28 31

VBA

Option Explicit

Sub Test_Happy()
Dim i&, Cpt&

    For i = 1 To 100
        If Is_Happy_Number(i) Then
            Debug.Print "Is Happy : " & i
            Cpt = Cpt + 1
            If Cpt = 8 Then Exit For
        End If
    Next
End Sub

Public Function Is_Happy_Number(ByVal N As Long) As Boolean
Dim i&, Number$, Cpt&
    Is_Happy_Number = False 'default value
    Do
        Cpt = Cpt + 1       'Count Loops
        Number = CStr(N)    'conversion Long To String to be able to use Len() function
        N = 0
        For i = 1 To Len(Number)
            N = N + CInt(Mid(Number, i, 1)) ^ 2
        Next i
        'If Not N = 1 after 50 Loop ==> Number Is Not Happy
        If Cpt = 50 Then Exit Function
    Loop Until N = 1
    Is_Happy_Number = True
End Function
Output:
Is Happy : 1
Is Happy : 7
Is Happy : 10
Is Happy : 13
Is Happy : 19
Is Happy : 23
Is Happy : 28
Is Happy : 31

VBScript

count = 0
firsteigth=""
For i = 1 To 100
	If IsHappy(CInt(i)) Then
		firsteight = firsteight & i & ","
		count = count + 1
	End If
	If count = 8 Then
		Exit For
	End If
Next
WScript.Echo firsteight

Function IsHappy(n)
	IsHappy = False
	m = 0
	Do Until m = 60
		sum = 0
		For j = 1 To Len(n)
			sum = sum + (Mid(n,j,1))^2
		Next
		If sum = 1 Then
			IsHappy = True
			Exit Do
		Else
			n = sum
			m = m + 1
		End If
	Loop
End Function
Output:
1,7,10,13,19,23,28,31,

Visual Basic .NET

This version uses Linq to carry out the calculations.

Module HappyNumbers
    Sub Main()
        Dim n As Integer = 1
        Dim found As Integer = 0

        Do Until found = 8
            If IsHappy(n) Then
                found += 1
                Console.WriteLine("{0}: {1}", found, n)
            End If
            n += 1
        Loop

        Console.ReadLine()
    End Sub

    Private Function IsHappy(ByVal n As Integer)
        Dim cache As New List(Of Long)()

        Do Until n = 1
            cache.Add(n)
            n = Aggregate c In n.ToString() _
                Into Total = Sum(Int32.Parse(c) ^ 2)
            If cache.Contains(n) Then Return False
        Loop

        Return True
    End Function
End Module

The output is:

1: 1
2: 7
3: 10
4: 13
5: 19
6: 23
7: 28
8: 31

Cacheless version

Translation of: C#

Curiously, this runs in about two thirds of the time of the cacheless C# version on Tio.run.

Module Module1

    Dim sq As Integer() = {1, 4, 9, 16, 25, 36, 49, 64, 81}

    Function isOne(x As Integer) As Boolean
        While True
            If x = 89 Then Return False
            Dim t As Integer, s As Integer = 0
            Do
                t = (x Mod 10) - 1 : If t >= 0 Then s += sq(t)
                x \= 10
            Loop While x > 0
            If s = 1 Then Return True
            x = s
        End While
        Return False
    End Function

    Sub Main(ByVal args As String())
        Const Max As Integer = 10_000_000
        Dim st As DateTime = DateTime.Now
        Console.Write("---Happy Numbers---" & vbLf & "The first 8:")
        Dim i As Integer = 1, c As Integer = 0
        While c < 8
            If isOne(i) Then Console.Write("{0} {1}", If(c = 0, "", ","), i, c) : c += 1
            i += 1
        End While
        Dim m As Integer = 10
        While m <= Max
            Console.Write(vbLf & "The {0:n0}th: ", m)
            While c < m
                If isOne(i) Then c += 1
                i += 1
            End While
            Console.Write("{0:n0}", i - 1)
            m = m * 10
        End While
        Console.WriteLine(vbLf & "Computation time {0} seconds.", (DateTime.Now - st).TotalSeconds)
    End Sub
End Module
Output:
---Happy Numbers---
The first 8: 1, 7, 10, 13, 19, 23, 28, 31
The 10th: 44
The 100th: 694
The 1,000th: 6,899
The 10,000th: 67,169
The 100,000th: 692,961
The 1,000,000th: 7,105,849
The 10,000,000th: 71,313,350
Computation time 19.235551 seconds.

Vlang

Translation of: go
fn happy(h int) bool {
    mut m := map[int]bool{}
    mut n := h
    for n > 1 {
        m[n] = true
        mut x := 0
        for x, n = n, 0; x > 0; x /= 10 {
            d := x % 10
            n += d * d
        }
        if m[n] {
            return false
        }
    }
    return true
}
 
fn main() {
    for found, n := 0, 1; found < 8; n++ {
        if happy(n) {
            print("$n ")
            found++
        }
    }
    println('')
}
Output:
1 7 10 13 19 23 28 31

Wren

Translation of: Go
var happy = Fn.new { |n|
    var m = {}
    while (n > 1) {
        m[n] = true
        var x = n
        n = 0
        while (x > 0) {
            var d = x % 10
            n = n + d*d
            x = (x/10).floor
        }
        if (m[n] == true) return false // m[n] will be null if 'n' is not a key
    }
    return true
}

var found = 0
var n = 1
while (found < 8) {
    if (happy.call(n)) {
        System.write("%(n) ")
        found = found + 1
    }
    n = n + 1
}
System.print()
Output:
1 7 10 13 19 23 28 31 

XPL0

The largest possible 32-bit integer is less than 9,999,999,999. The sum of the squares of these ten digits is 10*9^2 = 810. If a cycle consisted of all the values smaller than 810, an array size of 810 would still be sufficiently large to hold them. Actually, tests show that the array only needs to hold 16 numbers.

int List(810);          \list of numbers in a cycle
int Inx;                \index for List
include c:\cxpl\codes;


func HadNum(N);         \Return 'true' if number N is in the List
int N;
int I;
[for I:= 0 to Inx-1 do
        if N = List(I) then return true;
return false;
]; \HadNum


func SqDigits(N);       \Return the sum of the squares of the digits of N
int N;
int S, D;
[S:= 0;
while N do
        [N:= N/10;
        D:= rem(0);
        S:= S + D*D;
        ];
return S;
]; \SqDigits


int N0, N, C;
[N0:= 0;                \starting number
C:= 0;                  \initialize happy (starting) number counter
repeat  N:= N0;
        Inx:= 0;        \reset List index
        loop    [N:= SqDigits(N);
                if N = 1 then                   \happy number
                        [IntOut(0, N0);  CrLf(0);
                        C:= C+1;
                        quit;
                        ];
                if HadNum(N) then quit;         \if unhappy number then quit
                List(Inx):= N;                  \if neither, add it to the List
                Inx:= Inx+1;                    \ and continue the cycle
                ];
        N0:= N0+1;                              \next starting number
until   C=8;            \done when 8 happy numbers have been found
]

Output:

1
7
10
13
19
23
28
31

Zig

const std = @import("std");
const stdout = std.io.getStdOut().outStream();

pub fn main() !void {
    try stdout.print("The first 8 happy numbers are: ", .{});
    var n: u32 = 1;
    var c: u4 = 0;
    while (c < 8) {
        if (isHappy(n)) {
            c += 1;
            try stdout.print("{} ", .{n});
        }
        n += 1;
    }
    try stdout.print("\n", .{});
}

fn isHappy(n: u32) bool {
    var t = n;
    var h = sumsq(n);
    while (t != h) {
        t = sumsq(t);
        h = sumsq(sumsq(h));
    }
    return t == 1;
}

fn sumsq(n0: u32) u32 {
    var s: u32 = 0;
    var n = n0;
    while (n > 0) : (n /= 10) {
        const m = n % 10;
        s += m * m;
    }
    return s;
}
Output:
The first 8 happy numbers are: 1 7 10 13 19 23 28 31 

zkl

Here is a function that generates a continuous stream of happy numbers. Given that there are lots of happy numbers, caching them doesn't seem like a good idea memory wise. Instead, a num of squared digits == 4 is used as a proxy for a cycle (see the Wikipedia article, there are several number that will work).

Translation of: Icon and Unicon
fcn happyNumbers{  // continously spew happy numbers
   foreach N in ([1..]){
       n:=N; while(1){
	 n=n.split().reduce(fcn(p,n){ p + n*n },0);
	 if(n==1) { vm.yield(N); break; }
	 if(n==4) break;  // unhappy cycle
      }
   }
}
h:=Utils.Generator(happyNumbers);
h.walk(8).println();
Output:
L(1,7,10,13,19,23,28,31)

Get the one million-th happy number. Nobody would call this quick.

Utils.Generator(happyNumbers).drop(0d1_000_000-1).next().println();
Output:
7105849

ZX Spectrum Basic

Translation of: Run_BASIC
10 FOR i=1 TO 100
20 GO SUB 1000
30 IF isHappy=1 THEN PRINT i;" is a happy number"
40 NEXT i
50 STOP 
1000 REM Is Happy?
1010 LET isHappy=0: LET count=0: LET num=i
1020 IF count=50 OR isHappy=1 THEN RETURN 
1030 LET n$=STR$ (num)
1040 LET count=count+1
1050 LET isHappy=0
1060 FOR j=1 TO LEN n$
1070 LET isHappy=isHappy+VAL n$(j)^2
1080 NEXT j
1090 LET num=isHappy
1100 GO TO 1020