Happy numbers: Difference between revisions

101,886 bytes added ,  2 months ago
m
→‎{{header|Uiua}}: slightly nicer algorithm
(→‎{{header|Common Lisp}}: print the list)
m (→‎{{header|Uiua}}: slightly nicer algorithm)
 
(288 intermediate revisions by more than 100 users not shown)
Line 1:
{{task|Arithmetic operations}}
From Wikipedia, the free encyclopedia:
:: A [[wp:Happy number|happy number]] is defined by the following process:
:A [[wp:Happy number|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.
 
:: 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'''.  
'''Task:''' Find and print the first 8 happy numbers.
<br>
 
:: Those numbers for which this process end in &nbsp; '''1''' &nbsp; are &nbsp; &nbsp; &nbsp; ''happy'' &nbsp; numbers, &nbsp;
See also: [[oeis:A007770|The happy numbers on OEIS]]
:: while &nbsp; those numbers &nbsp; that &nbsp; do &nbsp; <u>not</u> &nbsp; end in &nbsp; '''1''' &nbsp; are &nbsp; ''unhappy'' &nbsp; numbers.
 
 
;Task:
Find and print the first &nbsp; '''8''' &nbsp; happy numbers.
 
Display an example of your output here on this page.
 
 
;Related tasks:
* [[Iterated digits squaring]]
 
;See also:
* &nbsp; The OEIS entry: &nbsp; [[oeis:A007770|The &nbsp; &nbsp; happy numbers: &nbsp; A007770]]
* &nbsp; The OEIS entry: &nbsp; [[oeis:A031177|The unhappy numbers; &nbsp; A031177]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">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])</syntaxhighlight>
 
{{out}}
<pre>
[1, 7, 10, 13, 19, 23, 28, 31]
</pre>
 
=={{header|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.
 
<syntaxhighlight lang="8080asm">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,'$'</syntaxhighlight>
 
{{out}}
 
<pre>001
007
010
013
019
023
028
031</pre>
 
 
=={{header|8th}}==
<syntaxhighlight lang="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
</syntaxhighlight>
{{out}}
<pre>
ok> 8 .happy
1 7 10 13 19 23 28 31
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN square.digit.sum n:
PUT 0 IN sum
WHILE n>0:
PUT n mod 10 IN digit
PUT sum + digit ** 2 IN sum
PUT floor (n/10) IN n
RETURN sum
 
HOW TO REPORT happy n:
PUT {} IN seen
WHILE n not.in seen:
INSERT n IN seen
PUT square.digit.sum n IN n
REPORT n=1
 
HOW TO RETURN next.happy n:
PUT n+1 IN n
WHILE NOT happy n: PUT n+1 IN n
RETURN n
 
PUT 0 IN n
FOR i IN {1..8}:
PUT next.happy n IN n
WRITE n/</syntaxhighlight>
{{out}}
<Pre>1
7
10
13
19
23
28
31</pre>
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(include-book "arithmetic-3/top" :dir :system)
 
(defun sum-of-digit-squares (n)
Line 32 ⟶ 242:
 
(defun first-happy-nums (n)
(first-happy-nums-r n 1))</langsyntaxhighlight>
Output:
<pre>(1 7 10 13 19 23 28 31)</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Happy_numbers.png Screenshot from Atari 8-bit computer]
<pre>
1: 1
2: 7
3: 10
4: 13
5: 19
6: 23
7: 28
8: 31
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function sumOfSquares(n:uint)
{
var sum:uint = 0;
Line 78 ⟶ 353:
}
}
printHappy();</langsyntaxhighlight>
Sample output:
<pre>
Line 92 ⟶ 367:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Ordered_Sets;
 
Line 132 ⟶ 407:
end if;
end loop;
end Test_Happy_Digits;</langsyntaxhighlight>
Sample output:
<pre>
Line 142 ⟶ 417:
{{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}}
<langsyntaxhighlight lang="algol68">INT base10 = 10, num happy = 8;
 
PROC next = (INT in n)INT: (
Line 166 ⟶ 441:
print((i, new line))
FI
OD</langsyntaxhighlight>
Output:
<pre>
Line 177 ⟶ 452:
+28
+31
</pre>
 
=={{header|ALGOL-M}}==
<syntaxhighlight lang="algolm">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</syntaxhighlight>
{{out}}
<pre> 1
7
10
13
19
23
28
31</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % find some happy numbers: numbers whose digit-square sums become 1 %
% when repeatedly applied %
% returns true if n is happy, false otherwise %
logical procedure isHappy ( integer value n ) ;
begin
% in base ten, numbers either reach 1 or loop around a sequence %
% containing 4 (see the Wikipedia article) %
integer v, dSum, d;
v := abs n;
if v > 1 then begin
while begin
dSum := 0;
while v not = 0 do begin
d := v rem 10;
v := v div 10;
dSum := dSum + ( d * d )
end while_v_ne_0 ;
v := dSum;
v not = 1 and v not = 4
end do begin end
end if_v_ne_0 ;
v = 1
end isHappy ;
begin % find the first 8 happy numbers %
integer n, hCount;
hCount := 0;
n := 1;
while hCount < 8 do begin
if isHappy( n ) then begin
writeon( i_w := 1, s_w := 0, " ", n );
hCount := hCount + 1
end if_isHappy__n ;
n := n + 1
end while_hCount_lt_10
end
end.
</syntaxhighlight>
{{out}}
<pre>
1 7 10 13 19 23 28 31
</pre>
 
=={{header|APL}}==
 
<lang APL> ∇ HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
===Tradfn===
<syntaxhighlight lang="apl"> ∇ HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
[1] ⍝0: Happy number
[2] ⍝1: http://rosettacode.org/wiki/Happy_numbers
Line 199 ⟶ 569:
[17]
[18] ⎕←~∘0¨∆first↑bin/iroof ⍝ Show ∆first numbers, but not 0
∇</langsyntaxhighlight>
<pre>
HappyNumbers 100 8
1 7 10 13 19 23 28 31
</pre>
 
===Dfn===
<syntaxhighlight lang="apl">
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
</syntaxhighlight>
 
=={{header|AppleScript}}==
 
===Iteration===
<syntaxhighlight lang="applescript">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</syntaxhighlight>
<pre>
Result: (*1, 7, 10, 13, 19, 23, 28, 31*)
</pre>
 
===Functional composition===
{{Trans|JavaScript}}
{{Trans|Haskell}}
<syntaxhighlight lang="applescript">---------------------- 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|</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="applescript">{1, 7, 10, 13, 19, 23, 28, 31}</syntaxhighlight>
 
=={{header|Arturo}}==
 
{{trans|Nim}}
 
<syntaxhighlight lang="rebol">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
]</syntaxhighlight>
 
{{out}}
 
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Loop {
If isHappy(A_Index) {
out .= (out="" ? "" : ",") . A_Index
Line 226 ⟶ 823:
Return false
Else Return isHappy(sum, list)
}</langsyntaxhighlight>
<pre>
The first 8 happy numbers are: 1,7,10,13,19,23,28,31
</pre>
===Alternative version===
<syntaxhighlight lang="autohotkey">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
}
}</syntaxhighlight>
<pre>1 7 10 13 19 23 28 31</pre>
 
=={{Headerheader|AutoIt}}==
<langsyntaxhighlight lang="autoit">
$c = 0
$k = 0
Line 255 ⟶ 872:
EndIf
WEnd
</syntaxhighlight>
</lang>
 
<pre>
Line 271 ⟶ 888:
 
===Alternative version===
<langsyntaxhighlight lang="autoit">
$c = 0
$k = 0
Line 296 ⟶ 913:
$a.Clear
WEnd
</syntaxhighlight>
</lang>
<pre>
Saves all numbers in a list, duplicate entry indicates a loop.
Line 311 ⟶ 928:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">function is_happy(n)
{
if ( n in happy ) return 1;
Line 351 ⟶ 968:
}
}
}</langsyntaxhighlight>
Result:
<pre>1
Line 366 ⟶ 983:
Alternately, for legibility one might write:
 
<langsyntaxhighlight lang="awk">BEGIN {
for (i = 1; i < 50; ++i){
if (isHappy(i)) {
Line 393 ⟶ 1,010:
}
return tot
}</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<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
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</syntaxhighlight>
{{out}}
<pre>
THE FIRST 8 HAPPY NUMBERS
1 7 10 13 19 23 28 31
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">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</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> 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)</syntaxhighlight>
Output:
<pre> 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</pre>
 
==={{header|Commodore BASIC}}===
The array sizes here are tuned to the minimum values required to find the first 8 happy numbers in numerical order. The <tt>H</tt> and <tt>U</tt> arrays are used for memoization, so the subscripts <tt>H(</tt><i>n</i><tt>)</tt> and <tt>U(</tt><i>n</i><tt>)</tt> must exist for the highest <i>n</i> encountered. The array <tt>N</tt> must have room to hold the longest chain examined in the course of determining whether a single number is happy, which thanks to the memoization is only ten elements long.
 
<syntaxhighlight lang="gwbasic">
100 C=8:DIM H(145),U(145),N(9)
110 PRINT CHR$(147):PRINT "THE FIRST"C"HAPPY NUMBERS:":PRINT
120 H(1)=1:N=1
130 FOR C=C TO 0 STEP 0
140 : GOSUB 200
150 : IF H THEN PRINT N,:C=C-1
160 : N=N+1
170 NEXT C
180 PRINT
190 END
200 K=0:N(K)=N
210 IF H(N(K)) THEN H=1:FOR J=0 TO K:U(N(J))=0:H(N(J))=1:NEXT J:RETURN
220 IF U(N(K)) THEN H=0:RETURN
230 U(N(K))=1
240 N$=MID$(STR$(N(K)),2)
250 L=LEN(N$)
260 K=K+1:N(K)=0
270 FOR I=1 TO L
280 : D = VAL(MID$(N$,I,1))
290 : N(K) = N(K) + D * D
300 NEXT I
310 GOTO 210</syntaxhighlight>
 
{{Out}}
<pre>
THE FIRST 8 HAPPY NUMBERS:
 
1 7 10 13
19 23 28 31
 
 
READY.
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="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</syntaxhighlight>
 
{{out}}
<pre>
1 => 1
2 => 7
3 => 10
4 => 13
5 => 19
6 => 23
7 => 28
8 => 31
</pre>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb"> 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</syntaxhighlight>
Output:-
<pre>1 1
2 7
3 10
4 13
5 19
6 23
7 28
8 31
</pre>
 
==={{header|Locomotive Basic}}===
 
<syntaxhighlight lang="locobasic">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</syntaxhighlight>
 
[[File:Happy Numbers, Locomotive BASIC.png]]
 
==={{header|PureBasic}}===
<syntaxhighlight lang="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</syntaxhighlight>
Sample output:
<pre>#1 1
#2 7
#3 10
#4 13
#5 19
#6 23
#7 28
#8 31</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">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</syntaxhighlight>
<pre>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
</pre>
 
 
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="text">
' ************************
' 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
' ************************
</syntaxhighlight>
 
==={{header|VBA}}===
 
<syntaxhighlight lang="vb">
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
</syntaxhighlight>
{{Out}}
<pre>Is Happy : 1
Is Happy : 7
Is Happy : 10
Is Happy : 13
Is Happy : 19
Is Happy : 23
Is Happy : 28
Is Happy : 31</pre>
 
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
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
</syntaxhighlight>
 
{{Out}}
<pre>1,7,10,13,19,23,28,31,</pre>
 
==={{header|Visual Basic .NET}}===
This version uses Linq to carry out the calculations.
<syntaxhighlight lang="vbnet">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</syntaxhighlight>
The output is:
<pre>1: 1
2: 7
3: 10
4: 13
5: 19
6: 23
7: 28
8: 31</pre>
====Cacheless version====
{{trans|C#}}
Curiously, this runs in about two thirds of the time of the cacheless C# version on Tio.run.
<syntaxhighlight lang="vbnet">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</syntaxhighlight>
{{out}}
<pre>---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.</pre>
 
==={{header|ZX Spectrum Basic}}===
{{trans|Run_BASIC}}
<syntaxhighlight lang="zxbasic">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</syntaxhighlight>
=={{header|Batch File}}==
happy.bat
<langsyntaxhighlight lang="dos">@echo off
setlocal enableDelayedExpansion
::Define a list with 10 terms as a convenience for defining a loop
Line 477 ⟶ 1,661:
)
set /a n=sum
)</langsyntaxhighlight>
Sample usage and output
<pre>
Line 535 ⟶ 1,719:
</pre>
 
=={{header|BBC BASICBCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
{{works with|BBC BASIC for Windows}}
 
<lang bbcbasic> number% = 0
let sumdigitsq(n) =
total% = 0
n=0 -> 0, (n rem 10)*(n rem 10)+sumdigitsq(n/10)
REPEAT
number% += 1
let happy(n) = valof
IF FNhappy(number%) THEN
$( let seen = vec 255
PRINT number% " is a happy number"
for i = 0 to 255 do total%i!seen +:= 1false
$( n!seen := ENDIFtrue
UNTIL total% n := 8sumdigitsq(n)
$) repeatuntil ENDn!seen
resultis 1!seen
$)
DEF FNhappy(num%)
 
LOCAL digit&()
let start() be
DIM digit&(10)
$( let n, i = REPEAT0, 0
while n < 8 digit&() = 0do
$( if $$^digit&happy(0i) = STR$(num%)do
digit&$() AND n := 15n + 1
num% = MOD(digit& writef("%N ",i))^2 + 0.5
UNTIL num% = 1 OR num% = 4$)
= (num% i := i + 1)</lang>
$)
Output:
wrch('*N')
<pre> 1 is a happy number
$)</syntaxhighlight>
7 is a happy number
{{out}}
10 is a happy number
<pre>1 7 10 13 is19 a23 happy28 number31</pre>
19 is a happy number
23 is a happy number
28 is a happy number
31 is a happy number</pre>
 
=={{header|Bori}}==
<langsyntaxhighlight lang="bori">bool isHappy (int n)
{
ints cache;
Line 604 ⟶ 1,784:
}
puts("First 8 happy numbers : " + str.newline + happynums);
}</langsyntaxhighlight>
Output:
<pre>First 8 happy numbers :
[1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">SumSqDgt ← +´2⋆˜ •Fmt-'0'˙
Happy ← ⟨⟩{𝕨((⊑∊˜ )◶⟨∾𝕊(SumSqDgt⊢),1=⊢⟩)𝕩}⊢
8↑Happy¨⊸/↕50</syntaxhighlight>
{{out}}
<pre>⟨ 1 7 10 13 19 23 28 31 ⟩</pre>
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">include :set
 
happiness = set.new 1
Line 638 ⟶ 1,825:
p "First eight happy numbers: #{happies}"
p "Happy numbers found: #{happiness.to_array.sort}"
p "Sad numbers found: #{sadness.to_array.sort}"</langsyntaxhighlight>
Output:
<pre>First eight happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]
Line 646 ⟶ 1,833:
=={{header|C}}==
Recursively look up if digit square sum is happy.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define CACHE 256
Line 678 ⟶ 1,865:
 
return 0;
}</langsyntaxhighlight>
output<pre>1 7 10 13 19 23 28 31
The 1000000th happy number: 7105849</pre>
Without caching, using cycle detection:
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int dsum(int n)
Line 712 ⟶ 1,899:
 
return 0;
}</langsyntaxhighlight> Output is same as above, but much slower.
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">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));
}
}
}</syntaxhighlight>
<pre>
First 8 happy numbers : 1,7,10,13,19,23,28,31
</pre>
 
===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/>
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;
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);
}
}</syntaxhighlight>
{{out}}
<pre>---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.</pre>
 
=={{header|C++}}==
{{trans|Python}}
<langsyntaxhighlight lang="cpp">#include <map>
#include <set>
 
Line 751 ⟶ 2,040:
std::cout << i << std::endl;
return 0;
}</langsyntaxhighlight>
Output:
<pre>1
Line 765 ⟶ 2,054:
49</pre>
Alternative version without caching:
<langsyntaxhighlight lang="cpp">unsigned int happy_iteration(unsigned int n)
{
unsigned int result = 0;
Line 805 ⟶ 2,094:
}
std::cout << std::endl;
}</langsyntaxhighlight>
Output:
<pre>1 7 10 13 19 23 28 31 </pre>
Cycle detection in <code>is_happy()</code> above is done using [[wp:Floyd's cycle-finding algorithm|Floyd's cycle-finding algorithm]].
 
=={{header|C sharp|C#Clojure}}==
<syntaxhighlight lang="clojure">(defn happy? [n]
<lang csharp>using System;
(loop [n n, seen #{}]
using System.Collections.Generic;
(cond
using System.Linq;
(= n 1) true
using System.Text;
(seen n) false
:else
(recur (->> (str n)
(map #(Character/digit % 10))
(map #(* % %))
(reduce +))
(conj seen n)))))
 
(def happy-numbers (filter happy? (iterate inc 1)))
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;
}
 
(println (take 8 happy-numbers))</syntaxhighlight>
static void Main(string[] args)
Output:<pre>(1 7 10 13 19 23 28 31)</pre>
{
===Alternate Version (with caching)===
int num = 1;
<syntaxhighlight lang="clojure">(require '[clojure.set :refer [union]])
List<int> happynums = new List<int>();
 
(def ^{:private true} cache {:happy (atom #{}) :sad (atom #{})})
while (happynums.Count < 8)
{
if (ishappy(num))
{
happynums.Add(num);
}
num++;
}
Console.WriteLine("First 8 happy numbers : " + string.Join(",", happynums));
}
}
}</lang>
<pre>
First 8 happy numbers : 1,7,10,13,19,23,28,31
</pre>
 
(defn break-apart [n]
=={{header|Clojure}}==
(->> (str n)
<lang clojure>(defn- digit-to-num [d] (Character/digit d 10))
(map str)
(defn- square [n] (* n n))
(map #(Long/parseLong %))))
 
(defn happy?next-number [n]
(->> (break-apart n)
(loop [n n, seen #{}]
(condmap #(=* n% 1%)) true
(seenapply n+))) false
:else
(recur (reduce + (map (comp square digit-to-num) (str n)))
(conj seen n)))))
 
(defdefn happy-numbers (filter happyor-sad? (iterate inc[prev 1)))n]
(cond (or (= n 1) ((deref (:happy cache)) n)) :happy
(or ((deref (:sad cache)) n) (some #(= % n) prev)) :sad
:else :unknown))
 
(println (take 8defn happy-numbers))</lang>algo [n]
(let [get-next (fn [[prev n]] [(conj prev n) (next-number n)])
Output:<pre>(1 7 10 13 19 23 28 31)</pre>
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))</syntaxhighlight>
Same output.
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">sum_dig_sq = proc (n: int) returns (int)
sum_sq: int := 0
while n > 0 do
sum_sq := sum_sq + (n // 10) ** 2
n := n / 10
end
return (sum_sq)
end sum_dig_sq
 
is_happy = proc (n: int) returns (bool)
nn: int := sum_dig_sq(n)
while nn ~= n cand nn ~= 1 do
n := sum_dig_sq(n)
nn := sum_dig_sq(sum_dig_sq(nn))
end
return (nn = 1)
end is_happy
 
happy_numbers = iter (start, num: int) yields (int)
n: int := start
while num > 0 do
if is_happy(n) then
yield (n)
num := num-1
end
n := n+1
end
end happy_numbers
 
start_up = proc ()
po: stream := stream$primary_output()
for i: int in happy_numbers(1, 8) do
stream$putl(po, int$unparse(i))
end
end start_up </syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. HAPPY.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 CANDIDATE PIC 9(4).
03 SQSUM-IN PIC 9(4).
03 FILLER REDEFINES SQSUM-IN.
05 DIGITS PIC 9 OCCURS 4 TIMES.
03 SQUARE PIC 9(4).
03 SUM-OF-SQUARES PIC 9(4).
03 N PIC 9.
03 TORTOISE PIC 9(4).
03 HARE PIC 9(4).
88 HAPPY VALUE 1.
03 SEEN PIC 9 VALUE ZERO.
03 OUT-FMT PIC ZZZ9.
 
PROCEDURE DIVISION.
BEGIN.
PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
UNTIL SEEN IS EQUAL TO 8.
STOP RUN.
 
DISPLAY-IF-HAPPY.
PERFORM CHECK-HAPPY.
IF HAPPY,
MOVE CANDIDATE TO OUT-FMT,
DISPLAY OUT-FMT,
ADD 1 TO SEEN.
CHECK-HAPPY.
MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
PERFORM CALC-SUM-OF-SQUARES.
MOVE SUM-OF-SQUARES TO HARE.
PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
CHECK-HAPPY-STEP.
MOVE TORTOISE TO SQSUM-IN.
PERFORM CALC-SUM-OF-SQUARES.
MOVE SUM-OF-SQUARES TO TORTOISE.
MOVE HARE TO SQSUM-IN.
PERFORM CALC-SUM-OF-SQUARES.
MOVE SUM-OF-SQUARES TO SQSUM-IN.
PERFORM CALC-SUM-OF-SQUARES.
MOVE SUM-OF-SQUARES TO HARE.
CALC-SUM-OF-SQUARES.
MOVE ZERO TO SUM-OF-SQUARES.
PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
UNTIL N IS GREATER THAN 4.
ADD-DIGIT-SQUARE.
MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
ADD SQUARE TO SUM-OF-SQUARES.</syntaxhighlight>
{{out}}
<pre> 1
7
10
13
19
23
28
31</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">happy = (n) ->
seen = {}
while true
Line 903 ⟶ 2,290:
console.log i
cnt += 1
i += 1</langsyntaxhighlight>
output
<pre>
Line 918 ⟶ 2,305:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun sqr (n)
(* n n))
 
(defun sum-of-sqr-dgts (n)
(loop for i = n then (floor i 10)
(if (zerop n)
0 while (plusp i)
(+ sum (sqr (mod ni 10))))
(sum-of-sqr-dgts (floor n 10)))))
(defun happy-p (n &optional cache)
Line 940 ⟶ 2,326:
(print (happys))
</syntaxhighlight>
</lang>
 
Output:<pre>(1 7 10 13 19 23 28 31)</pre>
 
=={{header|DCowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
<lang d>import std.stdio, std.algorithm, std.range;
 
sub sumDigitSquare(n: uint8): (s: uint8) is
bool isHappy(int n) pure nothrow {
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;</syntaxhighlight>
 
{{out}}
 
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="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) }</syntaxhighlight>
{{out}}
<pre>
1
7
10
13
19
23
28
31
 
99999999999901
99999999999910
99999999999914
99999999999915
99999999999916
99999999999937
99999999999941
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973</pre>
 
=={{header|D}}==
<syntaxhighlight lang="d">bool isHappy(int n) pure nothrow {
int[int] past;
 
Line 966 ⟶ 2,441:
 
void main() {
import std.stdio, std.algorithm, std.range;
int.max.iota().filter!isHappy().take(8).writeln();
 
}</lang>
int.max.iota.filter!isHappy.take(8).writeln;
}</syntaxhighlight>
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
===Alternative versionVersion===
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.conv, std.string;
Same output:
<lang d>import std.stdio, std.algorithm, std.range, std.conv;
 
bool isHappy(int n) /*pure nothrow*/ {
int[int] seen;
 
while (true) {
constimmutable t = n.text().representation.map!q{(a - '0') ^^ 2}().reduce!q{a + b}()sum;
if (t == 1)
return true;
Line 989 ⟶ 2,465:
 
void main() {
int.max.iota().filter!isHappy().take(8).writeln();
}</langsyntaxhighlight>
Same output.
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">main() {
HashMap<int,bool> happy=new HashMap<int,bool>();
happy[1]=true;
Line 1,027 ⟶ 2,504:
i++;
}
}</langsyntaxhighlight>
 
=={{header|dc}}==
<langsyntaxhighlight lang="dc">[lcI~rscd*+lc0<H]sH
[0rsclHxd4<h]sh
[lIp]s_
0sI[lI1+dsIlhx2>_z8>s]dssx</langsyntaxhighlight>
Output:
<pre>1
Line 1,043 ⟶ 2,520:
28
31</pre>
 
=={{header|DCL}}==
<syntaxhighlight lang="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*</syntaxhighlight>
{{out}}
<pre> 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</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| Boost.Int}}
Adaptation of [[#Pascal]]. The lib '''Boost.Int''' can be found here [https://github.com/MaiconSoft/DelphiBoostLib]
<syntaxhighlight lang="delphi">
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.</syntaxhighlight>
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
=={{header|Draco}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
<pre> 1
7
10
13
19
23
28
31</pre>
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">function IsHappy(n : Integer) : Boolean;
var
cache : array of Integer;
Line 1,074 ⟶ 2,728:
Dec(n);
end;
end;</langsyntaxhighlight>
Output:
<pre>1
Line 1,084 ⟶ 2,738:
28
31</pre>
 
=={{header|Dyalect}}==
 
<syntaxhighlight lang="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()</syntaxhighlight>
 
{{out}}
 
<pre>1 7 10 13 19 23 28 31</pre>
 
=={{header|Déjà Vu}}==
<syntaxhighlight lang="dejavu">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</syntaxhighlight>
{{output}}
<pre>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</pre>
 
=={{header|E}}==
{{output?|E}}
<langsyntaxhighlight lang="e">def isHappyNumber(var x :int) {
var seen := [].asSet()
while (!seen.contains(x)) {
Line 1,106 ⟶ 2,842:
println(x)
if ((count += 1) >= 8) { break }
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func dsum n .
while n > 0
d = n mod 10
s += d * d
n = n div 10
.
return s
.
func happy n .
while n > 999
n = dsum n
.
len seen[] 999
repeat
n = dsum n
until seen[n] = 1
seen[n] = 1
.
return if n = 1
.
while cnt < 8
n += 1
if happy n = 1
cnt += 1
write n & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
1 7 10 13 19 23 28 31
</pre>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="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
 
</syntaxhighlight>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 6.x :
<syntaxhighlight lang="elena">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())
}</syntaxhighlight>
{{out}}
<pre>
First 8 happy numbers: 1,7,10,13,19,23,28,31
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="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)</syntaxhighlight>
 
{{out}}
<pre>
[1, 7, 10, 13, 19, 23, 28, 31]
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight Erlanglang="erlang">-module(tasks).
-export([main/0]).
-import(lists, [map/2, member/2, sort/1, sum/1]).
Line 1,141 ⟶ 3,073:
main() ->
main(0, []).
</syntaxhighlight>
</lang>
Command: <langsyntaxhighlight Bashlang="bash">erl -run tasks main -run init stop -noshell</langsyntaxhighlight>
Output: <langsyntaxhighlight Bashlang="bash">8 Happy Numbers: [1,7,10,13,19,23,28,31]</langsyntaxhighlight>
 
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):
<langsyntaxhighlight Erlanglang="erlang">-module(tasks).
 
-export([main/0]).
Line 1,163 ⟶ 3,095:
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.</langsyntaxhighlight>
Output:
<pre>[1,7,10,13,19,23,28,31]</pre>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function is_happy(integer n)
sequence seen
integer k
Line 1,196 ⟶ 3,128:
end if
n += 1
end while</langsyntaxhighlight>
Output:
<pre>1
Line 1,209 ⟶ 3,141:
 
=={{header|F_Sharp|F#}}==
{{output?|F Sharp}}
This requires the F# power pack to be referenced and the 2010 beta of F#
<langsyntaxhighlight lang="fsharp">open System.Collections.Generic
open Microsoft.FSharp.Collections
 
Line 1,238 ⟶ 3,169:
|> 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</lang>
|> Seq.iter (Printf.printf "%d\n") // Print results
</syntaxhighlight>
Output:
<pre>
1
7
10
13
19
23
28
31
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators kernel make math sequences ;
 
: squares ( n -- s )
Line 1,261 ⟶ 3,205:
dup happy? [ dup , [ 1 - ] dip ] when 1 +
] while 2drop
] { } make ;</langsyntaxhighlight>
{{out}}
Output:
<langsyntaxhighlight lang="factor">8 happy-numbers ! { 1 7 10 13 19 23 28 31 }</langsyntaxhighlight>
 
=={{header|FALSE}}==
<syntaxhighlight lang="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+]#
%%</syntaxhighlight>
 
{{out}}
<pre>Happy numbers: 1 7 10 13 19 23 28 31</pre>
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">class Main
{
static Bool isHappy (Int n)
Line 1,301 ⟶ 3,265:
}
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,313 ⟶ 3,277:
31
</pre>
 
=={{header|FOCAL}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
{{out}}
 
<pre>= 1
= 7
= 10
= 13
= 19
= 23
= 28
= 31</pre>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: next ( n -- n )
0 swap begin 10 /mod >r dup * + r> ?dup 0= until ;
 
Line 1,334 ⟶ 3,330:
loop drop ;
 
8 happy-numbers \ 1 7 10 13 19 23 28 31</langsyntaxhighlight>
 
===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.
<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')
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</syntaxhighlight>
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
Produces the 1 millionth happy number with:
<syntaxhighlight lang="forth">: 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</syntaxhighlight>
in about 9 seconds.
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">program happy
 
implicit none
Line 1,403 ⟶ 3,421:
end function is_happy
 
end program happy</langsyntaxhighlight>
Output:
<pre>1
Line 1,417 ⟶ 3,435:
 
{{trans|Haskell}}
{{Works with|Frege|3.2021.113586-g026e8d7}}
 
<langsyntaxhighlight lang="frege">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
Line 1,429 ⟶ 3,447:
 
isHappy :: Integer -> Bool
isHappy = p emptyemptyMap
where p _ 1n = true
p s n | n `member` s = false
Line 1,435 ⟶ 3,453:
f = sum . map (sqr . digitToInteger) . unpacked . show
 
main _ = putStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n</syntaxhighlight>
numbers :: Integer -> [Integer]
numbers x = x:(numbers (x + 1n))
 
main _ = printStrLn $ unwords $ map show $ take 8 $ filter isHappy $ numbers 1n</lang>
 
{{out}}
Line 1,444 ⟶ 3,459:
<pre>
1 7 10 13 19 23 28 31
runtime 0.251614 wallclock seconds.
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn IsHappy( num as NSUInteger ) as NSUInteger
NSUInteger i, happy = 0, count = 0
while ( count < 50 ) and ( happy != 1 )
CFStringRef numStr = str( num )
count++ : happy = 0
for i = 1 to len( numStr )
happy = happy + fn StringIntegerValue( mid( numStr, i, 1 ) ) ^ 2
next
num = happy
wend
end fn = num
 
void local fn HappyNumbers
NSUInteger i, count = 0
for i = 1 to 100
if ( fn IsHappy(i) == 1 )
count++
NSLog( @"%2lu. %2lu is a happy number", count, i )
if count == 8 then exit fn
end if
next
end fn
 
fn HappyNumbers
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
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
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Happy_numbers}}
 
'''Solution.'''
 
The following function returns whether a given number is happy or not:
 
[[File:Fōrmulæ - Happy numbers 01.png]]
 
Retrieving the first 8 happy numbers
 
[[File:Fōrmulæ - Happy numbers 02.png]]
 
[[File:Fōrmulæ - Happy numbers 03.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import ("fmt"
"fmt"
"strconv"
)
 
func happy(n int) bool {
m := make(map[int]intbool)
for n > 1 {
m[n] = 0true
var x int
s := strconv.Itoa(n)
for x, n = n, 0; x > n0; x /= 010 {
for _, d := rangex s% {10
n += d * d
x := int(d) - '0'
}
n += x * x
if m[n] {
}
return false
if _, ok := m[n]; ok {
}
return false
}
}
return true
}
return true
}
 
func main() {
for found, n := 0, 1; found < 8; n++ {
if happy(n) {
fmt.Print(n, " ")
found++
}
}
}
}
fmt.Println("")
}</langsyntaxhighlight>
{{out}}
Output:
<pre>
1 7 10 13 19 23 28 31
</pre>
 
=={{header|Groovy}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|Harbour}}==
<syntaxhighlight lang="xbase">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 )</syntaxhighlight>
Output:
 
The first 8 happy numbers are:
1 7 10 13 19 23 28 31
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="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`_ s1 = FalseTrue
p s n
| otherwise = p (insert n s) (f n)
| n `member` s = False
f = sum . map ((^2) . toInteger . digitToInt) . show
| otherwise = p (insert n s) (f n)
f = sum . fmap ((^ 2) . toInteger . digitToInt) . show
 
main :: IO ()
main = mapM_ print $ take 8 $ filter isHappy [1..]</lang>
main = mapM_ print $ take 8 $ filter isHappy [1 ..]</syntaxhighlight>
Output:
{{Out}}
<pre>1
7
Line 1,509 ⟶ 3,642:
 
We can create a cache for small numbers to greatly speed up the process:
<langsyntaxhighlight lang="haskell">import Data.Array (Array, (!), listArray)
 
happy :: Int -> Bool
happy x = if xx <= 150 then seen!xx else happy xx where
happy x
xx = dsum x
| xx <= 150 = seen ! xx
seen :: Array Int Bool
| otherwise = happy xx
seen = listArray (1,150) $ True:False:False:False:(map happy [5..150])
where
dsum n | n < 10 = n * n
| otherwise = let (q,r)xx = n `divMod` 10 in r*r + dsum qx
seen :: Array Int Bool
seen =
main = print $ sum $ take 10000 $ filter happy [1..]</lang>
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 ..]</syntaxhighlight>
{{Out}}
<pre>327604323</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
local n
n := arglist[1] | 8 # limiting number of happy numbers to generate, default=8
Line 1,537 ⟶ 3,681:
if happy(n) then return i
}
end</langsyntaxhighlight>
Usage and Output:
<pre>
Line 1,546 ⟶ 3,690:
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> 8{. (#~1=+/@(*:@(,.&.":))^:(1&~:*.4&~:)^:_ "0) 1+i.100
1 7 10 13 19 23 28 31</langsyntaxhighlight>
This is a repeat while construction
<langsyntaxhighlight lang="j"> f ^: cond ^: _ input</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="j"> (binary array) # 1..100</langsyntaxhighlight>
So for easier reading the solution could be expressed as:
<langsyntaxhighlight lang="j"> cond=: 1&~: *. 4&~: NB. not equal to 1 and not equal to 4
sumSqrDigits=: +/@(*:@(,.&.":))
 
Line 1,559 ⟶ 3,703:
14
8{. (#~ 1 = sumSqrDigits ^: cond ^:_ "0) 1 + i.100
1 7 10 13 19 23 28 31</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
{{trans|JavaScript}}
<langsyntaxhighlight lang="java5">import java.util.HashSet;
public class Happy{
public static boolean happy(long number){
Line 1,590 ⟶ 3,734:
}
}
}</langsyntaxhighlight>
Output:
<pre>1
7
10
13
19
23
28
31</pre>
 
 
===Java 1.8===
{{works with|Java|1.8}}
{{trans|Java}}
<syntaxhighlight lang="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;
}
}</syntaxhighlight>
Output:
<pre>1
Line 1,602 ⟶ 3,788:
 
=={{header|JavaScript}}==
 
<lang javascript>function happy(number) {
===ES5===
====Iteration====
<syntaxhighlight lang="javascript">function happy(number) {
var m, digit ;
var cycle = [] ;
Line 1,627 ⟶ 3,816:
document.write(number + " ") ;
number++ ;
}</langsyntaxhighlight>
Output:
<pre>1 7 10 13 19 23 28 31 </langpre>
 
===ES6===
====Functional composition====
{{Trans|Haskell}}
<syntaxhighlight lang="javascript">(() => {
 
// 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)))
);
})()</syntaxhighlight>
{{Out}}
<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:
<syntaxhighlight lang="javascript">(() => {
 
// 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
);
})();</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="javascript">[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.4}}
<syntaxhighlight lang="jq">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 );</syntaxhighlight>
'''Emit a stream of the first n happy numbers''':
<syntaxhighlight lang="jq"># 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)</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">$ jq --arg n 8 -n -f happy.jq
1
7
10
13
19
23
28
31
</syntaxhighlight>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
function happy(x)
happy_ints = Int[]
int_try = 1
while length(happy_ints) < x
n = int_try
past = Int[]
while n != 1
n = sum(y^2 for y in digits(n))
n in past && break
push!(past, n)
end
n == 1 && push!(happy_ints,int_try)
int_try += 1
end
return happy_ints
end</syntaxhighlight>
Output
<pre> julia> happy(8)
8-element Int32 Array:
1
7
10
13
19
23
28
31</pre>
A recursive version:
<syntaxhighlight lang="julia">sumhappy(n) = sum(x->x^2, digits(n))
 
function ishappy(x, mem = Int[])
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) = accumulate((a, b) -> nexthappy(a), 1:n)
</syntaxhighlight>
{{Out}}
<pre>julia> show(happy(8))
[1,7,10,13,19,23,28,31,32]</pre>
 
Alternate, Translation of C<br>
Faster with use of cache
{{trans|C}}
<syntaxhighlight lang="julia">const CACHE = 256
buf = zeros(Int, CACHE)
buf[begin] = 1
 
function happy(n)
if n < CACHE
buf[n] > 0 && return 2-buf[n]
buf[n] = 2
end
sqsum = 0
nn = n
while nn != 0
nn, x = divrem(nn, 10)
sqsum += x * x
end
x = happy(sqsum)
n < CACHE && (buf[n] = 2 - x)
return x
end
 
function main()
i, counter = 1, 1000000
while counter > 0
if happy(i) != 0
counter -= 1
end
i += 1
end
return i - 1
end
</syntaxhighlight>
 
=={{header|K}}==
<langsyntaxhighlight lang="k"> hpy: {x@&1={~|/x=1 4}{_+/_sqr 0$'$x}//:x}
 
hpy 1+!100
Line 1,638 ⟶ 4,090:
 
8#hpy 1+!100
1 7 10 13 19 23 28 31</langsyntaxhighlight>
 
Another implementation which is easy to follow is given below:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="k">
<lang lb> ct = 0
/ happynum.k
n = 0
 
DO
/ sum of squares of digits of an integer
n = n + 1
dgtsmsqr: {d::(); (0<){d::d,x!10; x%:10}/x; +/d*d}
IF HappyN(n, sqrInt$) = 1 THEN
/ Test if an integer is a Happy number
ct = ct + 1
isHappy: {s::(); while[1<x;a:(dgtsmsqr x); :[(a _in s); :0; s::s,a]; x:a];:1} / Returns 1 if Happy
PRINT ct, n
/ Generate first x Happy numbers and display the list
END IF
hnum: {[x]; h::();i:1;while[(#h)<x; :[(isHappy i); h::(h,i)]; i+:1]; `0: ,"List of ", ($x), " Happy Numbers"; h}
LOOP UNTIL ct = 8
END
 
</syntaxhighlight>
FUNCTION HappyN(n, sqrInts$)
 
n$ = Str$(n)
The output of a session with this implementation is given below:
sqrInts = 0
{{out}}
FOR i = 1 TO Len(n$)
<pre>
sqrInts = sqrInts + Val(Mid$(n$, i, 1)) ^ 2
K Console - Enter \ for help
NEXT i
 
IF sqrInts = 1 THEN
\l happynum
HappyN = 1
hnum 8
EXIT FUNCTION
List of 8 Happy Numbers
END IF
1 7 10 13 19 23 28 31
IF Instr(sqrInts$, ":";Str$(sqrInts);":") > 0 THEN
HappyN = 0
EXIT FUNCTION
END IF
sqrInts$ = sqrInts$ + Str$(sqrInts) + ":"
HappyN = HappyN(sqrInts, sqrInts$)
END FUNCTION</lang>
Output:-
<pre>1 1
2 7
3 10
4 13
5 19
6 23
7 28
8 31
</pre>
 
=={{header|Locomotive BasicKotlin}}==
{{trans|C#}}
<syntaxhighlight lang="scala">// version 1.0.5-2
 
fun isHappy(n: Int): Boolean {
<lang locobasic>10 mode 1:defint a-z
val cache = mutableListOf<Int>()
20 for i=1 to 100
var sum = 0
30 i2=i
var nn = n
40 for l=1 to 20
var digit: Int
50 a$=str$(i2)
while (nn != 1) {
60 i2=0
if (nn in cache) return false
70 for j=1 to len(a$)
cache.add(nn)
80 d=val(mid$(a$,j,1))
while (nn != 0) {
90 i2=i2+d*d
digit = nn % 10
100 next j
sum += digit * digit
110 if i2=1 then print i;"is a happy number":n=n+1:goto 150
nn /= 10
120 if i2=4 then 150 ' cycle found
}
130 next l
nn = sum
140 ' check if we have reached 8 numbers yet
sum = 0
150 if n=8 then end
}
160 next i</lang>
return true
}
 
fun main(args: Array<String>) {
[[File:Happy Numbers, Locomotive BASIC.png]]
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(", "))
}</syntaxhighlight>
 
{{out}}
<pre>
First 8 happy numbers : 1, 7, 10, 13, 19, 23, 28, 31
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def happy
{def happy.sum
{lambda {:n}
{if {= {W.length :n} 1}
then {pow {W.first :n} 2}
else {+ {pow {W.first :n} 2}
{happy.sum {W.rest :n}}}}}}
{def happy.is
{lambda {:x :a}
{if {= :x 1}
then true
else {if {> {A.in? :x :a} -1}
then false
else {happy.is {happy.sum :x}
{A.addlast! :x :a}}}}}}
{def happy.rec
{lambda {:n :a :i}
{if {= {A.length :a} :n}
then :a
else {happy.rec :n
{if {happy.is :i {A.new}}
then {A.addlast! :i :a}
else :a}
{+ :i 1}}}}}
{lambda {:n}
{happy.rec :n {A.new} 0}}}
-> happy
 
{happy 8}
-> [1,7,10,13,19,23,28,31]
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="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</syntaxhighlight>
Output:
<syntaxhighlight lang="lasso">1, 7, 10, 13, 19, 23, 28, 31</syntaxhighlight>
 
=={{header|Logo}}==
 
<langsyntaxhighlight lang="logo">to sum_of_square_digits :number
output (apply "sum (map [[d] d*d] ` :number))
end
Line 1,726 ⟶ 4,232:
 
print n_happy 8
bye</langsyntaxhighlight>
 
Output:
<pre>1 7 10 13 19 23 28 31</pre>
 
=={{header|LOLCODE}}==
{{works with|lci 0.10.3}}
 
<syntaxhighlight lang="lolcode">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</syntaxhighlight>
 
Output:<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function digits(n)
if n > 0 then return n % 10, digits(math.floor(n/10)) end
end
Line 1,746 ⟶ 4,348:
repeat
i, j = happy[j] and (print(j) or i+1) or i, j + 1
until i == 8</langsyntaxhighlight>
Output:
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|M2000 Interpreter}}==
{{trans|ActionScript}}
 
Lambda Function PrintHappy has a closure another lambda function IsHappy which has a closure of another lambda function the sumOfSquares.
 
 
<syntaxhighlight lang="m2000 interpreter">
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()
</syntaxhighlight>
{{out}}
<pre>
1
7
10
13
19
23
28
31</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE HAPPY
.MCALL .TTYOUT,.EXIT
HAPPY:: MOV #^D8,R5 ; 8 HAPPY NUMBERS
CLR R4
1$: INC R4
MOV R4,R0
JSR PC,CHECK
BNE 1$
MOV R4,R0
JSR PC,PR0
SOB R5,1$
.EXIT
 
; CHECK IF R0 IS HAPPY: ZERO FLAG SET IF TRUE
CHECK: MOV #200,R1
MOV #3$,R2
1$: CLR (R2)+
SOB R1,1$
2$: INCB 3$(R0)
JSR PC,SUMSQ
TST 3$(R0)
BEQ 2$
DEC R0
RTS PC
3$: .BLKW 200
 
; LET R0 = SUM OF SQUARES OF DIGITS OF R0
SUMSQ: CLR R2
1$: MOV #-1,R1
2$: INC R1
SUB #12,R0
BCC 2$
ADD #12,R0
MOVB 3$(R0),R0
ADD R0,R2
MOV R1,R0
BNE 1$
MOV R2,R0
RTS PC
3$: .BYTE ^D 0,^D 1,^D 4,^D 9,^D16
.BYTE ^D25,^D36,^D49,^D64,^D81
 
; PRINT NUMBER IN R0 AS DECIMAL.
PR0: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .BYTE 15,12,0
.END HAPPY</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|MAD}}==
 
<syntaxhighlight lang="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
</syntaxhighlight>
 
{{out}}
 
<pre> 1
7
10
13
Line 1,759 ⟶ 4,525:
=={{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.)
<langsyntaxhighlight Maplelang="maple">SumSqDigits := proc( n :: posint )
local s := 0;
local m := n;
Line 1,766 ⟶ 4,532:
end do;
s
end proc:</langsyntaxhighlight>
(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,
<syntaxhighlight lang="maple">
<lang Maple>
> SumSqDigits( 1234567890987654321 );
570
</syntaxhighlight>
</lang>
We can check this by computing it another way (more directly).
<syntaxhighlight lang="maple">
<lang Maple>
> n := 1234567890987654321:
> `+`( op( map( parse, StringTools:-Explode( convert( n, 'string' ) ) )^~2) );
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).
<langsyntaxhighlight Maplelang="maple">Happy? := proc( n )
if n = 1 then
true
Line 1,793 ⟶ 4,559:
evalb( s = 1 )
end if
end proc:</langsyntaxhighlight>
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 ) ):
> nops( H ), nops( S );
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.
<langsyntaxhighlight Maplelang="maple">FindHappiness := proc( N )
local count := 0;
local T := table();
Line 1,811 ⟶ 4,577:
end do;
{seq}( T[ i ], i = 1 .. count )
end proc:</langsyntaxhighlight>
With input equal to 8, we get
<syntaxhighlight lang="maple">
<lang Maple>
> FindHappiness( 8 );
{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.
<langsyntaxhighlight Maplelang="maple">Happy? := proc( n :: posint )
local a, b;
a, b := n, SumSqDigits( n );
Line 1,826 ⟶ 4,592:
end do;
evalb( a = 1 )
end proc:</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Custom function HappyQ:
<langsyntaxhighlight Mathematicalang="mathematica">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</langsyntaxhighlight>
Examples for a specific number:
<langsyntaxhighlight Mathematicalang="mathematica">HappyQ[1337]
HappyQ[137]</langsyntaxhighlight>
gives back:
<syntaxhighlight lang="mathematica">True
<lang Mathematica>True
False</langsyntaxhighlight>
Example finding the first 8:
<langsyntaxhighlight Mathematicalang="mathematica">m = 8;
n = 1;
i = 0;
Line 1,851 ⟶ 4,617:
]
]
happynumbers</langsyntaxhighlight>
gives back:
<langsyntaxhighlight Mathematicalang="mathematica">{1, 7, 10, 13, 19, 23, 28, 31}</langsyntaxhighlight>
 
=={{header|MATLAB}}==
Recursive version:
<syntaxhighlight lang="matlab">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</syntaxhighlight>
{{out}}
<pre>1 7 10 13 19 23 28 31 </pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that decomposes te number into a list */
decompose(N) := block(
digits: [],
while N > 0 do
(remainder: mod(N, 10),
digits: cons(remainder, digits),
N: floor(N/10)),
digits
)$
 
/* Function that given a number returns the sum of their digits */
sum_squares_digits(n):=block(
decompose(n),
map(lambda([x],x^2),%%),
apply("+",%%))$
 
/* Predicate function based on the task iterated digits squaring */
happyp(n):=if n=1 then true else if n=89 then false else block(iter:n,while not member(iter,[1,89]) do iter:sum_squares_digits(iter),iter,if iter=1 then true)$
 
/* Test case */
/* First eight happy numbers */
block(
happy:[],i:1,
while length(happy)<8 do (if happyp(i) then happy:endcons(i,happy),i:i+1),
happy);
</syntaxhighlight>
{{out}}
<pre>
[1,7,10,13,19,23,28,31]
</pre>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="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
)
</syntaxhighlight>
Output:
<syntaxhighlight lang="maxscript">
1
7
10
13
19
23
28
31
</syntaxhighlight>
 
=={{header|Mercury}}==
<syntaxhighlight lang="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.</syntaxhighlight>
{{out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{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.
<syntaxhighlight lang="miniscript">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</syntaxhighlight>
{{out}}
<pre>First 8 happy numbers: [1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show (take 8 happynumbers)))]
 
happynumbers :: [num]
happynumbers = filter ishappy [1..]
 
ishappy :: num->bool
ishappy n = 1 $in loop (iterate sumdigitsquares n)
 
sumdigitsquares :: num->num
sumdigitsquares 0 = 0
sumdigitsquares n = (n mod 10)^2 + sumdigitsquares (n div 10)
 
loop :: [*]->[*]
loop = loop' []
where loop' mem (a:as) = mem, if a $in mem
= loop' (a:mem) as, otherwise
 
in :: *->[*]->bool
in val [] = False
in val (a:as) = True, if a=val
= val $in as, otherwise</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|ML}}==
==={{header|mLite}}===
<syntaxhighlight lang="ocaml">(*
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;
</syntaxhighlight>
Output:
<pre>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</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">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.</syntaxhighlight>
{{out}}
<pre> 1
7
10
13
19
23
28
31</pre>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="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
Line 1,879 ⟶ 4,953:
FOR I=1:1 QUIT:C<1 SET Q=+$$ISHAPPY(I) WRITE:Q !,I SET:Q C=C-1
KILL I
QUIT</langsyntaxhighlight>
Output:<pre>
USER>D HAPPY^ROSETTA(8)
Line 1,901 ⟶ 4,975:
=={{header|NetRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="netrexx">/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
limit = arg[0] /*get argument for LIMIT. */
say limit
Line 1,928 ⟶ 5,002:
q=sum /*now, lets try the Q sum. */
end
end</langsyntaxhighlight>
;Output
<pre>
Line 2,043 ⟶ 5,117:
694
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">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</syntaxhighlight>
Output:
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use IO;
use Structure;
 
Line 2,091 ⟶ 5,197:
}
}
}</langsyntaxhighlight>
output:
<pre>First 8 happy numbers: 1,7,10,13,19,23,28,31,</pre>
Line 2,097 ⟶ 5,203:
=={{header|OCaml}}==
Using [[wp:Cycle detection|Floyd's cycle-finding algorithm]].
<langsyntaxhighlight lang="ocaml">open Num
 
let step =
Line 2,121 ⟶ 5,227:
 
List.iter print_endline (
List.rev_map string_of_num (first 8)) ;;</langsyntaxhighlight>
Output:
<pre>$ ocaml nums.cma happy_numbers.ml
Line 2,132 ⟶ 5,238:
28
31</pre>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="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 ;</syntaxhighlight>
 
Output:
<pre>
>happyNum(8)
[1, 7, 10, 13, 19, 23, 28, 31]
</pre>
 
=={{header|Ol}}==
<syntaxhighlight lang="Scheme">
(define (number->list num)
(let loop ((num num) (lst #null))
(if (zero? num)
lst
(loop (quotient num 10) (cons (remainder num 10) lst)))))
 
(define (** x) (* x x))
 
(define (happy? num)
(let loop ((num num) (seen #null))
(cond
((= num 1) #true)
((memv num seen) #false)
(else
(loop (apply + (map ** (number->list num)))
(cons num seen))))))
 
(display "happy numbers: ")
(let loop ((n 1) (count 0))
(unless (= count 8)
(if (happy? n)
then
(display n) (display " ")
(loop (+ n 1) (+ count 1))
else
(loop (+ n 1) count))))
(print)
</syntaxhighlight>
<pre>
happy numbers: 1 7 10 13 19 23 28 31
</pre>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
count = 0
say "First 8 happy numbers are:"
Line 2,163 ⟶ 5,328:
number = next
end
</syntaxhighlight>
</lang>
<pre>
First 8 happy numbers are:
Line 2,175 ⟶ 5,340:
31
</pre>
 
=={{header|Oz}}==
<syntaxhighlight lang="oz">functor
{{output?|Oz}}
import
<lang oz>declare
System
define
fun {IsHappy N}
{IsHappy2 N nil}
Line 2,216 ⟶ 5,384:
HappyNumbers = {LFilter {Nat 1} IsHappy}
in
{ShowSystem.show {List.take HappyNumbers 8}}</lang>
end</syntaxhighlight>
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
 
=={{header|PARI/GP}}==
{{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.
<langsyntaxhighlight 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)={
if(n<262,
Line 2,230 ⟶ 5,401:
)
};
select(isHappy, vector(31,i,i))</langsyntaxhighlight>
Output:
<pre>%1 = [1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program HappyNumbers (output);
 
uses
Line 2,295 ⟶ 5,466:
end;
writeln;
end.</langsyntaxhighlight>
Output:
<pre>:> ./HappyNumbers
1 7 10 13 19 23 28 31
</pre>
===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
<syntaxhighlight lang="pascal">Program HappyNumbers (output);
{$IFDEF FPC}
{$MODE DELPHI}
{$OPTIMIZATION ON,All}
{$ELSE}
{$APPLICATION CONSOLE}
{$ENDIF}
//{$DEFINE Use1E9}
uses
sysutils,//Timing
strutils;//Numb2USA
 
const
=={{header|Perl}}==
base = 10;
<lang perl>use List::Util qw(sum);
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
sub is_happy ($)
tSumSqrDgts = array[0..cDigit2] of word;
{for (my ($n, %seen) = shift ;; $n = sum map {$_**2} split //, $n)
tCache = array[0..2*HighCache] of word;
{$n == 1 and return 1;
tSqrdSumCache = array[0..2*HighCache] of Uint32;
$seen{$n}++ and return 0;}}
 
var
for (my ($n, $happy) = (1, 0) ; $happy < 8 ; ++$n)
SumSqrDgts :tSumSqrDgts;
{is_happy $n or next;
Cache : print "$n\n"tCache;
++$happy;}</lang>
Output:
<pre>perl coins.pl
 
SqrdSumCache1,
1
SqrdSumCache2 :tSqrdSumCache;
7
 
10
T1,T0 : TDateTime;
13
MAX2,Max1 : NativeInt;
19
 
23
procedure InitSumSqrDgts;
28
//calc all sum of squared digits 0..cDigits2
31
//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.
</syntaxhighlight>
;output:
<pre>
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
</pre>
 
=={{header|Perl 6}}==
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 perl6>sub happy (Int $n is copy --> Bool) {
<syntaxhighlight lang="perl">use List::Util qw(sum);
my %seen;
 
loop {
sub ishappy {
$n = [+] $n.comb.map: { $_ ** 2 }
return True ifmy $ns == 1shift;
while ($s > 6 return&& False$s if!= 89) %seen{$n}++;
$s = sum(map { $_*$_ } split(//,$s));
}
$s == 1;
}
 
my $n = 0;
say join ' ', grep(&happy, 1 .. *)[^8];</lang>
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";</syntaxhighlight>
Output:
{{out}}
<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.
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.
<lang perl6>my @happy := gather for 1..* -> $number {
{{trans|Raku}}
my %stopper = 1 => 1;
<syntaxhighlight lang="perl">use List::Util qw(sum);
my $n = $number;
sub is_happy {
repeat until %stopper{$n}++ {
my ($n) = [+] $n.comb X** 2@_;
my %seen;
while (1) {
$n = sum map { $_ ** 2 } split //, $n;
return 1 if $n == 1;
return 0 if $seen{$n}++;
}
take $number if $n == 1;
}
 
my $n;
say ~@happy[^8];</lang>
is_happy( ++$n ) and print "$n " or redo for 1..8;</syntaxhighlight>
Output:
{{out}}
<pre>1 7 10 13 19 23 28 31</pre>
 
There's more than one way to do it...
=={{header|Phix}}==
Copy of [[Happy_numbers#Euphoria|Euphoria]] tweaked to give a one-line output
<!--<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: #004080;">sequence</span> <span style="color: #000000;">seen</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">seen</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">seen</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">is_happy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
{1,7,10,13,19,23,28,31}
</pre>
 
=={{header|PHP}}==
{{trans|D}}
<langsyntaxhighlight lang="php">function isHappy($n) {
while (1) {
$total = 0;
Line 2,379 ⟶ 5,871:
}
$i++;
}</langsyntaxhighlight>
<pre>1 7 10 13 19 23 28 31 </pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="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.</syntaxhighlight>
 
{{out}}
<pre>[1,7,10,13,19,23,28,31]</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de happy? (N)
(let Seen NIL
(loop
Line 2,395 ⟶ 5,917:
(do 8
(until (happy? (inc 'H)))
(printsp H) ) )</langsyntaxhighlight>
Output:
<pre>1 7 10 13 19 23 28 31</pre>
 
=={{header|PILOT}}==
<syntaxhighlight lang="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 :</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">test: proc options (main); /* 19 November 2011 */
declare (i, j, n, m, nh initial (0) ) fixed binary (31);
 
Line 2,424 ⟶ 5,994:
end;
end test;
</syntaxhighlight>
</lang>
OUTPUT:
<pre>
Line 2,436 ⟶ 6,006:
31 is a happy number
</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">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</syntaxhighlight>
{{out}}
<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Potion}}==
<syntaxhighlight lang="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</syntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">function happy([int] $n) {
{{output?|PowerShell}}
===Traditional===
<lang PowerShell>function happy([int] $n) {
$a=@()
for($i=2;$a.count -lt $n;$i++) {
Line 2,448 ⟶ 6,120:
if($sum -eq 1) {
$a+=$i
$i
}
$hist[$sum]=$sum
Line 2,459 ⟶ 6,130:
}
}
$a -join ','
}</lang>
}</syntaxhighlight>
===With Pipeline===
Output :
<lang PowerShell>function happy([int] $n) {
<syntaxhighlight lang="powershell">happy(8)
1..$n | ForEach-Object {
7,10,13,19,23,28,31,32</syntaxhighlight>
$sum=$_
$hist=@{}
while( $hist[$sum] -eq $null ) {
if($sum -eq 1) {
$_
}
$hist[$sum]=$sum
$sum2=0
foreach($j in $sum.ToString().ToCharArray()) {
$k=([int]$j)-0x30
$sum2+=$k*$k
}
$sum=$sum2
}
}
}</lang>
 
=={{header|Prolog}}==
{{Works with|SWI-Prolog}}
<langsyntaxhighlight Prologlang="prolog">happy_numbers(L, Nb) :-
% creation of the list
length(L, Nb),
Line 2,528 ⟶ 6,184:
 
square(N, SN) :-
SN is N * N.</langsyntaxhighlight>
Output :
<langsyntaxhighlight Prologlang="prolog"> ?- happy_numbers(L, 8).
L = [1,7,10,13,19,23,28,31].</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<lang 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</lang>
Sample output:
<pre>#1 1
#2 7
#3 10
#4 13
#5 19
#6 23
#7 28
#8 31</pre>
 
=={{header|Python}}==
===Procedural===
<lang python>>>> def happy(n):
<syntaxhighlight lang="python">>>> def happy(n):
past = set()
while n <>!= 1:
n = sum(int(i)**2 for i in str(n))
if n in past:
Line 2,589 ⟶ 6,201:
 
>>> [x for x in xrange(500) if happy(x)][:8]
[1, 7, 10, 13, 19, 23, 28, 31]</langsyntaxhighlight>
 
===Composition of pure functions===
 
Drawing 8 terms from a non finite stream, rather than assuming prior knowledge of the finite sample size required:
<syntaxhighlight lang="python">'''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()</syntaxhighlight>
{{Out}}
<pre>[1, 7, 10, 13, 19, 23, 28, 31]</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="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</syntaxhighlight>
 
{{Out}}
 
<pre>[ 1 7 10 13 19 23 28 31 ]</pre>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">is.happy <- function(n)
{
stopifnot(is.numeric(n) && length(n)==1)
Line 2,620 ⟶ 6,347:
}
happy
}</langsyntaxhighlight>
Example usage
<syntaxhighlight lang R="r">is.happy(2)</langsyntaxhighlight>
[1] FALSE
attr(,"cycle")
[1] 4 16 37 58 89 145 42 20
<langsyntaxhighlight Rlang="r">#Find happy numbers between 1 and 50
which(apply(rbind(1:50), 2, is.happy))</langsyntaxhighlight>
1 7 10 13 19 23 28 31 32 44 49
<langsyntaxhighlight Rlang="r">#Find the first 8 happy numbers
happies <- c()
i <- 1L
Line 2,637 ⟶ 6,364:
i <- i + 1L
}
happies</langsyntaxhighlight>
1 7 10 13 19 23 28 31
 
