-
Notifications
You must be signed in to change notification settings - Fork 1
/
next_happy_number.py
50 lines (40 loc) · 957 Bytes
/
next_happy_number.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Next Happy Number
Problem Link: https://practice.geeksforgeeks.org/problems/next-happy-number/0
Author: Shyam Kumar
Date: 12-09-2019
"""
def digit_square_sum(n):
sum = 0
while n > 0:
rem = n % 10
sum += rem*rem
n = n // 10
return sum
def next_happy_number(n):
"""Max 100 times the value of sum is calculated and
checked, if the value of sum is not found to be 1
then it is considered as NOT HAPPY VALUE """
for _ in range(100):
dss = digit_square_sum(n)
if dss == 1:
return True
n = dss
""" If after 100 times, the value of sum is never found
to be 1, so we considering this as limit and hence
the number is not happy number """
return False
def main():
t = int(input())
while(t>0):
n = int(input())
n+=1
while True:
ans=next_happy_number(n)
if ans == True:
break
n+=1
print(n)
t-=1
if __name__ == "__main__":
main()