Skip to content

Instantly share code, notes, and snippets.

@higgsmass
Created October 9, 2020 01:54
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 higgsmass/2b1c57445714f05958ea0620c97aed47 to your computer and use it in GitHub Desktop.
Save higgsmass/2b1c57445714f05958ea0620c97aed47 to your computer and use it in GitHub Desktop.
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