-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
51 lines (43 loc) · 1.72 KB
/
Copy path7.py
File metadata and controls
51 lines (43 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
Author: Michael Sherif Naguib
Date: Occtober 10, 2018
@: University of Tulsa
Question 7#: What is the 10 001st prime number?
Example: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
'''
isPrime=__import__("3").isPrime
import math
#Making this code a function so that it can be used in 10.py
#UPDATE: October 23 2018 I just realied I dont necessarily need to find the primes 1-n
# but exclude but exclude all non primes in a list 1-n which is similar
# but can be implemented faster... this is done in 10.py
def findPrimesUpto(N):
count=1#The number of primes fount so far
primes=[2]#an array storing the primes found
i=3#initial number to check for primality
while count<N:#calculations:
didEvenlyDivide=False
sqrtOfPotentialPrime = math.sqrt(i)#outside the loop to be more efficient
#for every prime already found
for j in range(1,len(primes)):#start at index one because the iteration skips all numbers powers of 2
#Does it divide?
p = primes[j]#cache the value
if i/p== i//p:#integer division... check if the same...
didEvenlyDivide=True
break
#if none of the factors below that number did not work then the sqrt is the max
#there could exist a factor
if p>sqrtOfPotentialPrime:
break
#Append To the list of primes
if(not didEvenlyDivide):
primes.append(i)
count+=1
i+=2#skip 2
return primes
if __name__=="__main__":
#setup
N=10001
primes = findPrimesUpto(N)
#print
print(primes[len(primes)-1])