Skip to content

Commit 0a1a4df

Browse files
Merge pull request #640 from haru-02/master
happy_num
2 parents 03bdf3f + 4588e46 commit 0a1a4df

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

happy_num

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
num = 82;
13+
result = num;
14+
15+
while(result != 1 and result != 4):
16+
result = isHappyNumber(result);
17+
18+
#Happy number always ends with 1
19+
if(result == 1):
20+
print(str(num) + " is a happy number");
21+
#Unhappy number ends in a cycle of repeating numbers which contain 4
22+
elif(result == 4):
23+
print(str(num) + " is not a happy number");

0 commit comments

Comments
 (0)