|
| 1 | +#isHappyNumber() will determine whether a number is happy or not |
| 2 | +def isHappyNumber(num): |
| 3 | + rem = sum = 0; |
| 4 | + |
| 5 | + #Calculates the sum of squares of digits |
| 6 | + while(num > 0): |
| 7 | + rem = num%10; |
| 8 | + sum = sum + (rem*rem); |
| 9 | + num = num//10; |
| 10 | + return sum; |
| 11 | + |
| 12 | +#Displays all happy numbers between 1 and 100 |
| 13 | +print("List of happy numbers between 1 and 100: "); |
| 14 | +for i in range(1, 101): |
| 15 | + result = i; |
| 16 | + |
| 17 | + #Happy number always ends with 1 and |
| 18 | + #unhappy number ends in a cycle of repeating numbers which contains 4 |
| 19 | + while(result != 1 and result != 4): |
| 20 | + result = isHappyNumber(result); |
| 21 | + |
| 22 | + if(result == 1): |
| 23 | + print(i), |
| 24 | + print(" "), |
0 commit comments