diff --git a/c/test_euler.py b/c/test_euler.py index c14e16a9..6515a5e9 100644 --- a/c/test_euler.py +++ b/c/test_euler.py @@ -227,7 +227,7 @@ def test_compiler_macros(compiler): @mark.skipif('NO_OPTIONAL_TESTS or ONLY_SLOW') def test_is_prime(benchmark, compiler): - from p0007 import is_prime, prime_factors, primes + from lib.primes import is_prime, prime_factors, primes MAX_PRIME = 1_000_000 exename = EXE_TEMPLATE.format("test_is_prime", compiler) test_path = C_FOLDER.joinpath("tests", "test_is_prime.c") diff --git a/cplusplus/test_euler.py b/cplusplus/test_euler.py index 3b608764..32924bcc 100644 --- a/cplusplus/test_euler.py +++ b/cplusplus/test_euler.py @@ -213,7 +213,7 @@ def test_compiler_macros(compiler): # @mark.skipif('NO_OPTIONAL_TESTS or ONLY_SLOW') # def test_is_prime(benchmark, compiler): -# from p0007 import is_prime, prime_factors, primes +# from lib.primes import is_prime, prime_factors, primes # MAX_PRIME = 1_000_000 # exename = EXE_TEMPLATE.format("test_is_prime", compiler) # test_path = CPP_FOLDER.joinpath("tests", "test_is_prime.cpp") diff --git a/docs/python.rst b/docs/python.rst index 7b801bc9..4af78e93 100644 --- a/docs/python.rst +++ b/docs/python.rst @@ -5,6 +5,16 @@ Euler Python Implementation :start-line: 2 :end-before: Problems Solved +Library Code +------------ + +.. toctree:: + :glob: + :numbered: + :maxdepth: 1 + + python/primes + Problems Solved --------------- diff --git a/docs/python/fibonacci.rst b/docs/python/fibonacci.rst new file mode 100644 index 00000000..6281d00e --- /dev/null +++ b/docs/python/fibonacci.rst @@ -0,0 +1,13 @@ +fibonacci.py +============ + +View source code `here on GitHub! `_ + +.. automodule:: python.lib.fibonacci + :members: + :undoc-members: + +.. literalinclude:: ../../python/lib/fibonacci.py + :language: python + :linenos: + diff --git a/docs/python/p0003.rst b/docs/python/p0003.rst index b61989e7..8ab77dd6 100644 --- a/docs/python/p0003.rst +++ b/docs/python/p0003.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 3 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0003 :members: :undoc-members: diff --git a/docs/python/p0007.rst b/docs/python/p0007.rst index 67c87fd1..d7211d37 100644 --- a/docs/python/p0007.rst +++ b/docs/python/p0007.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 7 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0007 :members: :undoc-members: diff --git a/docs/python/p0010.rst b/docs/python/p0010.rst index 2012c010..c3c1af14 100644 --- a/docs/python/p0010.rst +++ b/docs/python/p0010.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 10 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0010 :members: :undoc-members: diff --git a/docs/python/p0021.rst b/docs/python/p0021.rst index f5666379..5e2e3e67 100644 --- a/docs/python/p0021.rst +++ b/docs/python/p0021.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 21 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0021 :members: :undoc-members: diff --git a/docs/python/p0027.rst b/docs/python/p0027.rst index 3d51523f..eac54990 100644 --- a/docs/python/p0027.rst +++ b/docs/python/p0027.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 27 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0027 :members: :undoc-members: diff --git a/docs/python/p0035.rst b/docs/python/p0035.rst index 6e8c118b..5dcba5cb 100644 --- a/docs/python/p0035.rst +++ b/docs/python/p0035.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 35 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0035 :members: :undoc-members: diff --git a/docs/python/p0037.rst b/docs/python/p0037.rst index aac3adf3..519896ec 100644 --- a/docs/python/p0037.rst +++ b/docs/python/p0037.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 37 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0037 :members: :undoc-members: diff --git a/docs/python/p0041.rst b/docs/python/p0041.rst index fc433355..66e2076c 100644 --- a/docs/python/p0041.rst +++ b/docs/python/p0041.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 41 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0041 :members: :undoc-members: diff --git a/docs/python/p0046.rst b/docs/python/p0046.rst index c2296a9c..624e2f27 100644 --- a/docs/python/p0046.rst +++ b/docs/python/p0046.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 46 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0046 :members: :undoc-members: diff --git a/docs/python/p0047.rst b/docs/python/p0047.rst index e0fa17c5..7773246b 100644 --- a/docs/python/p0047.rst +++ b/docs/python/p0047.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 47 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0047 :members: :undoc-members: diff --git a/docs/python/p0049.rst b/docs/python/p0049.rst index 6d67103a..31ecc32a 100644 --- a/docs/python/p0049.rst +++ b/docs/python/p0049.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 49 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0049 :members: :undoc-members: diff --git a/docs/python/p0050.rst b/docs/python/p0050.rst index 2ab79a4f..c738b52e 100644 --- a/docs/python/p0050.rst +++ b/docs/python/p0050.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 50 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0050 :members: :undoc-members: diff --git a/docs/python/p0058.rst b/docs/python/p0058.rst index 89d8d813..adc5654c 100644 --- a/docs/python/p0058.rst +++ b/docs/python/p0058.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 58 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0058 :members: :undoc-members: diff --git a/docs/python/p0060.rst b/docs/python/p0060.rst index a506f631..ac6fc775 100644 --- a/docs/python/p0060.rst +++ b/docs/python/p0060.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 60 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0060 :members: :undoc-members: diff --git a/docs/python/p0069.rst b/docs/python/p0069.rst index 08a800ab..478f08e9 100644 --- a/docs/python/p0069.rst +++ b/docs/python/p0069.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 69 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0069 :members: :undoc-members: diff --git a/docs/python/p0077.rst b/docs/python/p0077.rst index 26861976..f4f88323 100644 --- a/docs/python/p0077.rst +++ b/docs/python/p0077.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 77 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0077 :members: :undoc-members: diff --git a/docs/python/p0087.rst b/docs/python/p0087.rst index 9d98f2d1..01a3adb2 100644 --- a/docs/python/p0087.rst +++ b/docs/python/p0087.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 87 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0087 :members: :undoc-members: diff --git a/docs/python/p0118.rst b/docs/python/p0118.rst index 5cac9375..28253281 100644 --- a/docs/python/p0118.rst +++ b/docs/python/p0118.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 118 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0118 :members: :undoc-members: diff --git a/docs/python/p0123.rst b/docs/python/p0123.rst index 44dfdd51..796cd57c 100644 --- a/docs/python/p0123.rst +++ b/docs/python/p0123.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 123 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0123 :members: :undoc-members: diff --git a/docs/python/p0134.rst b/docs/python/p0134.rst index e58c5482..d653e115 100644 --- a/docs/python/p0134.rst +++ b/docs/python/p0134.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 134 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0134 :members: :undoc-members: diff --git a/docs/python/p0187.rst b/docs/python/p0187.rst index 8ce596d1..cf9d5380 100644 --- a/docs/python/p0187.rst +++ b/docs/python/p0187.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 187 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0187 :members: :undoc-members: diff --git a/docs/python/p0357.rst b/docs/python/p0357.rst index 134e7d1c..9e54a7cf 100644 --- a/docs/python/p0357.rst +++ b/docs/python/p0357.rst @@ -3,6 +3,14 @@ Python Implementation of Problem 357 View source code `here on GitHub! `_ +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + .. automodule:: python.p0357 :members: :undoc-members: diff --git a/docs/python/primes.rst b/docs/python/primes.rst new file mode 100644 index 00000000..b2340fe3 --- /dev/null +++ b/docs/python/primes.rst @@ -0,0 +1,13 @@ +primes.py +========= + +View source code `here on GitHub! `_ + +.. automodule:: python.lib.primes + :members: + :undoc-members: + +.. literalinclude:: ../../python/lib/primes.py + :language: python + :linenos: + diff --git a/python/README.rst b/python/README.rst index 1b422bc2..fc1c6b5e 100644 --- a/python/README.rst +++ b/python/README.rst @@ -12,13 +12,11 @@ Organization ------------ All files share a common prefix with their problem number. Several of -the problems also have code leveraged as if they were a library. That +the problems also have code leveraged as if they were a library. I am in +the midst of moving these function into a library subfolder. That list is (approximately): -- p0002 for the fibonacci sequence -- p0003 for prime numbers and factors - p0004 for palindrome detection -- p0007 for is_prime() - p0008 for iterating over a series in groups of n - p0015 for n_choose_r - p0018 for trianglar pathfinding diff --git a/python/lib/__init__.py b/python/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/lib/fibonacci.py b/python/lib/fibonacci.py new file mode 100644 index 00000000..3a569857 --- /dev/null +++ b/python/lib/fibonacci.py @@ -0,0 +1,36 @@ + +from typing import Iterator + + +def fib() -> Iterator[int]: + """This generator goes through the fibonacci sequence""" + a, b = 0, 1 + while True: + yield b + a, b = b, a + b + + +def fib_by_3(start_index: int = 0) -> Iterator[int]: + """This generator goes through the fibonacci sequence skipping by 3s. This works because: + F[n] = F[n-1] + F[n-2] + F[n] = F[n-2] + F[n-3] + F[n-2] + F[n] = 2 * F[n-2] + F[n-3] + F[n] = 2 * (F[n-3] + F[n-4]) + F[n-3] + F[n] = 3 * F[n-3] + 2 * F[n-4] + F[n] = 3 * F[n-3] + F[n-4] + F[n-5] + F[n-6] + F[n] = 4 * F[n-3] + F[n-6] + """ + orig = fib() + a = 0 + if start_index: + for _ in range(start_index - 1): + next(orig) + a = next(orig) + next(orig) # start + 1 + next(orig) # start + 2 + b = next(orig) # start + 3 + del orig + yield a + while True: + yield b + a, b = b, a + b * 4 \ No newline at end of file diff --git a/python/lib/primes.py b/python/lib/primes.py new file mode 100644 index 00000000..f80a8565 --- /dev/null +++ b/python/lib/primes.py @@ -0,0 +1,135 @@ +from itertools import count, takewhile +from math import ceil, sqrt +from pathlib import Path +from typing import Callable, Collection, Dict, Iterator, Optional, Set, cast + +from sortedcontainers import SortedSet +from umsgpack import load + +cache_filename = 'primes_cache.mpack' + +try: + with Path(__file__).parent.joinpath(cache_filename).open('rb') as f: + cache = SortedSet(load(f)) +except Exception: + cache = SortedSet([ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61 + ]) +last_cached: int = cache[-1] + 2 + + +def primes(stop: Optional[int] = None) -> Iterator[int]: + """Iterates over the prime numbers up to an (optional) limit, with caching. + + This iterator leverages the :py:func:`modified_eratosthenes` iterator, but adds + additional features. The biggest is a ``stop`` argument, which will prevent it + from yielding any numbers larger than that. The next is caching of any yielded + prime numbers.""" + if stop is None: + yield from cache + else: + yield from takewhile(stop.__gt__, cache) + global last_cached + if stop and last_cached - 2 > stop: + return + if stop is None: + secondary = modified_eratosthenes() + else: + secondary = takewhile(stop.__gt__, modified_eratosthenes()) + for p in secondary: + if p in cache: + continue + cache.add(p) + last_cached = p + 2 + yield p + + +def modified_eratosthenes() -> Iterator[int]: + """Iterates over prime numbers using the Sieve of Eratosthenes. + + This function implements the `Sieve of Eratosthenes `_. In general, + it will iterate over all of the prime numbers. Below is a gif (courtesy of Wikipedia) that demonstrates the principle + of the sieve. + + .. image:: https://upload.wikimedia.org/wikipedia/commons/9/94/Animation_Sieve_of_Eratosth.gif + :alt: Any animated example of the Sieve of Eratosthenes""" + # modified_eratosthenes taken, refactored from https://stackoverflow.com/a/19391111 + yield from (2, 3, 5, 7) + sieve: Dict[int, int] = {} + recurse = primes() + next(recurse) + prime = next(recurse) + if prime != 3: + raise ValueError() + prime_squared = prime * prime + for candidate in count(9, 2): + if candidate in sieve: # if c is a multiple of some base prime, or + step = sieve.pop(candidate) + elif candidate < prime_squared: # prime, or + yield candidate + continue + else: # the next base prime's square + if candidate != prime_squared: + raise ValueError() + step = prime * 2 + prime = next(recurse) + prime_squared = prime * prime + candidate += step # the next multiple + while candidate in sieve: # make sure to not overwrite another sieve entry + candidate += step + sieve[candidate] = step + + +def prime_factors(num: int) -> Iterator[int]: + """Iterates over the prime factors of a number.""" + if num < 0: + yield -1 + num = -num + if num == 0: + yield 0 + else: + root = ceil(sqrt(num)) + for factor in primes(): + modulo = num % factor + if modulo == 0: + while modulo == 0: # double-check to call sqrt once + yield factor + num //= factor + modulo = num % factor + root = ceil(sqrt(num)) + if num <= 1: + break + if factor > root: + yield num + break + + +def is_prime( + num: int, + count: int = 1, + distinct: bool = False +) -> bool: + """Detects if a number is prime or not. + + If a count other than 1 is given, it returns True only if the number has + exactly count prime factors.""" + if num in (0, 1): + return False + factors = iter(prime_factors(num)) + if count == 1: + if num in cache: # always has 2 + return True + if num % 2 == 0: + return False + return next(factors) == num + seen: Collection[Optional[int]] + seen_add: Callable[[Optional[int]], None] + if distinct: + seen = set() + seen_add = seen.add + else: + seen = [] + seen_add = seen.append + while None not in seen and count != len(seen): + seen_add(next(factors, None)) + return None not in seen and next(factors, None) is None \ No newline at end of file diff --git a/python/p0003_cache.mpack b/python/lib/primes_cache.mpack similarity index 100% rename from python/p0003_cache.mpack rename to python/lib/primes_cache.mpack diff --git a/python/p0002.py b/python/p0002.py index d4494dc5..b9786abc 100644 --- a/python/p0002.py +++ b/python/p0002.py @@ -25,41 +25,7 @@ four million, find the sum of the even-valued terms. """ -from typing import Iterator - - -def fib() -> Iterator[int]: - """This generator goes through the fibonacci sequence""" - a, b = 0, 1 - while True: - yield b - a, b = b, a + b - - -def fib_by_3(start_index: int = 0) -> Iterator[int]: - """This generator goes through the fibonacci sequence skipping by 3s. This works because: - F[n] = F[n-1] + F[n-2] - F[n] = F[n-2] + F[n-3] + F[n-2] - F[n] = 2 * F[n-2] + F[n-3] - F[n] = 2 * (F[n-3] + F[n-4]) + F[n-3] - F[n] = 3 * F[n-3] + 2 * F[n-4] - F[n] = 3 * F[n-3] + F[n-4] + F[n-5] + F[n-6] - F[n] = 4 * F[n-3] + F[n-6] - """ - orig = fib() - a = 0 - if start_index: - for _ in range(start_index - 1): - next(orig) - a = next(orig) - next(orig) # start + 1 - next(orig) # start + 2 - b = next(orig) # start + 3 - del orig - yield a - while True: - yield b - a, b = b, a + b * 4 +from lib.fibonacci import fib_by_3 def main() -> int: diff --git a/python/p0003.py b/python/p0003.py index 05ee13ab..d0911d48 100644 --- a/python/p0003.py +++ b/python/p0003.py @@ -27,95 +27,7 @@ What is the largest prime factor of the number 600851475143 ? """ -from itertools import count, takewhile -from math import ceil, sqrt -from pathlib import Path -from typing import Dict, Iterator, Optional - -from sortedcontainers import SortedSet -from umsgpack import load - -cache_filename = 'p0003_cache.mpack' - -try: - with Path(__file__).parent.joinpath(cache_filename).open('rb') as f: - cache = SortedSet(load(f)) -except Exception: - cache = SortedSet([ - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61 - ]) -last_cached: int = cache[-1] + 2 - - -def primes(stop: Optional[int] = None) -> Iterator[int]: - if stop is None: - yield from cache - else: - yield from takewhile(stop.__gt__, cache) - global last_cached - if stop and last_cached - 2 > stop: - return - if stop is None: - secondary = modified_eratosthenes() - else: - secondary = takewhile(stop.__gt__, modified_eratosthenes()) - for p in secondary: - if p in cache: - continue - cache.add(p) - last_cached = p + 2 - yield p - - -def modified_eratosthenes() -> Iterator[int]: - # modified_eratosthenes taken, refactored from https://stackoverflow.com/a/19391111 - yield from (2, 3, 5, 7) - sieve: Dict[int, int] = {} - recurse = primes() - next(recurse) - prime = next(recurse) - if prime != 3: - raise ValueError() - prime_squared = prime * prime - for candidate in count(9, 2): - if candidate in sieve: # if c is a multiple of some base prime, or - step = sieve.pop(candidate) - elif candidate < prime_squared: # prime, or - yield candidate - continue - else: # the next base prime's square - if candidate != prime_squared: - raise ValueError() - step = prime * 2 - prime = next(recurse) - prime_squared = prime * prime - candidate += step # the next multiple - while candidate in sieve: # make sure to not overwrite another sieve entry - candidate += step - sieve[candidate] = step - - -def prime_factors(num: int) -> Iterator[int]: - if num < 0: - yield -1 - num = -num - if num == 0: - yield 0 - else: - root = ceil(sqrt(num)) - for factor in primes(): - modulo = num % factor - if modulo == 0: - while modulo == 0: # double-check to call sqrt once - yield factor - num //= factor - modulo = num % factor - root = ceil(sqrt(num)) - if num <= 1: - break - if factor > root: - yield num - break +from lib.primes import prime_factors def main() -> int: diff --git a/python/p0007.py b/python/p0007.py index 0bc58b4b..9db254ce 100644 --- a/python/p0007.py +++ b/python/p0007.py @@ -34,40 +34,7 @@ What is the 10 001st prime number? """ -from typing import Callable, Collection, Optional, Set, cast - -from p0003 import cache, prime_factors, primes - - -def is_prime( - num: int, - count: int = 1, - distinct: bool = False -) -> bool: - """Detects if a number is prime or not. - - If a count other than 1 is given, it returns True only if the number has - exactly count prime factors.""" - if num in (0, 1): - return False - factors = iter(prime_factors(num)) - if count == 1: - if num in cache: # always has 2 - return True - if num % 2 == 0: - return False - return next(factors) == num - seen: Collection[Optional[int]] - seen_add: Callable[[Optional[int]], None] - if distinct: - seen = set() - seen_add = seen.add - else: - seen = [] - seen_add = seen.append - while None not in seen and count != len(seen): - seen_add(next(factors, None)) - return None not in seen and next(factors, None) is None +from lib.primes import primes def main() -> int: diff --git a/python/p0010.py b/python/p0010.py index 3a21c52c..98641a7b 100644 --- a/python/p0010.py +++ b/python/p0010.py @@ -24,7 +24,7 @@ """ from itertools import takewhile -from p0003 import primes +from lib.primes import primes def main() -> int: diff --git a/python/p0021.py b/python/p0021.py index a4c46009..99f79dc8 100644 --- a/python/p0021.py +++ b/python/p0021.py @@ -26,7 +26,7 @@ from operator import mul from typing import Iterator, Set -from p0003 import prime_factors +from lib.primes import prime_factors def proper_divisors(num: int) -> Iterator[int]: diff --git a/python/p0025.py b/python/p0025.py index f98a2ee4..e51d3e27 100644 --- a/python/p0025.py +++ b/python/p0025.py @@ -29,7 +29,7 @@ What is the index of the first term in the Fibonacci sequence to contain 1000 digits? """ -from p0002 import fib +from lib.fibonacci import fib def main() -> int: diff --git a/python/p0027.py b/python/p0027.py index b619ea1a..7ef7ad67 100644 --- a/python/p0027.py +++ b/python/p0027.py @@ -31,8 +31,7 @@ from itertools import count, takewhile from typing import Iterator -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes def quadratic(n: int, a: int, b: int) -> int: diff --git a/python/p0035.py b/python/p0035.py index c489587b..9ab62d8e 100644 --- a/python/p0035.py +++ b/python/p0035.py @@ -17,7 +17,7 @@ """ from typing import Iterator -from p0007 import is_prime +from lib.primes import is_prime def rotations(x: int) -> Iterator[int]: diff --git a/python/p0037.py b/python/p0037.py index 680498bb..fe36600a 100644 --- a/python/p0037.py +++ b/python/p0037.py @@ -15,8 +15,7 @@ NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. """ -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes def main() -> int: diff --git a/python/p0041.py b/python/p0041.py index ea30f3ff..ba40b642 100644 --- a/python/p0041.py +++ b/python/p0041.py @@ -12,7 +12,7 @@ What is the largest n-digit pandigital prime that exists? """ -from p0003 import primes +from lib.primes import primes from p0074 import digits diff --git a/python/p0046.py b/python/p0046.py index 3ffed4b3..9d9a33e4 100644 --- a/python/p0046.py +++ b/python/p0046.py @@ -26,8 +26,7 @@ from itertools import count, takewhile from math import ceil, sqrt -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes def main() -> int: diff --git a/python/p0047.py b/python/p0047.py index 3d373fba..aa178f9a 100644 --- a/python/p0047.py +++ b/python/p0047.py @@ -26,7 +26,7 @@ from itertools import count from typing import Tuple -from p0007 import is_prime +from lib.primes import is_prime from p0008 import groupwise diff --git a/python/p0049.py b/python/p0049.py index 36012bbc..1bfcc03d 100644 --- a/python/p0049.py +++ b/python/p0049.py @@ -17,7 +17,7 @@ """ from itertools import tee -from p0003 import primes +from lib.primes import primes from p0074 import digits diff --git a/python/p0050.py b/python/p0050.py index c9919930..052853b7 100644 --- a/python/p0050.py +++ b/python/p0050.py @@ -20,8 +20,7 @@ """ from typing import List -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes from p0008 import groupwise diff --git a/python/p0058.py b/python/p0058.py index b42751ff..b4fe7882 100644 --- a/python/p0058.py +++ b/python/p0058.py @@ -22,7 +22,7 @@ """ from itertools import count -from p0007 import is_prime +from lib.primes import is_prime from p0028 import spiral_corners diff --git a/python/p0060.py b/python/p0060.py index 91088f61..9d42f421 100644 --- a/python/p0060.py +++ b/python/p0060.py @@ -13,8 +13,7 @@ from itertools import combinations from typing import DefaultDict, List, Set -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes def is_concat_prime(x: int, y: int) -> bool: diff --git a/python/p0069.py b/python/p0069.py index c59d6212..dcedda9b 100644 --- a/python/p0069.py +++ b/python/p0069.py @@ -25,7 +25,7 @@ Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum. """ -from p0003 import primes +from lib.primes import primes def main() -> int: diff --git a/python/p0077.py b/python/p0077.py index 3caa22d6..80a707c3 100644 --- a/python/p0077.py +++ b/python/p0077.py @@ -18,7 +18,7 @@ from itertools import count from typing import List, Tuple -from p0003 import primes +from lib.primes import primes def prime_summations(n: int) -> int: diff --git a/python/p0087.py b/python/p0087.py index 134e9056..c339d850 100644 --- a/python/p0087.py +++ b/python/p0087.py @@ -24,7 +24,7 @@ How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power? """ -from p0003 import primes +from lib.primes import primes def main() -> int: diff --git a/python/p0118.py b/python/p0118.py index cd960415..ec4e3cb0 100644 --- a/python/p0118.py +++ b/python/p0118.py @@ -14,7 +14,7 @@ from itertools import takewhile from math import ceil, log10 -from p0003 import primes +from lib.primes import primes all_digits = set("123456789") diff --git a/python/p0123.py b/python/p0123.py index a94c45a8..739ef2bd 100644 --- a/python/p0123.py +++ b/python/p0123.py @@ -21,7 +21,7 @@ Find the least value of n for which the remainder first exceeds 10**10. """ -from p0003 import primes +from lib.primes import primes def main() -> int: diff --git a/python/p0134.py b/python/p0134.py index a7b26880..510caa04 100644 --- a/python/p0134.py +++ b/python/p0134.py @@ -22,7 +22,7 @@ Find ∑ S for every pair of consecutive primes with 5 ≤ p1 ≤ 1000000. """ -from p0003 import primes +from lib.primes import primes def mul_inv(a: int, b: int) -> int: diff --git a/python/p0187.py b/python/p0187.py index 5ffb4a45..230204e2 100644 --- a/python/p0187.py +++ b/python/p0187.py @@ -24,7 +24,7 @@ """ from itertools import takewhile -from p0003 import primes +from lib.primes import primes def main() -> int: diff --git a/python/p0357.py b/python/p0357.py index 8b806adf..b20c96ca 100644 --- a/python/p0357.py +++ b/python/p0357.py @@ -27,8 +27,7 @@ """ from typing import Iterable -from p0003 import primes -from p0007 import is_prime +from lib.primes import is_prime, primes from p0021 import proper_divisors diff --git a/python/test_euler.py b/python/test_euler.py index 864ef0d9..7b70c69d 100644 --- a/python/test_euler.py +++ b/python/test_euler.py @@ -12,8 +12,7 @@ PY_FOLDER = Path(__file__).parent path.append(str(PY_FOLDER)) -from p0003 import primes # noqa: E402 # isort:skip -from p0007 import is_prime # noqa: E402 # isort:skip +from lib.primes import is_prime, primes # noqa: E402 # isort:skip from p0008 import groupwise # noqa: E402 # isort:skip answers = {