I've been trying to solve Project Euler Problem 37 (While the spirit of the Euler Problems is independent solving, I've legitimately given this my best shot, and think the bug is probably syntactical). Anyway, the following is my code (btw, I am happy to hear any suggestions on general improvements as well):
# 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.
primes = [2, 3, 5, 7, 11, 13]
trun_primes = []
# Tests whether number "n" is a trucatable prime, doesn't test whether "n" is prime itself
def test_trun(n):
trun_versions = []
for d in range(1, len(str(n))):
trun_versions.append(int(str(n)[:len(str(n)) - d]))
for d in range(1, len(str(n))):
trun_versions.append(int(str(n)[d:]))
test = 0
for v in trun_versions:
if v not in primes:
test += 1
if test == 0:
return True
else:
return False
n = primes[len(primes) - 1] + 2
while len(trun_primes) != 11:
ptest = []
for p in primes:
ptest.append(n % p)
if 0 in ptest:
n += 2
else:
primes.append(n)
if test_trun(n) == True:
trun_primes.append(n)
# Debugging tool:
print trun_primes
n += 2
print sum(trun_primes)
Every time I run it, the results are the same (except for runtime, I choose to end it): @JohnColeman, l32 is "ptest.append(n % p)", this issue has already been resolved, however. Atom Script Runner