Exemplo n.º 1
0
def primes1():
    """Generate prime numbers by trial division very slowly.

    >>> p = primes1()
    >>> [next(p) for _ in range(10)]
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    This adds a single optimization to ``primes0``, using a short-circuit
    test for primality: as soon as a factor is found, the candidate is
    rejected immediately.
    """
    i = 2
    yield i
    while True:
        i += 1
        if all(i%p != 0 for p in range(2, i)):
            yield i
Exemplo n.º 2
0
def primes2():
    """Generate prime numbers by trial division very slowly.

    >>> p = primes2()
    >>> [next(p) for _ in range(10)]
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    This is an incremental improvement over ``primes1`` by only testing
    odd numbers as potential primes and factors.
    """
    yield 2
    i = 3
    yield i
    while True:
        i += 2
        if all(i%p != 0 for p in range(3, i, 2)):
            yield i
Exemplo n.º 3
0
def primes3():
    """Generate prime numbers by trial division slowly.

    >>> p = primes3()
    >>> [next(p) for _ in range(10)]
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

    This is an incremental improvement over ``primes2`` by only testing
    potential factors up to the square root of the candidate. For small
    primes below 50000 or so, this may be slightly faster than ``primes4``.

    """
    yield 2
    i = 3
    yield i
    while True:
        i += 2
        if all(i%p != 0 for p in range(3, isqrt(i)+1, 2)):
            yield i
Exemplo n.º 4
0
 def test_all(self):
     self.assertTrue(compat23.all([1, 2, 3, 4]))
     self.assertTrue(compat23.all([]))
     self.assertFalse(compat23.all([1, 2, 0, 4]))
Exemplo n.º 5
0
 def test_all(self):
     self.assertTrue(compat23.all([1, 2, 3, 4]))
     self.assertTrue(compat23.all([]))
     self.assertFalse(compat23.all([1, 2, 0, 4]))