Saturday, January 05, 2008

Project Euler 40

One more :)

This one was really easy. Also learnt a thing or two about string concatenations in python when my original techinique of k += str(someInt) was taking way to long as the length of the string increased.

Apparently when wanting to a large number of concatenation, its fast to append to an array and then do a join.

Problem #4o states:-
An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

Solution:-

import time
w = time.time()
d = ''.join([`num` for num in xrange(1,190000)])
print int(d[0])*int(d[9])*int(d[99])*int(d[999])*int(d[9999])*int(d[99999])*int(d[999999])
print time.time() - w