def test_pollard_p1_2(self): p = list(primes(10)) prime_factors = random.choices(p, k=5) num = product(prime_factors) found_factors = factor(num, 'pollard-p1') self.assertEqual(num, product(found_factors)) self.assertCountEqual(found_factors, prime_factors)
def test_pollard_rho_1(self): # Taken from Wikipedia num = 8051 f = factor(num, 'pollard-rho') self.assertCountEqual(f, [97, 83]) prime_factors = [13, 19, 37] num = product(prime_factors) found_factors = factor(num, 'pollard-rho') self.assertEqual(num, product(found_factors)) self.assertCountEqual(found_factors, [13, 19, 37])
def test_fermat_factor_2(self): prime_factors = [13, 19, 37] num = product(prime_factors) found_factors = factor(num, 'fermat') self.assertEqual(num, product(found_factors)) self.assertCountEqual(found_factors, [13, 19, 37]) # A random composite number picked from nowhere. num = 124987921 found_factors = factor(num, 'fermat') self.assertEqual(num, product(found_factors)) self.assertCountEqual(found_factors, [690541, 181])
def test_gnu_factor(self): # test the toolchain, not the algorithm... # A random composite number picked from nowhere. num = 124987921 found_factors = factor(num, 'gnu-factor') self.assertEqual(num, product(found_factors)) self.assertCountEqual(found_factors, [690541, 181])
def jacobi(a, n): """ The Jacobi Symbol of `a` mod `n` Implemented by factoring `a` and multiplying powers of each factor's Legendre symbol to get the Jacobi Symbol of `a`. Example: >>> jacobi(4567, 12345) -1 >>> jacobi(107, 137) 1 """ factors = Counter(factor(n, 'pollard-p1')) return product(legendre(a, f)**b for f, b in factors.items())
def test_trial_division_factor(self): prime_factors = [13, 13, 19, 37, 113] num = product(prime_factors) found_factors = factor(num, 'trial-division') self.assertCountEqual(prime_factors, found_factors)
def main(): """Test whether a product of the first n primes, plus 1 is prime. Very few are...""" for i in range(1, 101): p = product(primes(i)) + 1 print(i, is_prime(p))