我通过 Twitter 从 centerofmath.org发现了这个谜题:
有一个十位数字,其中最左边的数字也是数字中零的个数,最左边的第二个数字是一的个数,依此类推,直到最后一个数字(或最右边的数字)是该数字中的九个数. 这个数字是多少,它是独一无二的吗?
我用 Python 写了一个程序来解决这个难题:
def yielder(exponent):
i = 10**exponent
while i < (10**(exponent+1))-1:
yield i
i += 1
def is_solution(num):
l = list(num)
#l.reverse()
is_sol = True
for i, e in enumerate(l):
if int(e) != l.count(str(i)):
is_sol = False
break
return is_sol
if __name__=="__main__":
import sys
ofile = open('solution.txt', 'w')
j = int(sys.argv[1])
print("checking between {0} and {1}".format(10**j, (10**(j+1))-1))
ofile.write("Solutions between {0} and {1}:\n".format(10**j, (10**(j+1))-1))
for x in yielder(j):
if is_solution(str(x)):
ofile.write('\t{0}\n'.format(x))
print("solution is {0}".format(x))
ofile.close()
我得到的解决方案是6210001000
但问题是需要几个小时才能解决。我想知道是否有一些我可以用来让它更快的技术
顺便说一句,这些是 10 9,999,999,999 之间的数字,它们是解决方案
Solutions between 10 and 99:
Solutions between 100 and 999:
Solutions between 1000 and 9999:
1210
2020
Solutions between 10000 and 99999:
21200
Solutions between 100000 and 999999:
Solutions between 1000000 and 9999999:
3211000
Solutions between 10000000 and 99999999:
42101000
Solutions between 100000000 and 999999999:
521001000
Solutions between 1000000000 and 9999999999:
6210001000