Skip to content

Instantly share code, notes, and snippets.

@yngwie74
Created January 17, 2012 00:12

Revisions

  1. yngwie74 revised this gist Jan 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion find_primes.py
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ def allPrimes():
    return chain([2], ifilter(isPrime, count(3, step=2)))

    def findNthPrime(n):
    for i, r in izip(xrange(1, n + 1), allPrimes()):
    for i, r in izip(xrange(n), allPrimes()):
    pass
    return r

  2. yngwie74 revised this gist Jan 20, 2012. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions find_primes.py
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,12 @@
    https://gist.github.com/1623748
    """

    import math
    from math import sqrt
    from itertools import count, izip, chain, ifilter


    def _hasMoreDivisors(n):
    max_candidate = int(math.sqrt(n)) + 1
    max_candidate = int(sqrt(n)) + 1
    range = chain([2], xrange(3, max_candidate, 2))
    return any(x for x in range if (n % x) == 0)

    @@ -25,9 +25,8 @@ def allPrimes():
    return chain([2], ifilter(isPrime, count(3, step=2)))

    def findNthPrime(n):
    r, p = 0, allPrimes()
    for i in xrange(1, n + 1):
    r = p.next()
    for i, r in izip(xrange(1, n + 1), allPrimes()):
    pass
    return r


  3. yngwie74 revised this gist Jan 20, 2012. 1 changed file with 10 additions and 21 deletions.
    31 changes: 10 additions & 21 deletions find_primes.py
    Original file line number Diff line number Diff line change
    @@ -5,41 +5,30 @@
    Implementación del problema #7 del proyecto Euler (projecteuler.net/problem=7):
    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
    the 6th prime is 13. What is the 10,001st prime number?
    https://gist.github.com/1623748
    """

    import math
    from itertools import count, izip, chain, ifilter


    def _first(iterable):
    for i in iterable:
    if i: return i
    raise ValueError('Ningún elemento cumple el criterio')

    def _findNth(n, iterable):
    return _first(p for i, p in izip(count(1), iterable) if i >= n)

    def _closedRange(start, stop, step):
    return xrange(start, stop + 1, step)

    def _hasMoreDivisors(n):
    max_candidate = int(math.sqrt(n))
    return any(x for x in _closedRange(3, max_candidate, step=2) if (n % x) == 0)
    max_candidate = int(math.sqrt(n)) + 1
    range = chain([2], xrange(3, max_candidate, 2))
    return any(x for x in range if (n % x) == 0)

    def isPrime(n):
    if n < 2:
    return False
    if n == 2:
    return True
    if (n % 2) == 0:
    return False
    return not _hasMoreDivisors(n)
    return n == 2 or n >= 3 and (n % 2) != 0 and not _hasMoreDivisors(n)

    def allPrimes():
    return chain([2], ifilter(isPrime, count(3, step=2)))

    def findNthPrime(n):
    return _findNth(n, allPrimes())
    r, p = 0, allPrimes()
    for i in xrange(1, n + 1):
    r = p.next()
    return r


    if __name__ == '__main__':
  4. yngwie74 revised this gist Jan 19, 2012. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions find_primes.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    """

    import math
    from itertools import count, izip
    from itertools import count, izip, chain, ifilter


    def _first(iterable):
    @@ -36,9 +36,7 @@ def isPrime(n):
    return not _hasMoreDivisors(n)

    def allPrimes():
    yield 2
    for n in count(3, step=2):
    if isPrime(n): yield n
    return chain([2], ifilter(isPrime, count(3, step=2)))

    def findNthPrime(n):
    return _findNth(n, allPrimes())
  5. yngwie74 created this gist Jan 17, 2012.
    48 changes: 48 additions & 0 deletions find_primes.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    """
    Implementación del problema #7 del proyecto Euler (projecteuler.net/problem=7):
    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
    the 6th prime is 13. What is the 10,001st prime number?
    """

    import math
    from itertools import count, izip


    def _first(iterable):
    for i in iterable:
    if i: return i
    raise ValueError('Ningún elemento cumple el criterio')

    def _findNth(n, iterable):
    return _first(p for i, p in izip(count(1), iterable) if i >= n)

    def _closedRange(start, stop, step):
    return xrange(start, stop + 1, step)

    def _hasMoreDivisors(n):
    max_candidate = int(math.sqrt(n))
    return any(x for x in _closedRange(3, max_candidate, step=2) if (n % x) == 0)

    def isPrime(n):
    if n < 2:
    return False
    if n == 2:
    return True
    if (n % 2) == 0:
    return False
    return not _hasMoreDivisors(n)

    def allPrimes():
    yield 2
    for n in count(3, step=2):
    if isPrime(n): yield n

    def findNthPrime(n):
    return _findNth(n, allPrimes())


    if __name__ == '__main__':
    print findNthPrime(10001)