# BSD Zero Clause License # # Copyright (c) 2022 Marek Krzywdziński # # Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT # SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import pytest from parameters import parameters from primes import primes from semiprimes import semiprimes def factorize_semiprimes(semiprime): recommended_strategy = None for key in reversed(parameters.keys()): if int(key) >= semiprime: recommended_strategy = key strategy = recommended_strategy if not strategy: raise NotImplementedError scope = parameters[strategy]['scope'] initial_semiprimes = str(parameters[strategy]['initial_semiprimes'])[1:] pattern = parameters[strategy]['pattern'] step_for_initial_semiprimes = parameters[strategy]['step_for_semiprimes'] size = len(bin(pattern)[2:]) initial_semiprimes = [initial_semiprimes[i * step_for_initial_semiprimes: i * step_for_initial_semiprimes + step_for_initial_semiprimes] for i in range(size)] dots = [int(bin(pattern)[2:][i]) for i in range(size)] allowed_primes = primes[:scope] fields = [(first_prime, second_prime) for first_prime in allowed_primes for second_prime in allowed_primes if first_prime <= second_prime] allowed_semiprimes = {} for i in range(len(dots)): if int(dots[i]): allowed_semiprimes[int(initial_semiprimes[i])] = fields[i] return allowed_semiprimes[semiprime] @pytest.mark.parametrize('semiprime', [semiprime for semiprime in semiprimes if semiprime <= 16003]) def test_semiprimes(semiprime): assert factorize_semiprimes(semiprime)[0] * factorize_semiprimes(semiprime)[1] == semiprime