mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Solves Problem 37 of Project Euler
Could be refactored a bit to avoid code redondance. Uses my list of primes numbers Performs calculations in less than one minute.
This commit is contained in:
@@ -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 <br />
|
||||
35 - How many circular primes are there below one million? <br />
|
||||
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br />
|
||||
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min <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 />
|
||||
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 />
|
||||
@@ -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. <br />
|
||||
33 - Discover all the fractions with an unorthodox cancelling method. <br />
|
||||
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. <br />
|
||||
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? <br />
|
||||
41 - What is the largest n-digit pandigital prime that exists? <br />
|
||||
97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1.
|
||||
|
||||
56
e_37.py
56
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())
|
||||
# We know that there are only 11 of them. Lets try to find them
|
||||
print trunc_primes()
|
||||
print "Answer : %d " % (sum(trunc_primes()))
|
||||
Reference in New Issue
Block a user