We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e224532 commit 09c6a3fCopy full SHA for 09c6a3f
1 file changed
happynumber/happynumber-py
@@ -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