示例#1
0
文件: awful.py 项目: cesarfm/pyprimes
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
示例#2
0
文件: awful.py 项目: cesarfm/pyprimes
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
示例#3
0
文件: awful.py 项目: cesarfm/pyprimes
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
示例#4
0
文件: tests.py 项目: uzumaxy/pyprimes
 def test_all(self):
     self.assertTrue(compat23.all([1, 2, 3, 4]))
     self.assertTrue(compat23.all([]))
     self.assertFalse(compat23.all([1, 2, 0, 4]))
示例#5
0
文件: tests.py 项目: cesarfm/pyprimes
 def test_all(self):
     self.assertTrue(compat23.all([1, 2, 3, 4]))
     self.assertTrue(compat23.all([]))
     self.assertFalse(compat23.all([1, 2, 0, 4]))