Was looking at problem 442 in project Euler and actually I am kind of stuck with a different problem. So no spoilers here. What I am doing is the following. I am using this code to count occurrences of power of eleven in numbers (code is not optimized but it works, so forgive missing optimization)
#!/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 "------------------------------------------------------"
Now this produce an output that looks like this (I shortened it)
======================================================
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
------------------------------------------------------
Now I managed to find a recursive relationship between the number of occurrences of the number 11 (let's call it N_{11}(n)$
) up to n
. That being
N_11(n)=9*N_11(n-1)+10^(n-2)+9*N_11(n-2)
Now somehow I don't seem to be able to find the same relationship for the other powers (although I think it should be similar). Anyone willing to help? Would be greatly appreciated... Is a matter of principle :-)
Thanks in advance, Umberto