mirror of
https://github.com/correl/euler.git
synced 2024-11-27 11:09:54 +00:00
Problem 037
This commit is contained in:
parent
7f8098ef77
commit
06f5017063
1 changed files with 35 additions and 0 deletions
35
e037.py
Normal file
35
e037.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
"""Find the sum of all eleven primes that are both truncatable from left to right and right to left.
|
||||||
|
|
||||||
|
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
|
||||||
|
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
|
||||||
|
|
||||||
|
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from e007 import is_prime, prime_generator
|
||||||
|
|
||||||
|
def is_truncatable(prime):
|
||||||
|
if prime < 10:
|
||||||
|
return False
|
||||||
|
rtl = [int(str(prime)[:i]) for i in xrange(len(str(prime))) if i > 0 and i < len(str(prime))]
|
||||||
|
ltr = [int(str(prime)[i:]) for i in xrange(len(str(prime))) if i > 0 and i < len(str(prime))]
|
||||||
|
truncated = sorted(rtl + ltr, reverse=True)
|
||||||
|
for n in truncated:
|
||||||
|
if not is_prime(n):
|
||||||
|
return False
|
||||||
|
print 'OK', prime, rtl, ltr
|
||||||
|
return True
|
||||||
|
|
||||||
|
def main():
|
||||||
|
truncatable = []
|
||||||
|
generator = prime_generator()
|
||||||
|
print 'Searching for truncatable primes...'
|
||||||
|
while len(truncatable) < 11:
|
||||||
|
prime = generator.next()
|
||||||
|
if is_truncatable(prime):
|
||||||
|
truncatable.append(prime)
|
||||||
|
print 'Found {0} truncatable primes'.format(len(truncatable))
|
||||||
|
print 'Sum:', sum(truncatable)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in a new issue