Skip to content

Commit 719c556

Browse files
authored
Add default args and type hints for problem 7 (TheAlgorithms#2973)
- Add default argument values - Add type hints - Change one letter variable names to a more descriptive one - Add doctest for `solution()`
1 parent 6541236 commit 719c556

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

Diff for: project_euler/problem_07/sol1.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 7: https://projecteuler.net/problem=7
3+
24
By listing the first six prime numbers:
35
46
2, 3, 5, 7, 11, and 13
@@ -8,20 +10,21 @@
810
from math import sqrt
911

1012

11-
def is_prime(n):
12-
if n == 2:
13+
def is_prime(num: int) -> bool:
14+
"""Determines whether the given number is prime or not"""
15+
if num == 2:
1316
return True
14-
elif n % 2 == 0:
17+
elif num % 2 == 0:
1518
return False
1619
else:
17-
sq = int(sqrt(n)) + 1
20+
sq = int(sqrt(num)) + 1
1821
for i in range(3, sq, 2):
19-
if n % i == 0:
22+
if num % i == 0:
2023
return False
2124
return True
2225

2326

24-
def solution(n):
27+
def solution(nth: int = 10001) -> int:
2528
"""Returns the n-th prime number.
2629
2730
>>> solution(6)
@@ -36,18 +39,20 @@ def solution(n):
3639
229
3740
>>> solution(100)
3841
541
42+
>>> solution()
43+
104743
3944
"""
40-
i = 0
41-
j = 1
42-
while i != n and j < 3:
43-
j += 1
44-
if is_prime(j):
45-
i += 1
46-
while i != n:
47-
j += 2
48-
if is_prime(j):
49-
i += 1
50-
return j
45+
count = 0
46+
number = 1
47+
while count != nth and number < 3:
48+
number += 1
49+
if is_prime(number):
50+
count += 1
51+
while count != nth:
52+
number += 2
53+
if is_prime(number):
54+
count += 1
55+
return number
5156

5257

5358
if __name__ == "__main__":

Diff for: project_euler/problem_07/sol2.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 7: https://projecteuler.net/problem=7
3+
24
By listing the first six prime numbers:
35
46
2, 3, 5, 7, 11, and 13
@@ -7,14 +9,15 @@
79
"""
810

911

10-
def isprime(number):
12+
def isprime(number: int) -> bool:
13+
"""Determines whether the given number is prime or not"""
1114
for i in range(2, int(number ** 0.5) + 1):
1215
if number % i == 0:
1316
return False
1417
return True
1518

1619

17-
def solution(n):
20+
def solution(nth: int = 10001) -> int:
1821
"""Returns the n-th prime number.
1922
2023
>>> solution(6)
@@ -29,34 +32,38 @@ def solution(n):
2932
229
3033
>>> solution(100)
3134
541
35+
>>> solution()
36+
104743
3237
>>> solution(3.4)
3338
5
3439
>>> solution(0)
3540
Traceback (most recent call last):
3641
...
37-
ValueError: Parameter n must be greater or equal to one.
42+
ValueError: Parameter nth must be greater or equal to one.
3843
>>> solution(-17)
3944
Traceback (most recent call last):
4045
...
41-
ValueError: Parameter n must be greater or equal to one.
46+
ValueError: Parameter nth must be greater or equal to one.
4247
>>> solution([])
4348
Traceback (most recent call last):
4449
...
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.
4651
>>> solution("asd")
4752
Traceback (most recent call last):
4853
...
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.
5055
"""
5156
try:
52-
n = int(n)
57+
nth = int(nth)
5358
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.")
5764
primes = []
5865
num = 2
59-
while len(primes) < n:
66+
while len(primes) < nth:
6067
if isprime(num):
6168
primes.append(num)
6269
num += 1

Diff for: project_euler/problem_07/sol3.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Project 7: https://projecteuler.net/problem=7
3+
24
By listing the first six prime numbers:
35
46
2, 3, 5, 7, 11, and 13
@@ -9,7 +11,8 @@
911
import math
1012

1113

12-
def primeCheck(number):
14+
def prime_check(number: int) -> bool:
15+
"""Determines whether a given number is prime or not"""
1316
if number % 2 == 0 and number > 2:
1417
return False
1518
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
@@ -18,12 +21,12 @@ def primeCheck(number):
1821
def prime_generator():
1922
num = 2
2023
while True:
21-
if primeCheck(num):
24+
if prime_check(num):
2225
yield num
2326
num += 1
2427

2528

26-
def solution(n):
29+
def solution(nth: int = 10001) -> int:
2730
"""Returns the n-th prime number.
2831
2932
>>> solution(6)
@@ -38,8 +41,10 @@ def solution(n):
3841
229
3942
>>> solution(100)
4043
541
44+
>>> solution()
45+
104743
4146
"""
42-
return next(itertools.islice(prime_generator(), n - 1, n))
47+
return next(itertools.islice(prime_generator(), nth - 1, nth))
4348

4449

4550
if __name__ == "__main__":

0 commit comments

Comments
 (0)