1
1
"""
2
+ Problem 7: https://projecteuler.net/problem=7
3
+
2
4
By listing the first six prime numbers:
3
5
4
6
2, 3, 5, 7, 11, and 13
7
9
"""
8
10
9
11
10
- def isprime (number ):
12
+ def isprime (number : int ) -> bool :
13
+ """Determines whether the given number is prime or not"""
11
14
for i in range (2 , int (number ** 0.5 ) + 1 ):
12
15
if number % i == 0 :
13
16
return False
14
17
return True
15
18
16
19
17
- def solution (n ) :
20
+ def solution (nth : int = 10001 ) -> int :
18
21
"""Returns the n-th prime number.
19
22
20
23
>>> solution(6)
@@ -29,34 +32,38 @@ def solution(n):
29
32
229
30
33
>>> solution(100)
31
34
541
35
+ >>> solution()
36
+ 104743
32
37
>>> solution(3.4)
33
38
5
34
39
>>> solution(0)
35
40
Traceback (most recent call last):
36
41
...
37
- ValueError: Parameter n must be greater or equal to one.
42
+ ValueError: Parameter nth must be greater or equal to one.
38
43
>>> solution(-17)
39
44
Traceback (most recent call last):
40
45
...
41
- ValueError: Parameter n must be greater or equal to one.
46
+ ValueError: Parameter nth must be greater or equal to one.
42
47
>>> solution([])
43
48
Traceback (most recent call last):
44
49
...
45
- TypeError: Parameter n must be int or passive of cast to int.
50
+ TypeError: Parameter nth must be int or passive of cast to int.
46
51
>>> solution("asd")
47
52
Traceback (most recent call last):
48
53
...
49
- TypeError: Parameter n must be int or passive of cast to int.
54
+ TypeError: Parameter nth must be int or passive of cast to int.
50
55
"""
51
56
try :
52
- n = int (n )
57
+ nth = int (nth )
53
58
except (TypeError , ValueError ):
54
- raise TypeError ("Parameter n must be int or passive of cast to int." )
55
- if n <= 0 :
56
- raise ValueError ("Parameter n must be greater or equal to one." )
59
+ raise TypeError (
60
+ "Parameter nth must be int or passive of cast to int."
61
+ ) from None
62
+ if nth <= 0 :
63
+ raise ValueError ("Parameter nth must be greater or equal to one." )
57
64
primes = []
58
65
num = 2
59
- while len (primes ) < n :
66
+ while len (primes ) < nth :
60
67
if isprime (num ):
61
68
primes .append (num )
62
69
num += 1
0 commit comments