Skip to content

Instantly share code, notes, and snippets.

@allenaven
Created August 13, 2014 13:53
Show Gist options
  • Save allenaven/b97f8a869dfe60607919 to your computer and use it in GitHub Desktop.
Save allenaven/b97f8a869dfe60607919 to your computer and use it in GitHub Desktop.
Find 1000th prime number
# -*- 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