Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Happy-numbers/Harbour/happy-numbers.harbour
Normal file
33
Task/Happy-numbers/Harbour/happy-numbers.harbour
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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 )
|
||||
Loading…
Add table
Add a link
Reference in a new issue