diff --git a/README.markdown b/README.markdown index fd93a0f..0f938a8 100644 --- a/README.markdown +++ b/README.markdown @@ -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
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min
+40 - Finding the nth digit of the fractional part of the irrational number. > 2 sec
41 - What is the largest n-digit pandigital prime that exists? < 5 min
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec
@@ -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.
43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property.
47 - Find the first four consecutive integers to have four distinct primes factors.
97 - Find the last ten digits of the non-Mersenne prime: 28433 � 2^7830457 + 1.
diff --git a/e_40.py b/e_40.py index 9c12f12..1447f74 100644 --- a/e_40.py +++ b/e_40.py @@ -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())