diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-25 09:29:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-25 09:29:21 +0100 |
| commit | 261da1d2c54ab8faab88537076b4248d5abd00f7 (patch) | |
| tree | c60a0327997c7d9759f275323ae120d735726784 /challenge-147/paulo-custodio/python/ch-1.py | |
| parent | 9e56f497ae79225e5a6e041a963741885335d0d0 (diff) | |
| parent | 206c2fce8db1de9b7f82f04a3276005a284b3c40 (diff) | |
| download | perlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.tar.gz perlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.tar.bz2 perlweeklychallenge-club-261da1d2c54ab8faab88537076b4248d5abd00f7.zip | |
Merge pull request #10906 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-147/paulo-custodio/python/ch-1.py')
| -rw-r--r-- | challenge-147/paulo-custodio/python/ch-1.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-147/paulo-custodio/python/ch-1.py b/challenge-147/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..f3982a0174 --- /dev/null +++ b/challenge-147/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +# Challenge 147 +# +# TASK #1 > Truncatable Prime +# Submitted by: Mohammad S Anwar +# Write a script to generate first 20 left-truncatable prime numbers in base 10. +# +# In number theory, a left-truncatable prime is a prime number which, in a given +# base, contains no 0, and if the leading left digit is successively removed, +# then all resulting numbers are primes. +# +# Example +# 9137 is one such left-truncatable prime since 9137, 137, 37 and 7 are all +# prime numbers. + +from sympy import isprime, nextprime + +def left_truncatable_prime_it(): + prime = None + while True: + prime = nextprime(prime) if prime is not None else 2 + if is_left_truncatable_prime(prime): + yield prime + +def is_left_truncatable_prime(p): + while True: + if not isprime(p): + return False + p = int(str(p)[1:]) if len(str(p)) > 1 else '' + if p == '': + return True + +it = left_truncatable_prime_it() +out = [next(it) for _ in range(20)] +print(", ".join(map(str, out))) |