=={{header|REXXRacket}}==
<syntaxhighlight lang="racket">#lang racket
===unoptimized===
(define (sum-of-squared-digits number (result 0))
<lang REXX>/*REXX program displays eight (or a specified amount) happy numbers. */
(if (zero? number)
parse arg limit . /*get argument for LIMIT. */
result
if limit=='' then limit=8 /*Not specified? Set LIMIT to 8.*/
(sum-of-squared-digits (quotient number 10)
haps=0 /*count of happy numbers so far. */
(+ result (expt (remainder number 10) 2)))))
 
(define (happy-number? number (seen null))
do n=1 while haps<limit; q=n; a.=0 /*search integers starting at 1. */
(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)
do until q==1 /*see if Q is a happy number. */
(for/list ((x (in-range max))
s=0 /*prepare to add squares of digs.*/
#:when (happy-number? x))
do j=1 for length(q) /*sum the squares of the digits. */
x))
s=s + substr(q,j,1)**2
end /*j*/
 
(display (take (get-happys 100) 8)) ;displays (1 7 10 13 19 23 28 31)</syntaxhighlight>
if a.s then iterate n /*if already summed, Q is unhappy*/
a.s=1 /*mark the sum as being found. */
q=s /*now, lets try the Q sum. */
end /*forever*/
 
=={{header|Raku}}==
say n /*display the number (N is happy)*/
(formerly Perl 6)
haps=haps+1 /*bump the count of happy numbers*/
{{works with|rakudo|2015-09-13}}
end /*n*/
<syntaxhighlight lang="raku" line>sub happy (Int $n is copy --> Bool) {
/*stick a fork in it, we're done.*/</lang>
loop {
'''output''' (using the default input of 8)
state %seen;
<pre>
$n = [+] $n.comb.map: { $_ ** 2 }
1
return True if $n == 1;
return False if %seen{$n}++;
}
}
 
say join ' ', grep(&happy, 1 .. *)[^8];</syntaxhighlight>
{{out}}
<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.
<syntaxhighlight lang="raku" line>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];</syntaxhighlight>
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):
<syntaxhighlight lang="raku" line>subset Happy of Int where sub ($n) {
$n == 1 ?? True !!
$n < 7 ?? False !!
&?ROUTINE([+] $n.comb »**» 2);
}
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.
 
There's more than one way to do it...
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <ShowFirst 8 Happy 1>;
};
 
ShowFirst {
0 s.F s.I = ;
s.N s.F s.I, <Mu s.F s.I>: T =
<Prout s.I>
<ShowFirst <- s.N 1> s.F <+ s.I 1>>;
s.N s.F s.I =
<ShowFirst s.N s.F <+ s.I 1>>;
};
 
Happy {
1 e.X = T;
s.N e.X s.N e.Y = F;
s.N e.X = <Happy <SqDigSum s.N> s.N e.X>;
};
 
SqDigSum {
0 = 0;
s.N, <Symb s.N>: s.Ds e.Rs,
<Numb s.Ds>: s.D,
<Numb e.Rs>: s.R =
<+ <* s.D s.D> <SqDigSum s.R>>;
};</syntaxhighlight>
{{out}}
<pre>1
7
10
Line 2,673 ⟶ 6,466:
23
28
31</pre>
 
</pre>
=={{header|Relation}}==
'''output''' when the input is: <tt> 100 </tt>
<syntaxhighlight lang="relation">
<pre style="height:30ex;overflow:scroll">
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
</syntaxhighlight>
 
<pre>
1
7
Line 2,685 ⟶ 6,512:
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
</pre>
 
===optimized={{header|REXX}}==
This<syntaxhighlight lang="rexx">/*REXX codeprogram usescomputes additionaland memoizationdisplays (bya keepingspecified trackrange of happy/unhappy numbers),. */
Call time 'R'
<br>it's about 25% faster than the unoptimized version.
linesize=80
<lang rexx>/*REXX program displays eight (or a specified amount) happy numbers. */
parseParse argArg low high limit . /*get argumentobtain range forof happy LIMIT.numbers */
If low='?' Then Call help
if limit=='' then limit=8 /*Not specified? Set LIMIT to 8.*/
If low='' Then low=10
haps=0 /*count of happy numbers so far. */
If high='' Then
@.=0 /*sparse array of happy numbers*/
Parse Value 1 low With low high
!.=0 /*sparse array of unhappy numbers*/
Do i=0 To 9 /*build a squared decimal digit table. */
square.i=i*i
End
happy.=0 /* happy.m=1 - m is a happy number */
unhappy.=0 /* unhappy.n=1 - n is an unhappy number*/
hapn=0 /* count of the happy numbers */
ol=''
Do n=1 While hapn<high /* test integers starting with 1 */
If unhappy.n Then /* if n is unhappy, */
Iterate /* then try next number */
work=n
suml='' /* list of computed sums */
Do Forever
sum=0
Do length(work) /* compute sum of squared digits */
Parse Var work digit +1 work
sum=sum+square.digit
End
Select
When unhappy.sum |, /* sum is known to be unhappy */
wordpos(sum,suml)>0 Then Do /* or was already encountered */
-- If wordpos(sum,suml)>0 Then say 'Loop' n':' suml sum
-- If n<7 Then say n':' suml sum
unhappy.n=1 /* n is unhappy */
Call set suml /* amd so are all sums so far */
Iterate n
End
When sum=1 Then Do /* we reached sum=1 */
hapn+=1 /* increment number of happy numbers */
happy.n=1 /* n is happy */
If hapn>=low Then /* if it is in specified range */
Call out n /* output it */
If hapn=high Then /* end of range reached */
Leave n /* we are done */
Iterate n /* otherwise proceed */
End
Otherwise Do /* otherwise */
suml=suml sum /* add sum to list of sums */
work=sum /* proceed with the new sum */
End
End
End
End
If ol>'' Then /* more output data */
Say strip(ol) /* write to console */
-- Say time('E')
Exit
 
set: /* all intermediate sums are unhappy */
do n=1 while haps<limit; q=n; a.=0 /*search integers starting at 1.*/
Parse Arg list
if !.n then iterate /*if N is unhappy, try another.*/
Do While list<>''
Parse Var list s list
unhappy.s=1
End
Return
 
out: do until q==1 /*see ifoutput management Q is a happy number. */
Parse Arg s=0hn /* the happy number /*prepare to add squares of digs.*/
If length(ol hn)>linesize Then Do do j=1 /* if it does not fit for length(q) /*sum the squares of the digits. */
Say strip(ol) /* output the line */
s=s + substr(q,j,1)**2
ol=hn end /*j and start a new line */
End
Else /* otherwise */
ol=ol hn /* append is to the output line */
Return
 
help:
if @.s then do; q=1; iterate; end /*we have found a happy number.*/
Say 'rexx hno n compute and show the first n happy numbers'
if !.s then iterate n /*sum unhappy? Then Q is unhappy*/
Say 'rexx hno low high show happy numbers from index low to high'
if a.s then do /*if already summed? Q is unhappy*/
Exit
!.q=1; !.s=1 /*mark Q & S as unhappy numbers*/
</syntaxhighlight>
iterate n /*if already summed, Q is unhappy*/
{{out}}
end
<pre>
a.s=1 /*make the sum as being found. */
K:\_B\HN>rexx hno ?
q=s /*now, lets try the Q sum. */
rexx hno n compute and show the first n happy numbers
end /*forever*/
rexx hno low high show happy numbers from index low to high
 
K:\_B\HN>rexx hno 8
1 7 10 13 19 23 28 31
 
K:\_B\HN>rexx hno 1000 1003
say n /*display the number (N is happy)*/
6899 6904 6917 6923
haps=haps+1 /*bump the count of happy numbers*/
</pre>
@.n=1 /*mark N as a happy number. */
 
end /*n*/
=={{header|Ring}}==
/*stick a fork in it, we're done.*/</lang>
<syntaxhighlight lang="ring">n = 1
'''output''' is identical to the first version.
found = 0
<br><br>
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
</syntaxhighlight>
{{out}}
<pre>
1 : 1
2 : 7
3 : 10
4 : 13
5 : 19
6 : 23
7 : 28
8 : 31
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ { } SWAP '''DO'''
SWAP OVER + 0 ROT
'''DO'''
MANT RND DUP IP SQ ROT + SWAP FP
'''UNTIL''' DUP NOT '''END'''
DROP
'''UNTIL''' DUP2 POS '''END'''
SWAP DROP 1 ==
'HAPY?' STO
≪ { } 0 '''DO'''
1 + '''IF''' DUP HAPY? '''THEN''' SWAP OVER + SWAP '''END'''
'''UNTIL''' OVER SIZE 8 == '''END'''
≫ EVAL
{{out}}
<pre>
1: { 1 7 10 13 19 23 28 31 }
</pre>
=={{header|Ruby}}==
{{works with|Ruby|2.1}}
Use of cache from Python
 
<lang ruby>require 'set'
<syntaxhighlight lang="ruby">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
seen = Set[]
return @happy_numbers.include?(n) if @seen_numbers.include?(n) # Use performance cache, and stop unhappy cycles
state = while n>1 and not seen.include?(n)
 
if $happy_cache[:happy].include?(n): break :happy
@seen_numbers << n
elsif $happy_cache[:sad].include?(n): break :sad
digit_squared_sum = n.digits.sum{|n| n*n}
end
 
seen << n
if happy?(digit_squared_sum)
n = sum_of_squares_of_digits(n)
@happy_numbers << n
true # Return true
else
false # Return false
end
end</syntaxhighlight>
state.nil? and state = n == 1 ? :happy : :sad
 
$happy_cache[state] += seen
Helper method to produce output:
state == :happy
<syntaxhighlight lang="ruby">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</syntaxhighlight>
def sum_of_squares_of_digits(n)
 
n.to_s.each_char.inject(0) {|sum,c| sum += (c.to_i)**2}
{{out}}
<syntaxhighlight lang="ruby">[1, 7, 10, 13, 19, 23, 28, 31]</syntaxhighlight>
 
===Alternative version===
<syntaxhighlight lang="ruby">@memo = [0,1]
def happy(n)
sum = n.digits.sum{|n| n*n}
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
$happy_cache = Hash.new(Set[])
while count < 8
happy_numbers = []
i = 1
while happy_numbers.length < 8
happy_numbers << i if happy? i
i += 1
puts i or count+=1 if happy(i)==1
end
p happy_numbers
p $happy_cache</lang>
<pre style='width:full;overflow:scroll'>[1, 7, 10, 13, 19, 23, 28, 31]
{:happy=>[7, 49, 97, 130, 10, 13, 19, 82, 68, 100, 23, 28, 31],
:sad=>[2, 4, 16, 37, 58, 89, 145, 42, 20, 3, 9, 81, 65, 61, 5, 25, 29, 85, 6, 36, 45, 41, 17, 50, 8, 64, 52, 11, 12, 14, 15, 26, 40, 18, 21, 22, 24, 27, 53, 34, 30]}</pre>
 
