def main():
print getPrimes()
def getPrimes():
# This script won't generate the first
# prime number (2), so we start with
# primes = 1, where primes is the # of
# primes we have generated.
primes = 1
num = 1
while primes < 1000:
num += 2
if isPrime(num):
primes += 1
# Returns [1000, num] where num is the
# 1000th prime number.
return primes, num
def isPrime(n):
# Assert that n is not even. Even numbers
# cannot be prime, so why bother with them?
assert n % 2 != 0
# All values of n will be odd, so we don't need to
# check and see if they are divisible by 1 or 2.
# This also means that we don't need to check and
# see if n is divisible by any even numbers, so we
# step by 2 each iteration.
for i in range(3, n/2, 2):
if n % i == 0: return False
return True
if __name__ == "__main__":
main()
This post has been edited by Brewer: 18 July 2011 - 05:15 AM

New Topic/Question
Reply




MultiQuote





|