From a9364c97a3d9d5637eccae168bd60914f0a77d26 Mon Sep 17 00:00:00 2001 From: Kshitij Saraogi Date: Wed, 22 Nov 2023 09:00:44 +0100 Subject: [PATCH] feat: Implement A065722 Paterson Primes (#391) --- oeis.py | 23 +++++++++++++++ tests/test_A065722.py | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/test_A065722.py diff --git a/oeis.py b/oeis.py index d757e35..e74422a 100644 --- a/oeis.py +++ b/oeis.py @@ -97,6 +97,11 @@ def __getitem__(self, key: Union[int, slice]) -> Union[int, Sequence[int]]: """Return a slice or a value from an integer sequence.""" raise NotImplementedError + def __iter__(self) -> Iterator[int]: + """Iterate over the integer sequence.""" + for i in count(self.offset): + yield self[i] + class IntegerSequenceFromGenerator(IntegerSequence): """IntegerSequence based on a generator. @@ -752,6 +757,24 @@ def A002182() -> Iterable[int]: yield n +@oeis.from_generator(offset=0) +def A065722() -> Iterable[int]: + """Primes that when written in base 4, then reinterpreted in base 10, again give primes.""" + for p in A000040: # pylint: disable=not-an-iterable + # Refer: https://github.com/pylint-dev/pylint/issues/9251 + if _is_patterson_prime(p): + yield p + + +def _is_patterson_prime(n): + import numpy as np + from sympy.ntheory import isprime + + base_four_repr = np.base_repr(n, base=4) + base_ten_repr = int(base_four_repr) + return isprime(base_ten_repr) + + def main() -> None: # pylint: disable=too-many-branches """Command line entry point.""" args = parse_args() diff --git a/tests/test_A065722.py b/tests/test_A065722.py new file mode 100644 index 0000000..ab6cb80 --- /dev/null +++ b/tests/test_A065722.py @@ -0,0 +1,65 @@ +from oeis import A065722 + + +def test_A065722(): + from_oeis_org = [ + 2, + 3, + 5, + 7, + 11, + 13, + 17, + 19, + 23, + 29, + 37, + 43, + 47, + 53, + 61, + 71, + 73, + 79, + 83, + 97, + 103, + 107, + 109, + 113, + 131, + 149, + 151, + 157, + 163, + 167, + 181, + 191, + 193, + 197, + 227, + 233, + 241, + 251, + 277, + 293, + 307, + 311, + 313, + 317, + 349, + 359, + 373, + 389, + 401, + 419, + 421, + 433, + 443, + 449, + 463, + 467, + 503, + ] + + assert A065722[: len(from_oeis_org)] == from_oeis_org