Skip to content

Commit c9a370a

Browse files
Merge pull request #2 from OddExtension5/Project_Euler_P01
Create sol8.py
2 parents 29e2071 + 6cd118d commit c9a370a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

project_euler/problem_01/sol8.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Problem Statement:
3+
If we list all the natural number below 20 that are multiples of 3 or 5,
4+
we get 3,5,6,9,10,12,15,18. The sum if these multiples is 78.
5+
6+
Find the sum of all the multiples of 3 or 5 below N.
7+
8+
"""
9+
10+
11+
def solution(n, target):
12+
"""
13+
Efficient Solution for Sum Multiple of 3 or 5
14+
15+
>>> solution(3)
16+
0
17+
>>> solution(4)
18+
3
19+
>>> solution(10)
20+
23
21+
>>> solution(-7)
22+
0
23+
>>> solution(7000)
24+
11432168
25+
>>> solution(100000000000000000000)
26+
2333333333333333333316666666666666666668
27+
>>> solution(7000000000000000000000000000000)
28+
11433333333333333333333333333332166666666666666666666666666668
29+
>>> solution(9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)
30+
23333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333321666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666669
31+
32+
Note : Non-efffcient solution display MemoryError
33+
"""
34+
if target > 0: # check for positive or negative
35+
p = target // n
36+
return (n * (p * (p + 1))) // 2
37+
else:
38+
return 0
39+
40+
41+
if __name__ == "__main__":
42+
N = int(input())
43+
m = N - 1
44+
print(solution(3, m) + solution(5, m) - solution(15, m))
45+
46+
# For detail explanation of the above problem head to the below link:
47+
# https://github.com/BigOh-Koders/INCF2019/blob/master/MUL35/explanation.pdf

0 commit comments

Comments
 (0)