Finished 7
This commit is contained in:
54
7/main.py
Normal file
54
7/main.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Find the 10001st prime number
|
||||||
|
|
||||||
|
|
||||||
|
def isDivisible(n, ms):
|
||||||
|
res = False
|
||||||
|
for m in ms:
|
||||||
|
if n % m == 0:
|
||||||
|
res = True
|
||||||
|
break
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def getNextPrime(ps):
|
||||||
|
if len(ps) == 0:
|
||||||
|
return [2]
|
||||||
|
else:
|
||||||
|
p = ps[-1] + 1
|
||||||
|
while isDivisible(p, [q for q in ps if q <= np.sqrt(p)]):
|
||||||
|
p = p + 1
|
||||||
|
ps.append(p)
|
||||||
|
return ps
|
||||||
|
|
||||||
|
|
||||||
|
def findNthPrime(n):
|
||||||
|
ps = [2]
|
||||||
|
i = 1
|
||||||
|
|
||||||
|
while n > i:
|
||||||
|
ps = getNextPrime(ps)
|
||||||
|
i = i + 1
|
||||||
|
print(i, ps[-1])
|
||||||
|
|
||||||
|
return ps[-1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Hello, this is Patrick")
|
||||||
|
|
||||||
|
p = findNthPrime(10001)
|
||||||
|
|
||||||
|
print(p)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user