python 计算 happy number

第一篇贡献给python计算happy number~

 

def cal(n):
    s=str(n)
    sum=0
    for i in range(len(s)):
        sum+=int(s[i])**2
    return sum

a=77   # 13 139 ok; 4 5 6 not
# print (cal(a))
def itr(a):
    log_set=set([a])
    while a!=1:
        temp = cal(a)
        if temp in log_set:
            return False # False but not false
        else:
            log_set.add(temp)
            a=temp # lost as testing online
    return True

print ("itr(a): ",itr(a))

 

python 计算 happy number

上一篇:「docker实战篇」python的docker爬虫技术-移动自动化控制工具appium工具(17)


下一篇:flask 报错“ You did not provide the "FLASK_APP" environment variable”