forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumber.py
More file actions
37 lines (31 loc) · 729 Bytes
/
HappyNumber.py
File metadata and controls
37 lines (31 loc) · 729 Bytes
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
34
35
36
37
def numSquareSum(num):
squareSum = 0;
while(num):
squareSum += (num % 10) * (num % 10);
num = int(num / 10);
return squareSum;
def isHappynumber(num):
slow = num
fast = num
while(True):
slow = numSquareSum(slow)
fast = numSquareSum(numSquareSum(fast));
if(slow != fast):
continue
else:
break
return (slow == 1)
# Driver Code
num = int(input("Enter a number: "))
if (isHappynumber(num)):
print(num , "is a Happy number")
else:
print(num , "is not a Happy number")
"""
Time Complexity: O(logN)
Space Complexity: O(logN)
Sample Input:
Enter a number: 13
Sample Output:
13 is a Happy number
"""