aboutsummaryrefslogtreecommitdiff
path: root/challenge-164/eric-cheung/python/ch-2.py
blob: 1f63da88b1bdc3a2358b5dadcdcf146fcaef917b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Reference:
## https://www.javatpoint.com/python-program-to-check-if-the-given-number-is-happy-number

nDiv = 10

def GetDigitSumSq(strInput):
    nSum = 0
    for nDigit in range(0, len(strInput)):
        nSum = nSum + int(strInput[nDigit]) * int(strInput[nDigit])

    return nSum

def IsHappyNumber(nInput):
    if nInput == 1:
        return True
    elif nInput == 4:
        return False
    else:
        return IsHappyNumber(GetDigitSumSq(str(nInput)))

nLoop = 1
nCount = 0
arrHappyNum = []

while nCount < 8:
    if IsHappyNumber(nLoop):
        arrHappyNum.append(nLoop)
        nCount = nCount + 1
    
    nLoop = nLoop + 1

print (arrHappyNum)