Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
    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])
Exemplo n.º 3
0
    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])
Exemplo n.º 4
0
 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])
Exemplo n.º 5
0
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())
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
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))