Skip to content

Commit bc4c94c

Browse files
author
shiv
committed
started euler project problems. Problem 1 solved
1 parent 3aa6083 commit bc4c94c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

multiples_of_3_5.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# multiples of 3 and 5
2+
3+
'''
4+
EULER:
5+
6+
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.
7+
8+
Find the sum of all the multiples of 3 or 5 below 1000.
9+
10+
Performance:
11+
233168
12+
finished in: 0.0001709461212158203
13+
14+
Performance with extra condition (avoiding re computations):
15+
233168
16+
finished in: 9.894371032714844e-05
17+
18+
'''
19+
20+
import time
21+
22+
n = 1000
23+
24+
def get_multiples(n):
25+
multiples = set()
26+
for i in range(1,n):
27+
val = 3*i
28+
if val<n: multiples.add(val)
29+
else:break
30+
for i in range(1,n):
31+
# a condition to speed it up
32+
if i%3 == 0:
33+
# because all the previous multiples of 3 are covered
34+
continue
35+
val = 5*i
36+
if val<n: multiples.add(val)
37+
else:break
38+
return sum(multiples)
39+
40+
41+
start =time.time()
42+
print(get_multiples(n))
43+
print('finished in:', time.time()-start)

0 commit comments

Comments
 (0)