The prime P=13 reverses to the prime 31, and its square 169 reverses to 961, which is the square of 31.
Find additional examples where R(P)^2 = R(P^2).
Extra credit for any solution containing a digit larger than 3.
Notes:
. No palindromes like 101 or 10201.
. No modification of an existing solution by stuffing it with zeros, such as 113 modified to 1103 or 11003.
Using "R" to represent the reverse of P,
P R P^2 R^2
13 31 169 961
113 311 12769 96721
1021 1201 1042441 1442401
1031 1301 1062961 1692601
1103 3011 1216609 9066121 (this one does not count)
11003 30011 121066009 900660121 (this one does not count)
110221 122011 12148668841 14886684121
111211 112111 12367886521 12568876321
def isprime(n):
'''check if integer n is a prime'''
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
def revN(n):
return int(str(n)[::-1])
primes = [i for i in range(1,1000000) if isprime(i)]
ans = []
for p in primes:
revp = revN(p)
if p == revp:
continue
if revp < p:
continue
if not isprime(revp):
continue
if p**2 == revN(revp**2):
print(p, revp, p**2, revp**2)
ans.append([p, revp, p**2, revp**2])
|
Posted by Larry
on 2023-10-31 13:28:21 |