Happy numbers: Difference between revisions

consolidate BASICs
(consolidate BASICs)
Line 744:
<syntaxhighlight lang="applescript">{1, 7, 10, 13, 19, 23, 28, 31}</syntaxhighlight>
 
=={{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|Arturo}}==
 
Line 1,004 ⟶ 991:
}</syntaxhighlight>
 
=={{header|BASIC256BASIC}}==
==={{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:"
Line 1,031 ⟶ 1,032:
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|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|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|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
Line 1,170 ⟶ 1,704:
ERROR: Maximum integer value reached
</pre>
 
=={{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|BCPL}}==
Line 2,814 ⟶ 3,315:
28
31</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|Frege}}==
Line 2,901 ⟶ 3,344:
1 7 10 13 19 23 28 31
runtime 0.614 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>
 
Line 3,606 ⟶ 4,003:
Output:
<syntaxhighlight lang="lasso">1, 7, 10, 13, 19, 23, 28, 31</syntaxhighlight>
 
=={{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|Logo}}==
Line 5,479 ⟶ 5,815:
<syntaxhighlight lang="prolog"> ?- happy_numbers(L, 8).
L = [1,7,10,13,19,23,28,31].</syntaxhighlight>
 
=={{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|Python}}==
Line 6,165 ⟶ 6,456:
99999999999965
99999999999973</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|Rust}}==
Line 6,691 ⟶ 6,952:
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|UNIX Shell}}==
Line 6,865 ⟶ 7,060:
1 7 10 13 19 23 28 31
</pre>
 
=={{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|Vlang}}==
Line 7,235 ⟶ 7,255:
<syntaxhighlight lang="zkl">Utils.Generator(happyNumbers).drop(0d1_000_000-1).next().println();</syntaxhighlight>
{{out}}<pre>7105849</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>
1,480

edits