puts
=={{header|Run BASIC}}==
for i in 99999999999900..99999999999999
<lang runbasic>for j = 1 to 100 ' test 1 to 100 for happy number
puts i if happy(i)==1
if isHappy(j) = 1 then print j;" is happy"
end</syntaxhighlight>
next j
 
{{out}}
function isHappy(n)
<pre>
for i = 1 to 100
1
isHappy = 0
7
while n <> 0 ' Form the sum of squares of the digits.
10
isHappy = isHappy + (n mod 10)^2
13
n = int(n/10)
19
wend
23
if isHappy = 1 then goto [endIt]
28
n = isHappy ' Replace n with the new number formed from digits.
31
next i
 
[endIt]
99999999999901
end function</lang>
99999999999910
<pre>1 is happy
99999999999914
7 is happy
99999999999915
10 is happy
99999999999916
13 is happy
99999999999937
19 is happy
99999999999941
23 is happy
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973
</pre>
 
===Simpler Alternative===
{{trans|Python}}
<syntaxhighlight lang="ruby">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) }</syntaxhighlight>
{{out}}
<pre>
1
7
10
13
19
23
28
31
 
99999999999901
99999999999910
99999999999914
99999999999915
99999999999916
99999999999937
99999999999941
99999999999951
99999999999956
99999999999961
99999999999965
99999999999973</pre>
 
