Problem #40

Solved another Project Euler prob­lem today.  From the site:

An irra­tional dec­i­mal frac­tion is cre­ated by con­cate­nat­ing the pos­i­tive integers:


It can be seen that the 12^(th) digit of the frac­tional part is 1.

If d_(n) rep­re­sents the n^(th) digit of the frac­tional part, find the value of the fol­low­ing expression.

d_(1) × d_(10) × d_(100) × d_(1000) × d_(10000) × d_(100000) × d_(1000000

My orig­i­nal inten­tions were to fig­ure out an ele­gant for­mula for deriv­ing dn given some n. I thought about it for a few min­utes and fig­ured it’d be inter­est­ing to see if the straight brute force solu­tion was quick enough. So I quickly coded it up in python, and sure enough, it’s sub-second and there­fore eas­ily quick enough.

[email protected]:~/proj_euler$ time python

real 0m0.906s
user 0m0.724s
sys 0m0.044s


list =””

for n in range(1,1000001):
    list += str(n)

answers = [list[0], list[9], list[99], list[999], list[9999], list[99999], list[999999]]
prod = 1
for n in answers:
    prod = prod * int(n)

print prod

Leave a Reply