June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,44 @@
import extensions.
import system'collections.
import system'routines.
isHappy = (:n)<int>
[
list<int> cache := 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
].
program =
[
list<int> happynums := list<int>(8).
int num := 1.
while (happynums length < 8)
[
if (isHappy eval(num))
[
happynums append(num)
].
num += 1
].
console printLine("First 8 happy numbers: ", happynums).
].

View file

@ -0,0 +1,29 @@
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