aboutsummaryrefslogtreecommitdiff
path: root/challenge-167
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2022-06-04 11:23:07 +0200
committerLubos Kolouch <lubos@kolouch.net>2022-06-04 11:23:07 +0200
commit1d38441ef24f99e0d77373550090e5db15675b19 (patch)
tree23352688e7b09e1752d401bbbd57ff9cb0a29bb1 /challenge-167
parenta678d21485714aba2af2dede9e069c8bcf001c56 (diff)
downloadperlweeklychallenge-club-1d38441ef24f99e0d77373550090e5db15675b19.tar.gz
perlweeklychallenge-club-1d38441ef24f99e0d77373550090e5db15675b19.tar.bz2
perlweeklychallenge-club-1d38441ef24f99e0d77373550090e5db15675b19.zip
feat(challenge-167/lubos-kolouch/python/ch-1.py): Challenge 167 Task 1 LK Python
Diffstat (limited to 'challenge-167')
-rw-r--r--challenge-167/lubos-kolouch/python/ch-1.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-167/lubos-kolouch/python/ch-1.py b/challenge-167/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..09050404c9
--- /dev/null
+++ b/challenge-167/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,44 @@
+""" Chalenge 167 Task 1 Python"""
+
+from sympy import isprime, nextprime
+
+
+def is_circular_prime(prime: int) -> bool:
+ """
+ Checks if a prime number is circular prime.
+ """
+ prime_str = str(prime)
+ for i in range(len(prime_str)):
+ if not isprime(int(prime_str[i:] + prime_str[:i])):
+ return False
+ return True
+
+
+def generate_primes(limit: int) -> list:
+ """
+ Generates all prime numbers below a limit.
+ """
+
+ primes: list = []
+ prime = nextprime(100)
+
+ while len(primes) < limit:
+ if is_circular_prime(prime):
+ primes.append(prime)
+
+ prime = nextprime(prime)
+
+ return primes
+
+
+assert is_circular_prime(2) == 1
+assert is_circular_prime(3) == 1
+assert is_circular_prime(11) == 1
+assert is_circular_prime(13) == 1
+assert is_circular_prime(57) == 0
+assert is_circular_prime(197) == 1
+
+# Note that the example given on the challenge is wrong according to
+# https://oeis.org/A068652
+
+assert generate_primes(10) == [113, 131, 197, 199, 311, 337, 373, 719, 733, 919]