Skip to content

Commit bc606d1

Browse files
archaengelstokhos
authored andcommitted
Hacktoberfest 2020: Apply style guidelines for Project Euler problem_02 (TheAlgorithms#2898)
* 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
1 parent 47e3cc0 commit bc606d1

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

Diff for: project_euler/problem_02/sol1.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313

14-
def solution(n):
14+
def solution(n: int = 4000000) -> int:
1515
"""Returns the sum of all fibonacci sequence even elements that are lower
1616
or equals to n.
1717
@@ -28,13 +28,13 @@ def solution(n):
2828
"""
2929
i = 1
3030
j = 2
31-
sum = 0
31+
total = 0
3232
while j <= n:
3333
if j % 2 == 0:
34-
sum += j
34+
total += j
3535
i, j = j, i + j
3636

37-
return sum
37+
return total
3838

3939

4040
if __name__ == "__main__":

Diff for: project_euler/problem_02/sol2.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
"""
1212

1313

14-
def solution(n):
14+
def solution(n: int = 4000000) -> int:
1515
"""Returns the sum of all fibonacci sequence even elements that are lower
1616
or equals to n.
1717
1818
>>> solution(10)
19-
[2, 8]
19+
10
2020
>>> solution(15)
21-
[2, 8]
21+
10
2222
>>> solution(2)
23-
[2]
23+
2
2424
>>> solution(1)
25-
[]
25+
0
2626
>>> solution(34)
27-
[2, 8, 34]
27+
44
2828
"""
29-
ls = []
29+
even_fibs = []
3030
a, b = 0, 1
3131
while b <= n:
3232
if b % 2 == 0:
33-
ls.append(b)
33+
even_fibs.append(b)
3434
a, b = b, a + b
35-
return ls
35+
return sum(even_fibs)
3636

3737

3838
if __name__ == "__main__":

Diff for: project_euler/problem_02/sol3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313

14-
def solution(n):
14+
def solution(n: int = 4000000) -> int:
1515
"""Returns the sum of all fibonacci sequence even elements that are lower
1616
or equals to n.
1717

Diff for: project_euler/problem_02/sol4.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from decimal import Decimal, getcontext
1414

1515

16-
def solution(n):
16+
def solution(n: int = 4000000) -> int:
1717
"""Returns the sum of all fibonacci sequence even elements that are lower
1818
or equals to n.
1919
@@ -57,8 +57,8 @@ def solution(n):
5757

5858
index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2
5959
num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2)
60-
sum = num // 2
61-
return int(sum)
60+
total = num // 2
61+
return int(total)
6262

6363

6464
if __name__ == "__main__":

Diff for: project_euler/problem_02/sol5.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313

14-
def solution(n):
14+
def solution(n: int = 4000000) -> int:
1515
"""Returns the sum of all fibonacci sequence even elements that are lower
1616
or equals to n.
1717
@@ -27,19 +27,19 @@ def solution(n):
2727
44
2828
"""
2929

30-
a = [0, 1]
30+
fib = [0, 1]
3131
i = 0
32-
while a[i] <= n:
33-
a.append(a[i] + a[i + 1])
34-
if a[i + 2] > n:
32+
while fib[i] <= n:
33+
fib.append(fib[i] + fib[i + 1])
34+
if fib[i + 2] > n:
3535
break
3636
i += 1
37-
sum = 0
38-
for j in range(len(a) - 1):
39-
if a[j] % 2 == 0:
40-
sum += a[j]
37+
total = 0
38+
for j in range(len(fib) - 1):
39+
if fib[j] % 2 == 0:
40+
total += fib[j]
4141

42-
return sum
42+
return total
4343

4444

4545
if __name__ == "__main__":

0 commit comments

Comments
 (0)