本题只需要按规则模拟即可。
生成显示每个数字所需要的能量,以及不同数字间转换时所需要的能量。
再生成给定范围内的素数及其对应的数跟序列分别计算双方的能量,最终结果是$13625242$。
注:以下代码为Python 3,且打印了进度信息。
注2:本人使用的Python 3版本是3.9,使用3.10及其以后版本的可以在第15行改为直接使用i^j.bit_count()函数。
target=20000000
d=[0]*target
n=2
while n < target:
for i in range(n*n,target,n):
d[i]=d[i]+1
n=n+1
while n < target and d[n]>0:
n=n+1
print("prime sieve ready")
switchs=[6,2,5,5,4,5,6,4,7,6]
s=[int(i,2) for i in ["1110111","0010010","1011101","1011011","0111010","1101011","1101111","1110010","1111111","1111011"]]
transitions=[[sum(k=="1" for k in format(i^j,"b")) for j in s] for i in s]
digitRoot=lambda x:sum(int(i) for i in str(x))
onOff=lambda x:sum(switchs[int(i)] for i in str(x))
sam=marks=0
for p in range(10000000,target):
if d[p]==0:
q=[p]
while digitRoot(q[-1])!=q[-1]:
q.append(digitRoot(q[-1]))
sam+=sum(onOff(r)*2 for r in q)
marks+=onOff(q[0])+onOff(q[-1])
for i in range(len(q)-1):
a=[int(j) for j in str(q[i])][::-1]
b=[int(j) for j in str(q[i+1])][::-1]
marks+=sum(transitions[a[j]][b[j]] if j < len(b) else switchs[a[j]] for j in range(len(a)))
if (p-10000000)%100000==0:print((p-10000000)//100000,"percent completed")
print(sam-marks)
|