diff --git a/README.markdown b/README.markdown index 50a4517..3dabbed 100644 --- a/README.markdown +++ b/README.markdown @@ -46,6 +46,7 @@ Should be used in order to help future reuse of code :) 34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec - 16 sec
35 - How many circular primes are there below one million?
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933
+37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min
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
@@ -58,7 +59,6 @@ Should be used in order to help future reuse of code :) 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
33 - Discover all the fractions with an unorthodox cancelling method.
-37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left.
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?
41 - What is the largest n-digit pandigital prime that exists?
97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1. diff --git a/e_37.py b/e_37.py index 2051f62..5b98f82 100644 --- a/e_37.py +++ b/e_37.py @@ -11,7 +11,59 @@ Find the sum of the only eleven primes that are both truncatable from left to ri NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. ''' +import pickle + +plist = pickle.load(open("primes_list.dup", "rb")) + +def is_prime(val): + """ + Returns True if the number is prime + """ + return (val in plist) + +def trunc_left(val): + """ + Returns true if the prime number is truncatable from left to right + """ + if not is_prime(val): + return False + if len(str(val)) == 1: + return True + else: + return trunc_left(int(str(val)[1:])) + +def trunc_right(val): + """ + Returns true if the prime number is truncatable from left to right + """ + if not is_prime(val): + return False + if len(str(val)) == 1: + return True + else: + return trunc_right(int(str(val)[:-1])) + +def trunc_primes(): + """ + Returns the complete list of primes that are both truncatable from left to right and right to left + """ + # there we already have to 1 million. + inplist = list(plist) + out_list = [] + + for p in inplist: + if p > 11: + if (trunc_left(p) and trunc_right(p)): + print "Found one : %d" %(p) + out_list.append(p) + + if len(out_list) > 10 : #already found them all + return out_list + + print "End of the line. Should go higher than one million !" + return out_list if __name__ == '__main__': - print 1 - #print "Answer : %d " % (last_ten()) \ No newline at end of file + # We know that there are only 11 of them. Lets try to find them + print trunc_primes() + print "Answer : %d " % (sum(trunc_primes())) \ No newline at end of file