Skip to content

Instantly share code, notes, and snippets.

@christianp
Created June 12, 2013 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/5764458 to your computer and use it in GitHub Desktop.
Save christianp/5764458 to your computer and use it in GitHub Desktop.
Find palindromic numbers in base 10 such that when you insert a 1 between each digit you get a prime. (Actually, start with primes, find ones that alternate any digit and 1, then find which ones are palindromes)
# palindromic numbers in base 10 such that when you insert a 1 between each digit you get a prime
import re
primes = open('primes.txt').read().split('\n')[:-1]
r = re.compile(r'^\d(1\d)+$')
def is_palindrome(n):
return ''.join(reversed(n))==n
matches = [p[::2] for p in primes if r.match(p) and is_palindrome(p)]
print(', '.join(matches))
"""
output when primes.txt goes up to 86028121:
33, 99, 131, 141, 303, 353, 737, 797, 909, 3883, 7447
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment