-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiples_of_3_5.py
More file actions
43 lines (32 loc) · 910 Bytes
/
multiples_of_3_5.py
File metadata and controls
43 lines (32 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# multiples of 3 and 5
'''
EULER:
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.
Performance:
233168
finished in: 0.0001709461212158203
Performance with extra condition (avoiding re computations):
233168
finished in: 9.894371032714844e-05
'''
import time
n = 1000
def get_multiples(n):
multiples = set()
for i in range(1,n):
val = 3*i
if val<n: multiples.add(val)
else:break
for i in range(1,n):
# a condition to speed it up
if i%3 == 0:
# because all the previous multiples of 3 are covered
continue
val = 5*i
if val<n: multiples.add(val)
else:break
return sum(multiples)
start =time.time()
print(get_multiples(n))
print('finished in:', time.time()-start)