Solves Problem 40 of Project Euler

Dumbest solution ever, but so fast to implement.
And finally runs in less than 2 seconds on my Netbook.

I really wonder even why search for optimization.

Python int to str conversion is so useful for those problems.
This commit is contained in:
Julien Lengrand-Lambert
2012-10-16 23:25:29 +02:00
parent 57353d2d26
commit 957ede8215
2 changed files with 35 additions and 5 deletions

View File

@@ -56,6 +56,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min <br />
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec <br />
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min<br />
40 - Finding the nth digit of the fractional part of the irrational number. > 2 sec <br />
41 - What is the largest n-digit pandigital prime that exists? < 5 min <br />
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
@@ -68,7 +69,6 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
## In progress:
40 - Finding the nth digit of the fractional part of the irrational number. <br />
43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property. <br />
47 - Find the first four consecutive integers to have four distinct primes factors. <br />
97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br />

38
e_40.py
View File

@@ -12,11 +12,41 @@
It can be seen that the 12th digit of the fractional part is 1.
If d_n represents the nth digit of the fractional part, find the value of the following expression.
d_1 d_10 d_100 d_1000 d_10000 d_100000 d_1000000
d_1 * d_10 * d_100 * d_1000 * d_10000 * d_100000 * d_1000000
This first version is going to be over dumb. We are actually going to create the fractional part.
We first need to know how much elements we need to sum.
##---
"""
if __name__ == '__main__':
print 1
#print "Answer : %d " % (last_ten())
def el_to_go(max_val=1000000):
"""
Returns the maximum number of the range we are going to need to be able to get d_1000000
"""
return 1000000 # max index is 100000. Dumbest limit is thus 1000000
def get_index(myfrac, el):
"""
Given the string representation of the fractional part, return the elth element
"""
return int(myfrac[el - 1])
def sum_elements():
"""
Returns the value of the following expression.
d_1 d_10 d_100 d_1000 d_10000 d_100000 d_1000000
"""
els_to_go = [1, 10, 100, 1000, 10000, 100000, 1000000]
max_el = el_to_go()
sum_res = 1
frac = ''.join(map(str, range(1, max_el + 1)))
for el in els_to_go:
sum_res *= get_index(frac, el)
return sum_res
if __name__ == '__main__':
print "Answer : %d " % (sum_elements())