Skip to content

Commit 6359875

Browse files
committed
Code cleanup and fixes using Black and linters
1 parent 8dc0579 commit 6359875

File tree

5 files changed

+57
-50
lines changed

5 files changed

+57
-50
lines changed

Diff for: p001.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
# Multiples of 3 and 5
2-
# Problem 1
3-
# https://projecteuler.net/problem=1
1+
"""
2+
Problem 1.
43
5-
sum = 0
4+
Multiples of 3 and 5
5+
https://projecteuler.net/problem=1
6+
"""
67

78
# Pretty self-exlanatory code.
89
# Check if given number is dividable by 3 or 5.
910
# Rinse and repeat for 1 to 1000.
1011

12+
answer = 0
13+
1114
for i in range(1000):
1215
if i % 3 == 0 or i % 5 == 0:
13-
sum += i
16+
answer += i
1417

15-
print(sum)
18+
print(answer)

Diff for: p002.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
# Even Fibonacci numbers
2-
# Problem 2
3-
# https://projecteuler.net/problem=2
1+
"""
2+
Problem 2.
3+
4+
Even Fibonacci numbers
5+
https://projecteuler.net/problem=2
6+
"""
47

58
# Basic example of using a recursion.
69

10+
711
def fibonacci(n):
8-
"""
9-
Find the nth fibonacci number.
10-
"""
12+
"""Return the nth fibonacci number."""
1113
if n == 1:
1214
return 1
13-
elif n == 2:
15+
if n == 2:
1416
return 2
15-
else:
16-
# Follow the definition.
17-
return fibonacci(n - 1) + fibonacci(n - 2)
17+
# Follow the definition.
18+
return fibonacci(n - 1) + fibonacci(n - 2)
1819

1920

20-
sum = 0
21+
answer = 0
2122
n = 1
2223

23-
while (fibonacci(n) < 4000000):
24+
while fibonacci(n) < 4000000:
2425
if fibonacci(n) % 2 == 0:
25-
sum += fibonacci(n)
26+
answer += fibonacci(n)
2627
n += 1
2728

28-
print(sum)
29+
print(answer)

Diff for: p003.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Largest prime factor
2-
# Problem 3
3-
# https://projecteuler.net/problem=3
1+
"""
2+
Problem 3.
3+
4+
Largest prime factor
5+
https://projecteuler.net/problem=3
6+
"""
7+
48

59
def is_prime(n):
6-
"""
7-
Find if the given number n is prime.
8-
"""
10+
"""Determine whether the given integer n is prime."""
911
result = True
1012
for i in range(n - 1, 2, -1):
1113
if n % i == 0:
@@ -25,7 +27,7 @@ def is_prime(n):
2527
# However, we will not use sqrt() function!
2628
# We can just compare the quotient with the divisor.
2729

28-
while (600851475143 // current_number >= current_number):
30+
while 600851475143 // current_number >= current_number:
2931
# We first check if current_number can divide 600851475143.
3032
# If 600851475143 is divisible
3133
# then we check if current_number is a prime, with the help of is_prime(n).

Diff for: p004.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# Largest palindrome product
2-
# Problem 4
3-
# https://projecteuler.net/problem=4
1+
"""
2+
Problem 4.
3+
4+
Largest palindrome product
5+
https://projecteuler.net/problem=4
6+
"""
47

5-
def is_palindrome(n):
6-
"""
7-
Find if n is palindrome.
8-
"""
98

9+
def is_palindrome(n):
10+
"""Determine whether n is palindrome."""
1011
# Convert the given number into list.
1112
number_original = list(map(int, str(n)))
1213

@@ -15,14 +16,11 @@ def is_palindrome(n):
1516
# Then, reverse the list.
1617
number_reversed.reverse()
1718

18-
return (number_original == number_reversed)
19+
return number_original == number_reversed
1920

2021

2122
def is_prod_of_two_3_digit_num(n):
22-
"""
23-
Find if n is the product of 3-digit numbers.
24-
"""
25-
23+
"""Determine whether n is the product of 3-digit numbers."""
2624
result = False
2725

2826
for i in range(100, 1000):

Diff for: p005.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
# Smallest multiple
2-
# Problem 5
3-
# https://projecteuler.net/problem=5
1+
"""
2+
Problem 5.
3+
4+
Smallest multiple
5+
https://projecteuler.net/problem=5
6+
"""
47

5-
# For integer a, b, it holds that
6-
# a * b = LCM(a, b) * GCD(a, b).
7-
# Starting from range(1, 21), we replace two head elements x, y to LCM(x, y).
88

99
def gcd(a, b):
1010
"""
1111
Find the greatest common divisor of two integers a, b.
12-
This uses Euclid's algorithm.
12+
13+
This uses Euclid's algorithm. For integer a, b, it holds that
14+
a * b = LCM(a, b) * GCD(a, b).
15+
Starting from range(1, 21), we replace two head elements x, y to LCM(x, y).
1316
"""
1417
dividend = max(a, b)
1518
divisor = min(a, b)
1619

1720
if divisor == 0:
1821
return dividend
19-
else:
20-
return gcd(dividend % divisor, divisor)
22+
23+
return gcd(dividend % divisor, divisor)
2124

2225

23-
lcm_list = range(1, 21)
26+
lcm_list = [*range(1, 21)]
2427

2528
while len(lcm_list) != 1:
26-
lcm_head = (lcm_list[0] * lcm_list[1]) / gcd(lcm_list[0], lcm_list[1])
29+
lcm_head = (lcm_list[0] * lcm_list[1]) // gcd(lcm_list[0], lcm_list[1])
2730
del lcm_list[0:2]
2831
lcm_list.insert(0, lcm_head)
2932

0 commit comments

Comments
 (0)