如何使用 Python 查找 Keith 数字?
pythonprogramming
您可以使用以下代码在 Python − 中查找数字是否为 Keith 数字
示例
def is_keith_number(n): # 首先获取所有数字的数组然后将它们相加,从而找到数字之和 c = str(n) a = list(map(int, c)) b = sum(a) # 现在检查数字是否为 Keith 数字 # 例如,14 是 Keith 数字,因为: # 1+4 = 5 # 4+5 = 9 # 5+9 = 14 while b < n: a = a[1:] + [b] b = sum(a) return (b == n) & (len(c) > 1) print(is_keith_number(14))
输出
将给出输出 −
True