Skip to content

Commit 5440138

Browse files
authored
Hacktoberfest 2020: Add style improvements for Project Euler Problem 03 (#2917)
* Fix typehints in project_euler/problem01 Squashed commit of the following: commit 6801d07 Author: Archaengel <[email protected]> Date: Mon Oct 5 16:40:10 2020 -0700 Fix typehints in project_euler/problem01 commit 29afc3a Author: Archaengel <[email protected]> Date: Mon Oct 5 15:06:34 2020 -0700 Add typehints and default argument for project_euler/problem_01 * Add default args, typehints, and expand variable names for PE prob 02 * Add style improvements for first solution of PE Problem 02 * Add default arg and typehints for second solution of PE Problem 02 * Add default arg for third solution of PE Problem 02 * Add style improvements for 1st soln of PE problem 03 * Add default arg and typehints for 2nd soln of PE problem 03 * Add default arg for 3rd soln of PE problem 03 * Remove unnecessary newlines * Remove unnecessary newlines * Fix end of file for 2nd soln in PE problem 03
1 parent e74adc4 commit 5440138

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

Diff for: project_euler/problem_03/sol1.py

+40-24
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,43 @@
55
66
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
77
"""
8+
89
import math
910

1011

11-
def isprime(no):
12-
if no == 2:
12+
def isprime(num: int) -> bool:
13+
"""Returns boolean representing primality of given number num.
14+
>>> isprime(2)
15+
True
16+
>>> isprime(3)
17+
True
18+
>>> isprime(27)
19+
False
20+
>>> isprime(2999)
21+
True
22+
>>> isprime(0)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: Parameter num must be greater or equal to two.
26+
>>> isprime(1)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: Parameter num must be greater or equal to two.
30+
"""
31+
if num <= 1:
32+
raise ValueError("Parameter num must be greater or equal to two.")
33+
if num == 2:
1334
return True
14-
elif no % 2 == 0:
35+
elif num % 2 == 0:
1536
return False
16-
sq = int(math.sqrt(no)) + 1
17-
for i in range(3, sq, 2):
18-
if no % i == 0:
37+
for i in range(3, int(math.sqrt(num)) + 1, 2):
38+
if num % i == 0:
1939
return False
2040
return True
2141

2242

23-
def solution(n):
43+
def solution(n: int = 600851475143) -> int:
2444
"""Returns the largest prime factor of a given number n.
25-
2645
>>> solution(13195)
2746
29
2847
>>> solution(10)
@@ -54,24 +73,21 @@ def solution(n):
5473
raise TypeError("Parameter n must be int or passive of cast to int.")
5574
if n <= 0:
5675
raise ValueError("Parameter n must be greater or equal to one.")
57-
maxNumber = 0
76+
max_number = 0
77+
if isprime(n):
78+
return n
79+
while n % 2 == 0:
80+
n //= 2
5881
if isprime(n):
5982
return n
60-
else:
61-
while n % 2 == 0:
62-
n = n / 2
63-
if isprime(n):
64-
return int(n)
65-
else:
66-
n1 = int(math.sqrt(n)) + 1
67-
for i in range(3, n1, 2):
68-
if n % i == 0:
69-
if isprime(n / i):
70-
maxNumber = n / i
71-
break
72-
elif isprime(i):
73-
maxNumber = i
74-
return maxNumber
83+
for i in range(3, int(math.sqrt(n)) + 1, 2):
84+
if n % i == 0:
85+
if isprime(n / i):
86+
max_number = n / i
87+
break
88+
elif isprime(i):
89+
max_number = i
90+
return max_number
7591

7692

7793
if __name__ == "__main__":

Diff for: project_euler/problem_03/sol2.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
"""
88

99

10-
def solution(n):
10+
def solution(n: int = 600851475143) -> int:
1111
"""Returns the largest prime factor of a given number n.
12-
1312
>>> solution(13195)
1413
29
1514
>>> solution(10)

Diff for: project_euler/problem_03/sol3.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
"""
88

99

10-
def solution(n: int) -> int:
10+
def solution(n: int = 600851475143) -> int:
1111
"""Returns the largest prime factor of a given number n.
12-
1312
>>> solution(13195)
1413
29
1514
>>> solution(10)
@@ -52,12 +51,8 @@ def solution(n: int) -> int:
5251
while n % i == 0:
5352
n = n / i
5453
i += 1
55-
5654
return int(ans)
5755

5856

5957
if __name__ == "__main__":
60-
# print(solution(int(input().strip())))
61-
import doctest
62-
63-
doctest.testmod()
58+
print(solution(int(input().strip())))

0 commit comments

Comments
 (0)