-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_1.py
49 lines (31 loc) · 862 Bytes
/
problem_1.py
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
44
45
46
47
48
49
"""
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.
"""
import time
#Attempt 1
start_time = time.time()
total = 0
for i in range(1, 1000):
if i % 3 == 0 or i % 5 == 0:
total = total + i
print total
print time.time() - start_time, "seconds\n"
"""
These 'improved' versions are only really more efficient with numbers
larger than 1000
"""
#Improvement 1
start_time = time.time()
total = 0
for i in xrange(1, 1000):
if i % 3 == 0 or i % 5 == 0:
total = total + i
print total
print time.time() - start_time, "seconds\n"
#Improvement 2
start_time = time.time()
total = sum([i for i in xrange(1, 1000) if i % 3 == 0 or i % 5 == 0])
print total
print time.time() - start_time, "seconds\n"