-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp007.py
38 lines (26 loc) · 828 Bytes
/
p007.py
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
#
# Solution to Project Euler problem 7
# Copyright (c) Andrea Fresco. All rights reserved.
#
# https://projecteuler.net/problem=7
# https://github.com/andreafresco/Project-Euler
#
def Is_prime(n): # check if the number n is prime
assert n > 0
i = 2
# we restrict the search of prime numbers between 2 and sqrt(n)
while i*i <= n: # equivalent to i <= sqrt(n)
if n%i == 0:
return False # if the number has a divisor then it's NOT prime
i+=1
return True # if no divisors were found then it's a prime number
def IDXthPrime(idx):
i = 2
primeCounts = 0
while primeCounts < idx:
if Is_prime(i):
primeCounts+=1
i += 1
return i-1
if __name__ == "__main__":
print(IDXthPrime(10001))