-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem7.py
More file actions
71 lines (52 loc) · 1.28 KB
/
problem7.py
File metadata and controls
71 lines (52 loc) · 1.28 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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?
#
# @date 12/03/2012
# @author Ciarán McCann
# @website http://ciaranmccann.me/
#
# @timeTaken 1.7669999599456787
import math
from time import time
def chunk(collection, chuckSize):
return [collection[i:i+chuckSize] for i in range(0, len(collection), chuckSize)]
def printGridArray(array,width = 10):
rows = chunk(array,width)
print(" ")
for row in rows:
print(row)
print(" ")
def remove_all(element, list):
return filter(lambda x: x != element, list)
def genPrimeList(upTo):
primeNth = 0
numbers = []
for i in range(0,upTo):
numbers.append(i)
numbers[1] = 0
p = 2
n = 2
#printGridArray(numbers)
while True:
n = 2
while n*p < len(numbers): # remove all multplies
numbers[n*p] = 0
n += 1
while p < len(numbers): # find next multiple to remove
p += 1
if p == len(numbers):
return numbers
if numbers[p] != 0:
p = numbers[p]
primeNth += 1
break
def findNthPrime(n,range):
ans = list(remove_all(0, genPrimeList(range) ))[n-1]
print (" The " + str(n) + " th prime is " + str(ans))
start = time()
print(start)
findNthPrime(10001,1310000)
end = time()
print(end - start)