Skip to content

Commit

Permalink
update happy_num
Browse files Browse the repository at this point in the history
Another way to do this and code is also less
  • Loading branch information
subahanii committed Oct 21, 2019
1 parent 0a1a4df commit d242e81
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions happy_num
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Way2 1:

#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0;
Expand All @@ -17,7 +19,27 @@ while(result != 1 and result != 4):

#Happy number always ends with 1
if(result == 1):
print(str(num) + " is a happy number");
print(str(num) + " is a happy number after apply way 1");
#Unhappy number ends in a cycle of repeating numbers which contain 4
elif(result == 4):
print(str(num) + " is not a happy number");
print(str(num) + " is not a happy number after apply way 1");





#way 2:

#Another way to do this and code is also less
n=num
setData=set() #set datastructure for checking a number is repeated or not.
while 1:
if n==1:
print("{} is a happy number after apply way 2".format(num))
break
if n in setData:
print("{} is Not a happy number after apply way 2".format(num))
break
else:
setData.add(n) #adding into set if not inside set
n=int(''.join(str(sum([int(i)**2 for i in str(n)])))) #Pythonic way

0 comments on commit d242e81

Please sign in to comment.