Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2014 10:19
Show Gist options
  • Save anonymous/c45613220bf5036362ea to your computer and use it in GitHub Desktop.
Save anonymous/c45613220bf5036362ea to your computer and use it in GitHub Desktop.
Finds bidirectionally-truncatable primes
def mark(sieve, x):
for i in xrange(2*x, len(sieve), x):
sieve[i] = False
sieve = [True] * 1000001
sieve[0], sieve[1] = False, False
for x in xrange(2, int(len(sieve) ** 0.5) + 1):
if sieve[x]:
mark(sieve, x)
def check_rtl(n):
while n > 0:
if sieve[n]:
n = n/10
else:
return False
return True
def check_ltr(n):
place = len(str(n)) - 1
while n > 0:
if sieve[n]:
n = n % 10**place
place -= 1
else:
return False
return True
counter = 0
i = 11 # smallest two-digit prime
while counter < 11: # total number of bi-truncatable primes
if check_rtl(i) and check_ltr(i):
print(i)
counter += 1
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment