Ejemplo n.º 1
0
def test_is_prime_false_for_any_integer_not_ending_in_1379(z):
    if z <= 1:
        assert not is_prime(z)
    elif z == 2:
        assert is_prime(z)
    else:
        if str(z)[-1] not in map(str, (1, 3, 7, 9)):
            assert not is_prime(z)
Ejemplo n.º 2
0
def test_goldbach_partitions(z):
    expected = set()
    if z % 2:
        assert Integer(z).goldbach_partitions == expected
    else:
        expected = set(filter(lambda x: is_prime(x[0]) and is_prime(x[1]),
                              zip(range(z//2+1),
                                  (z - x for x in range(z//2+1)))))
        assert Integer(z).goldbach_partitions == expected
Ejemplo n.º 3
0
def test_nearest_prime(z):
    def generate_primes_before_n(n):
        p = generate_primes()
        largest_so_far = next(p)
        while largest_so_far < n:
            yield largest_so_far
            largest_so_far = next(p)

    def generate_primes_after_n(n):
        p = generate_primes()
        largest_so_far = next(p)
        while largest_so_far < n:
            largest_so_far = next(p)

        yield largest_so_far
        while True:
            yield next(p)

    np = Integer(z).nearest_prime
    if z <= 0:
        assert np == (2,)
    elif is_prime(z):
        assert np == (z,)
    else:
        closest_before_z = max(generate_primes_before_n(z))
        closest_after_z = next(generate_primes_after_n(z))
        if abs(z - closest_before_z) == abs(z - closest_after_z):
            assert np == (closest_before_z, closest_after_z)
        else:
            assert np == (min(closest_after_z, closest_before_z,
                              key=lambda n, z=z: abs(z-n)),)
Ejemplo n.º 4
0
def test_is_mersenne_prime(z):
    if z <= 0:
        assert not Integer(z).is_mersenne_prime
    else:
        if math.log(z+1, 2).is_integer() and is_prime(z):
            assert Integer(z).is_mersenne_prime
        else:
            assert not Integer(z).is_mersenne_prime
Ejemplo n.º 5
0
def test_primality(z):
    if is_prime(z):
        assert Integer(z).primality == "Prime"
    else:
        assert Integer(z).primality == "Composite"
Ejemplo n.º 6
0
def test_pi(z):
    assert Integer(z).pi == sum(1 for x in range(2, z + 1) if is_prime(x))
Ejemplo n.º 7
0
def test_omega(z):
    Z = Integer(z)
    assert Z.omega == sum(1 for _ in
                          filter(lambda x: is_prime(x) and not z % x,
                                 range(0, z+1)))
Ejemplo n.º 8
0
def test_generate_primes(how_many):
    primes = generate_primes()
    for _ in range(how_many):
        assert is_prime(next(primes))