Created
August 13, 2014 13:53
-
-
Save allenaven/b97f8a869dfe60607919 to your computer and use it in GitHub Desktop.
Find 1000th prime number
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 characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Aug 12 23:54:45 2014 | |
@author: Allen | |
""" | |
primes = [2] # Vector of prime numbers: initialize with 2, since that is | |
# a prime number but fails the tests | |
def ptest(num): | |
if num in [2, 3, 5, 7]: | |
return(True) | |
if num % 2 == 0: | |
return(False) | |
rem = [num % i for i in range(3,num)] | |
if 0 in rem: | |
return(False) | |
else: | |
return(True) | |
counter = 1 # This will cause the ptest to start at 3 | |
while len(primes) < 1000: | |
counter += 2 | |
if ptest(counter): | |
primes.append(counter) | |
print('The 1000th prime number is: ', primes[999]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment