aboutsummaryrefslogtreecommitdiff
path: root/challenge-167/lubos-kolouch/python/ch-1.py
blob: 09050404c9df071d867c6005125385ccba915b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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]