Created
January 17, 2012 00:12
Revisions
-
yngwie74 revised this gist
Jan 20, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(n), allPrimes()): pass return r -
yngwie74 revised this gist
Jan 20, 2012 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,12 +9,12 @@ https://gist.github.com/1623748 """ from math import sqrt from itertools import count, izip, chain, ifilter def _hasMoreDivisors(n): 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): for i, r in izip(xrange(1, n + 1), allPrimes()): pass return r -
yngwie74 revised this gist
Jan 20, 2012 . 1 changed file with 10 additions and 21 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 _hasMoreDivisors(n): 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): 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): r, p = 0, allPrimes() for i in xrange(1, n + 1): r = p.next() return r if __name__ == '__main__': -
yngwie74 revised this gist
Jan 19, 2012 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ """ import math from itertools import count, izip, chain, ifilter def _first(iterable): @@ -36,9 +36,7 @@ def isPrime(n): return not _hasMoreDivisors(n) def allPrimes(): return chain([2], ifilter(isPrime, count(3, step=2))) def findNthPrime(n): return _findNth(n, allPrimes()) -
yngwie74 created this gist
Jan 17, 2012 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)