在项目Euler中查看问题442,实际上我遇到了一个不同的问题。所以这里没有破坏者。我正在做的是以下内容。我正在使用此代码来计算数字中11个幂的出现次数(代码未经优化,但它有效,因此原谅缺少优化)
#!/usr/bin/python
for ii in xrange (1,8):
count1=0
count2=0
count3=0
count4=0
count5=0
count6=0
count7=0
#for jj in xrange ((10**ii), (10**(ii+1))):
for jj in xrange (1, (10**(ii+1))):
found = False
a="11"
jjs=str(jj)
if (a in jjs):
count1=count1+1
found = True
a="121"
if (a in jjs and not found):
count2=count2+1
found = True
a="1331"
if (a in jjs and not found):
count3=count3+1
found = True
a="14641"
if (a in jjs and not found):
count4=count4+1
found = True
a="161051"
if (a in jjs and not found):
count5=count5+1
found = True
a="1771561"
if (a in jjs and not found):
count6=count6+1
found = True
a="19487171"
if (a in jjs and not found):
count7=count7+1
found = True
print "======================================================"
print "1 <-> 10^",ii+1, " - ",10**ii, " - ", 10**(ii+1)
print "======================================================"
print "11 ", count1
print "121 ", count2
print "1331 ", count3
print "14641 ", count4
print "161051 ", count5
print "1771561 ", count6
print "19487171 ", count7
print "Sum ", count1+count2+count3+count4+count5
print "------------------------------------------------------"
现在生成一个看起来像这样的输出(我缩短了它)
======================================================
1 <-> 10^ 2 - 10 - 100
======================================================
11 1
121 0
1331 0
14641 0
161051 0
1771561 0
19487171 0
Sum 1
------------------------------------------------------
======================================================
1 <-> 10^ 3 - 100 - 1000
======================================================
11 19
121 1
1331 0
14641 0
161051 0
1771561 0
19487171 0
Sum 20
------------------------------------------------------
======================================================
1 <-> 10^ 4 - 1000 - 10000
======================================================
11 280
121 18
1331 1
14641 0
161051 0
1771561 0
19487171 0
Sum 299
------------------------------------------------------
======================================================
1 <-> 10^ 5 - 10000 - 100000
======================================================
11 3691
121 260
1331 18
14641 1
161051 0
1771561 0
19487171 0
Sum 3970
------------------------------------------------------
现在,我设法找到了号码11的出现次数(让我们称之为N_{11}(n)$
)到n
之间的递归关系。那是
N_11(n)=9*N_11(n-1)+10^(n-2)+9*N_11(n-2)
现在不知怎的,我似乎无法为其他权力找到相同的关系(虽然我认为它应该是相似的)。有人愿意帮忙吗?将不胜感激......原则问题: - )
提前致谢, 翁