-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path001.py
More file actions
35 lines (24 loc) · 749 Bytes
/
001.py
File metadata and controls
35 lines (24 loc) · 749 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
"""
Project Euler Problem 1
=======================
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.
"""
def sum_multiples(f, n):
"""
Find the sum of all the multiples of `f` within `n`.
"""
return f * (n // f * (n // f + 1)) / 2
def sum_multiples_3_and_5(n):
"""
Find the sum of all the multiples of 3 or 5 below `n`.
"""
n -= 1
return (
sum_multiples(3, n) + sum_multiples(5, n) - sum_multiples(15, n)
)
def test_sum_multiples():
assert sum_multiples_3_and_5(10) == 23
assert sum_multiples_3_and_5(20) == 78
print(sum_multiples_3_and_5(1000))