Skip to content

Commit f7d341e

Browse files
Create sol8.py
Efficient solution of the problem
1 parent 29e2071 commit f7d341e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

project_euler/problem_01/sol8.py

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

0 commit comments

Comments
 (0)