Skip to content

Fix style of the first ten solutions for Project Euler #3242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions project_euler/problem_001/sol1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.
"""
Returns the sum of all the multiples of 3 or 5 below n.
>>> solution(3)
0
Expand All @@ -25,4 +30,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
13 changes: 9 additions & 4 deletions project_euler/problem_001/sol2.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.
"""
Returns the sum of all the multiples of 3 or 5 below n.
>>> solution(3)
0
Expand All @@ -30,4 +35,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
10 changes: 7 additions & 3 deletions project_euler/problem_001/sol3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


Expand Down Expand Up @@ -57,4 +61,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
99 changes: 52 additions & 47 deletions project_euler/problem_001/sol4.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
"""
Problem Statement:
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

xmulti = []
zmulti = []
z = 3
x = 5
temp = 1
while True:
result = z * temp
if result < n:
zmulti.append(result)
temp += 1
else:
temp = 1
break
while True:
result = x * temp
if result < n:
xmulti.append(result)
temp += 1
else:
break
collection = list(set(xmulti + zmulti))
return sum(collection)


if __name__ == "__main__":
print(solution(int(input().strip())))
"""
Project Euler Problem 1: https://projecteuler.net/problem=1

Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""
Returns the sum of all the multiples of 3 or 5 below n.

>>> solution(3)
0
>>> solution(4)
3
>>> solution(10)
23
>>> solution(600)
83700
"""

xmulti = []
zmulti = []
z = 3
x = 5
temp = 1
while True:
result = z * temp
if result < n:
zmulti.append(result)
temp += 1
else:
temp = 1
break
while True:
result = x * temp
if result < n:
xmulti.append(result)
temp += 1
else:
break
collection = list(set(xmulti + zmulti))
return sum(collection)


if __name__ == "__main__":
print(f"{solution() = }")
15 changes: 10 additions & 5 deletions project_euler/problem_001/sol5.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.
A straightforward pythonic solution using list comprehension.
"""
Returns the sum of all the multiples of 3 or 5 below n.
A straightforward pythonic solution using list comprehension.
>>> solution(3)
0
Expand All @@ -24,4 +29,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
13 changes: 9 additions & 4 deletions project_euler/problem_001/sol6.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.
"""
Returns the sum of all the multiples of 3 or 5 below n.
>>> solution(3)
0
Expand All @@ -31,4 +36,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
13 changes: 9 additions & 4 deletions project_euler/problem_001/sol7.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""
Problem Statement:
Project Euler Problem 1: https://projecteuler.net/problem=1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Find the sum of all the multiples of 3 or 5 below 1000.
"""


def solution(n: int = 1000) -> int:
"""Returns the sum of all the multiples of 3 or 5 below n.
"""
Returns the sum of all the multiples of 3 or 5 below n.
>>> solution(3)
0
Expand All @@ -29,4 +34,4 @@ def solution(n: int = 1000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
25 changes: 16 additions & 9 deletions project_euler/problem_002/sol1.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"""
Problem:
Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
Project Euler Problem 2: https://projecteuler.net/problem=2
1,2,3,5,8,13,21,34,55,89,..
Even Fibonacci Numbers
Each new term in the Fibonacci sequence is generated by adding the previous
two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
10.
four million, find the sum of the even-valued terms.
References:
- https://en.wikipedia.org/wiki/Fibonacci_number
"""


def solution(n: int = 4000000) -> int:
"""Returns the sum of all fibonacci sequence even elements that are lower
or equals to n.
"""
Returns the sum of all even fibonacci sequence elements that are lower
or equal to n.
>>> solution(10)
10
Expand All @@ -26,6 +32,7 @@ def solution(n: int = 4000000) -> int:
>>> solution(34)
44
"""

i = 1
j = 2
total = 0
Expand All @@ -38,4 +45,4 @@ def solution(n: int = 4000000) -> int:


if __name__ == "__main__":
print(solution(int(input().strip())))
print(f"{solution() = }")
85 changes: 46 additions & 39 deletions project_euler/problem_002/sol2.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
"""
Problem:
Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:

1,2,3,5,8,13,21,34,55,89,..

By considering the terms in the Fibonacci sequence whose values do not exceed
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
10.
"""


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

>>> solution(10)
10
>>> solution(15)
10
>>> solution(2)
2
>>> solution(1)
0
>>> solution(34)
44
"""
even_fibs = []
a, b = 0, 1
while b <= n:
if b % 2 == 0:
even_fibs.append(b)
a, b = b, a + b
return sum(even_fibs)


if __name__ == "__main__":
print(solution(int(input().strip())))
"""
Project Euler Problem 2: https://projecteuler.net/problem=2

Even Fibonacci Numbers

Each new term in the Fibonacci sequence is generated by adding the previous
two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed
four million, find the sum of the even-valued terms.

References:
- https://en.wikipedia.org/wiki/Fibonacci_number
"""


def solution(n: int = 4000000) -> int:
"""
Returns the sum of all even fibonacci sequence elements that are lower
or equal to n.

>>> solution(10)
10
>>> solution(15)
10
>>> solution(2)
2
>>> solution(1)
0
>>> solution(34)
44
"""

even_fibs = []
a, b = 0, 1
while b <= n:
if b % 2 == 0:
even_fibs.append(b)
a, b = b, a + b
return sum(even_fibs)


if __name__ == "__main__":
print(f"{solution() = }")
Loading