=={{header|Rust}}==
In Rust, using a tortoise/hare cycle detection algorithm (generic for integer types)
<syntaxhighlight lang="rust">#![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)
}</syntaxhighlight>
{{out}}
<pre>
[1, 7, 10, 13, 19, 23, 28, 31]
</pre>
 
=={{header|Salmon}}==
<langsyntaxhighlight Salmonlang="salmon">variable happy_count := 0;
outer:
iterate(x; [1...+oo])
Line 2,905 ⟶ 6,863:
now := new;
};
};</langsyntaxhighlight>
This Salmon program produces the following output:
<pre>1 is happy.
Line 2,917 ⟶ 6,875:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">scala> def isHappy(n: Int) = {
| new Iterator[Int] {
| val seen = scala.collection.mutable.Set[Int]()
Line 2,941 ⟶ 6,899:
28
31
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (number->list num)
(do ((num num (quotient num 10))
(lst '() (cons (remainder num 10) lst)))
Line 2,960 ⟶ 6,918:
(cond ((= more 0) (newline))
((happy? n) (display " ") (display n) (loop (+ n 1) (- more 1)))
(else (loop (+ n 1) more))))</langsyntaxhighlight>
The output is:
<pre>happy numbers: 1 7 10 13 19 23 28 31</pre>
 
=={{header|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)
 
=={{header|Seed7}}==
<syntaxhighlight lang="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;</syntaxhighlight>
 
Output:
<pre>
1
7
10
13
19
23
28
31
32
44
49
</pre>
 
=={{header|SequenceL}}==
<syntaxhighlight lang="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]);</syntaxhighlight>
 
{{out}}
<pre>
$>happy.exe 8
[1,7,10,13,19,23,28,31]
</pre>
 
=={{header|SETL}}==
<langsyntaxhighlight SETLlang="setl">proc is_happy(n);
s := [n];
while n > 1 loop
Line 2,974 ⟶ 7,033:
end while;
return true;
end proc;</langsyntaxhighlight>
<langsyntaxhighlight SETLlang="setl">happy := [];
n := 1;
until #happy = 8 loop
Line 2,982 ⟶ 7,041:
end loop;
 
print(happy);</langsyntaxhighlight>
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
Alternative version:
<langsyntaxhighlight SETLlang="setl">print([n : n in [1..100] | is_happy(n)](1..8));</langsyntaxhighlight>
Output:
<pre>[1 7 10 13 19 23 28 31]</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">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)</syntaxhighlight>
 
{{out}}
<pre>
[1, 7, 10, 13, 19, 23, 28, 31]
</pre>
 
=={{header|Smalltalk}}==
Line 2,994 ⟶ 7,071:
{{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.
<langsyntaxhighlight lang="smalltalk">Object subclass: HappyNumber [
|cache negativeCache|
HappyNumber class >> new [ |me|
Line 3,056 ⟶ 7,133:
]
]
].</langsyntaxhighlight>
<langsyntaxhighlight lang="smalltalk">|happy|
happy := HappyNumber new.
 
Line 3,063 ⟶ 7,140:
(happy isHappy: i)
ifTrue: [ i displayNl ]
].</langsyntaxhighlight>
Output:
1
Line 3,073 ⟶ 7,150:
28
31
 
an alternative version is:
{{works with|Smalltalk/X}}
<syntaxhighlight lang="smalltalk">|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</syntaxhighlight>
Output:
OrderedCollection(1 7 10 13 19 23 28 31)
 
=={{header|Swift}}==
<syntaxhighlight lang="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++
}</syntaxhighlight>
{{out}}
<pre>
1
7
10
13
19
23
28
31</pre>
 
=={{header|Tcl}}==
using code from [[Sum of squares#Tcl]]
<langsyntaxhighlight lang="tcl">proc is_happy n {
set seen [list]
while {$n > 1 && [lsearch -exact $seen $n] == -1} {
Line 3,091 ⟶ 7,235:
incr n
}
puts "the first 8 happy numbers are: [list $happy]"</langsyntaxhighlight>
<pre>the first 8 happy numbers are: {1 7 10 13 19 23 28 31}</pre>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
SECTION check
IF (n!=1) THEN
Line 3,130 ⟶ 7,274:
DO check
ENDLOOP
ENDLOOP</langsyntaxhighlight>
Output:
<pre>
Line 3,142 ⟶ 7,286:
31 is a happy number
</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
<syntaxhighlight lang="Uiua">
HC ← /+ⁿ2≡⋕°⋕ # Happiness calc = sum of squares of digits
IH ← |2 memo⟨IH ⊙⊂.|=1⟩∊,, HC # Apply HC until seen value recurs
Happy ← ⟨0◌|∘⟩IH : [1] . # Pre-load `seen` with 1. Return start number or 0
 
# Brute force approach isn't too bad with memoisation even for high bounds.
↙8⊚>0≡Happy⇡10000
 
# But iterative approach is still much faster
NH ← |1 ⟨NH|∘⟩≠0Happy.+1 # Find next Happy number
⇌[⍥(NH.) 7 1]
</syntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
<lang bash>#!/bin/bash
{{works with|Z Shell}}
function sum_of_square_digits
<syntaxhighlight lang="bash">function sum_of_square_digits {
{
localtypeset -i n="$1" sum=0 d
while (( n )); do
local -i(( d=n%10, sum+=d*d, n=n/10 ))
let sum+=d*d
let n=n/10
done
echoprintf '%d\n' "$sum"
}
 
function is_happy? {
typeset -i n=$1
{
localtypeset -ia nseen="$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 {
typeset -i count=$1 n
{
localfor -i(( n=1; count; n+="$1" )); do
if is_happy "$n"; then
local -i n
printf '%d\n' "$n"
for (( n=0; count; n+=1 )); do
(( count -= 1 ))
if is_happy? "$n"; then
echo "$n"fi
let count-=1
fi
done
return 0
}
 
first_n_happy 8</langsyntaxhighlight>
Output:<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Ursala}}==
Line 3,198 ⟶ 7,344:
and first(p) defines a function mapping a number n to the first n
positive naturals having property p.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 3,207 ⟶ 7,353:
#cast %nL
 
main = (first happy) 8</langsyntaxhighlight>
output:
<pre><1,7,10,13,19,23,28,31></pre>
Line 3,213 ⟶ 7,359:
=={{header|Vala}}==
{{libheader|Gee}}
<langsyntaxhighlight lang="vala">using Gee;
 
/* function to sum the square of the digits */
Line 3,258 ⟶ 7,404:
stdout.printf("%d ", num);
stdout.printf("\n");
} // end main</langsyntaxhighlight>
The output is:
<pre>
Line 3,264 ⟶ 7,410:
</pre>
 
=={{header|VisualV Basic .NET(Vlang)}}==
{{trans|go}}
This version uses Linq to carry out the calculations.
<syntaxhighlight lang="v (vlang)">fn happy(h int) bool {
<lang vbnet>Module HappyNumbers
mut m := map[int]bool{}
Sub Main()
Dimmut n As Integer := 1h
for n > 1 Dim found As Integer = 0{
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('')
}</syntaxhighlight>
{{out}}
<pre>
1 7 10 13 19 23 28 31
</pre>
 
=={{header|Wren}}==
Do Until found = 8
{{trans|Go}}
If IsHappy(n) Then
<syntaxhighlight lang="wren">var happy = Fn.new { |n|
found += 1
var m = {}
Console.WriteLine("{0}: {1}", found, n)
while (n > 1) End If{
m[n] += 1true
Loopvar 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
Console.ReadLine()
var n = 1
End Sub
while (found < 8) {
if (happy.call(n)) {
System.write("%(n) ")
found = found + 1
}
n = n + 1
}
System.print()</syntaxhighlight>
 
{{out}}
Private Function IsHappy(ByVal n As Integer)
<pre>
Dim cache As New List(Of Long)()
1 7 10 13 19 23 28 31
 
</pre>
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</lang>
The output is:
<pre>1: 1
2: 7
3: 10
4: 13
5: 19
6: 23
7: 28
8: 31</pre>
 
=={{header|XPL0}}==
Line 3,312 ⟶ 7,484:
numbers.
 
<langsyntaxhighlight XPL0lang="xpl0">int List(810); \list of numbers in a cycle
int Inx; \index for List
include c:\cxpl\codes;
Line 3,356 ⟶ 7,528:
N0:= N0+1; \next starting number
until C=8; \done when 8 happy numbers have been found
]</langsyntaxhighlight>
 
Output:
Line 3,369 ⟶ 7,541:
31
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="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;
}
</syntaxhighlight>
{{Out}}
<pre>
The first 8 happy numbers are: 1 7 10 13 19 23 28 31
</pre>
=={{header|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).
{{trans|Icon and Unicon}}
<syntaxhighlight lang="zkl">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
}
}
}</syntaxhighlight>
<syntaxhighlight lang="zkl">h:=Utils.Generator(happyNumbers);
h.walk(8).println();</syntaxhighlight>
{{out}}
<pre>L(1,7,10,13,19,23,28,31)</pre>
Get the one million-th happy number. Nobody would call this quick.
<syntaxhighlight lang="zkl">Utils.Generator(happyNumbers).drop(0d1_000_000-1).next().println();</syntaxhighlight>
{{out}}<pre>7105849</pre>
73

edits