From 0e1f5b17707b5151487904b00ea69b3ba3b1e961 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Thu, 9 Jun 2022 21:15:40 +0200 Subject: feat(challenge-168/lubos-kolouch/p[erl,ython]/ch-1.p[ly]): Challenge 168 Task 1 LK Perl Python --- challenge-168/lubos-kolouch/python/ch-1.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-168/lubos-kolouch/python/ch-1.py (limited to 'challenge-168/lubos-kolouch/python/ch-1.py') diff --git a/challenge-168/lubos-kolouch/python/ch-1.py b/challenge-168/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..8c8790c5dc --- /dev/null +++ b/challenge-168/lubos-kolouch/python/ch-1.py @@ -0,0 +1,39 @@ +""" Challenge 168 Task 1 LK Python """ + +from sympy import isprime + + +def generate_perrin_primes(n: int) -> list: + """ + Generate all primes up to n using the Perrin sequence. + """ + perrin_sequence = [3, 0, 2] + + perrin_primes: dict = {} + + while len(perrin_primes.keys()) < n: + + next_number = perrin_sequence[-3] + perrin_sequence[-2] + perrin_sequence.append(next_number) + + if isprime(next_number): + perrin_primes[next_number] = True + + return sorted(perrin_primes.keys()) + + +assert generate_perrin_primes(13) == [ + 2, + 3, + 5, + 7, + 17, + 29, + 277, + 367, + 853, + 14197, + 43721, + 1442968193, + 792606555396977, +] -- cgit