Discuss / Python / 数学黑洞:神奇的6174

数学黑洞:神奇的6174

Topic source
# -*- coding: utf-8 -*-

from functools import reduce

x = str(input("请输入四位数或者三位数的数字: "))
times = 1
while True:

    max_math = sorted(x,reverse=True)
    min_math = sorted(x)
    max_math_switch_str = int(reduce(lambda x,y:x+y,max_math))
    min_math_switch_str = int(reduce(lambda x,y:x+y,min_math))

    s = max_math_switch_str-min_math_switch_str
    print("now",times,max_math_switch_str,"-",min_math_switch_str,"=",s)

    if s == 6174:
        break
    if s == 495:
        break

    x = str(s)
    times += 1
  • 注:6174是黑洞数(亦称为卡布列克常数,任何非四位相同数字,只要将数字重新排列,组合成最大的值和最小的数再加以相减,重复以上步骤,七次以内就会出现6174。例如:8045,8540-0458=8082,8820-0288=8532,8532-2358=6174。还有一个数字是495)

  • 1

Reply