Skip to content

Commit 09c6a3f

Browse files
authored
Create happynumber-py
1 parent e224532 commit 09c6a3f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

happynumber/happynumber-py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def get_digit_square_sum(num: int) -> int:
2+
return sum(int(d) ** 2 for d in str(num))
3+
4+
def is_happy_number(num: int) -> bool:
5+
slow, fast = num, num
6+
while True:
7+
slow = get_digit_square_sum(slow) # 1 step
8+
fast = get_digit_square_sum(get_digit_square_sum(fast)) # 2 steps
9+
if slow == fast:
10+
break
11+
return slow == 1
12+
13+
if __name__ == "__main__":
14+
n = int(input("Enter a number: "))
15+
if is_happy_number(n):
16+
print(f"{n} is a Happy Number ")
17+
else:
18+
print(f"{n} is NOT a Happy Number ")

0 commit comments

Comments
 (0)