1 Star 0 Fork 0

fanyangchu/PythonCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
happy_num 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
Ghulam Mohiyuddin 提交于 2019-10-21 17:29 . update happy_num
#Way2 1:
#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0;
#Calculates the sum of squares of digits
while(num > 0):
rem = num%10;
sum = sum + (rem*rem);
num = num//10;
return sum;
num = 82;
result = num;
while(result != 1 and result != 4):
result = isHappyNumber(result);
#Happy number always ends with 1
if(result == 1):
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 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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fanych/pythoncode.git
git@gitee.com:fanych/pythoncode.git
fanych
pythoncode
PythonCode
master

搜索帮助