Skip to content

Instantly share code, notes, and snippets.

@pukkandan
Last active August 14, 2022 12:35
Show Gist options
  • Save pukkandan/d2853c687d020bc7c41df379dea1c813 to your computer and use it in GitHub Desktop.
Save pukkandan/d2853c687d020bc7c41df379dea1c813 to your computer and use it in GitHub Desktop.
Get a tree of all Right Truncatable Primes
import math
def treePrint(l, sp=''):
if isinstance(l, list):
for i in l: treePrint(i, ' ' + sp)
else:
print(sp + str(l))
def isPrime(n):
n = abs(n)
if n < 2: return False
if not n % 2: return n == 2
for i in range(3, math.floor(math.sqrt(n)), 2):
if n % i: return False
return True
def getAllRightTruncatablePrimesFrom(n):
test = [10 * n + i for i in (1, 3, 7, 9)]
return [n, [getAllRightTruncatablePrimesFrom(i) for i in test if isPrime(i)]]
def getAllRightTruncatablePrimes():
return [getAllRightTruncatablePrimesFrom(x) for x in (2, 3, 5, 7)]
if __name__ == '__main__':
treePrint(getAllRightTruncatablePrimes())
import timeit
n = 100
print(f'\n\n\nTime = {timeit.timeit(getAllRightTruncatablePrimes, number=n) / n}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment