Skip to content

Instantly share code, notes, and snippets.

@yngwie74
Created January 17, 2012 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yngwie74/1623748 to your computer and use it in GitHub Desktop.
Save yngwie74/1623748 to your computer and use it in GitHub Desktop.
Mi implementación del problema #7 del proyecto Euler
#!/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?
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)
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):
for i, r in izip(xrange(n), allPrimes()):
pass
return r
if __name__ == '__main__':
print findNthPrime(10001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment