Skip to content

Instantly share code, notes, and snippets.

@higgsmass

higgsmass/pal_cands_01.py Secret

Created Oct 9, 2020
Embed
What would you like to do?
Palindrome Test With Candidates
import functools
import time
def timer(func):
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):
tic = time.perf_counter()
value = func(*args, **kwargs)
toc = time.perf_counter()
elapsed_time = (toc - tic)*1e6
print(f"{elapsed_time:0.6f}")
return value
return wrapper_timer
def paltest(p):
P = str(p*(p+1))
if P != P[::-1]:
return False
Q = str( (p+22)*(p+23) )
if Q == Q[::-1]:
return True
def candidates():
cands = []
for m in range (5,10):
## 4mn7, p+1 odd
n = m + 7 - 4
if n < 10:
p = 4*10**3 + m*10**2 + 10*n + 6
cands.append(p)
## 4mn1, p odd
n = m + 1 - 4
if n < 10:
p = 4*10**3 + m*10**2 + 10*n + 1
cands.append(p)
for s in range (0, 5):
## 5st6, p+1 odd
t = s + 7 - 5
if t < 10:
p = 5*10**3 + s*10**2 + 10*t + 6
cands.append(p)
## 5st1, p odd
t = s + 1 - 5
if t < 0:
t += 11
if t < 10:
p = 5*10**3 + s*10**2 + 10*t + 1
cands.append(p)
return cands
@timer
def run():
print ( [p for p in candidates() if paltest(p)] )
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment