Created
June 12, 2013 11:14
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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