Skip to content

Commit 8b572e6

Browse files
suadjelilicclauss
authored andcommitted
added solution 7 for problem_01 (TheAlgorithms#1416)
* added solution 7 for problem_01 * added solution 5 for problem_02
1 parent ce7faa5 commit 8b572e6

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

project_euler/problem_01/sol7.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
"""
7+
8+
9+
def solution(n):
10+
"""Returns the sum of all the multiples of 3 or 5 below n.
11+
12+
>>> solution(3)
13+
0
14+
>>> solution(4)
15+
3
16+
>>> solution(10)
17+
23
18+
>>> solution(600)
19+
83700
20+
"""
21+
22+
result = 0
23+
for i in range(n):
24+
if i % 3 == 0:
25+
result += i
26+
elif i % 5 == 0:
27+
result += i
28+
return result
29+
30+
31+
if __name__ == "__main__":
32+
print(solution(int(input().strip())))

project_euler/problem_02/sol5.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Problem:
3+
Each new term in the Fibonacci sequence is generated by adding the previous two
4+
terms. By starting with 1 and 2, the first 10 terms will be:
5+
6+
1,2,3,5,8,13,21,34,55,89,..
7+
8+
By considering the terms in the Fibonacci sequence whose values do not exceed
9+
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
10+
10.
11+
"""
12+
13+
14+
def solution(n):
15+
"""Returns the sum of all fibonacci sequence even elements that are lower
16+
or equals to n.
17+
18+
>>> solution(10)
19+
10
20+
>>> solution(15)
21+
10
22+
>>> solution(2)
23+
2
24+
>>> solution(1)
25+
0
26+
>>> solution(34)
27+
44
28+
"""
29+
30+
a = [0,1]
31+
i = 0
32+
while a[i] <= n:
33+
a.append(a[i] + a[i+1])
34+
if a[i+2] > n:
35+
break
36+
i += 1
37+
sum = 0
38+
for j in range(len(a) - 1):
39+
if a[j] % 2 == 0:
40+
sum += a[j]
41+
42+
return sum
43+
44+
45+
if __name__ == "__main__":
46+
print(solution(int(input().strip())))

0 commit comments

Comments
 (0)