-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_of_multiples.py
More file actions
37 lines (31 loc) · 958 Bytes
/
sum_of_multiples.py
File metadata and controls
37 lines (31 loc) · 958 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
"""
Sum of Multiples
"""
def sum_of_multiples(limit, multiples):
"""
sum of all the unique multiples of particular numbers up to
but not including that number
arg limit: (int) limit number
arg multiples: (list) list of factors
return: (int) sum of multiples
"""
# # simple
# sum = 0
# for index in range(limit):
# for number in multiples:
# if number:
# if index % number == 0:
# sum+=index
# break
# return sum
# # improve efficiency, using a set,
# # range increment and sum.
# values = set()
# for number in multiples:
# if number:
# for index in range(number,limit,number):
# values.add(index)
# return sum(values)
# the same but using set comprehension
return sum({index for number in multiples if number
for index in range(number, limit, number)})