-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_001.py
More file actions
28 lines (26 loc) · 842 Bytes
/
Copy pathproblem_001.py
File metadata and controls
28 lines (26 loc) · 842 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
# Project Euler Problem 1: Multiples of 3 and 5
"""
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.
"""
# Brute -force approach
# Count from 1 to 1000, check if number is divisible by 3 or 5. if true, add to sum.
def sum_of_multiples_bf(limit):
sum = 0
for a in range(1,limit):
if(a % 3 == 0 or a % 5 == 0):
sum += a
return sum
print(sum_of_multiples_bf(1000))
# Find multiples of 3 and 5, subtract numbers that are counted twice.
def sum_of_multiples_ms(limit):
sum = 0
for i in range(3,limit,3):
sum += i
for j in range(5,limit,5):
sum += j
for k in range(15, limit, 15):
sum -= k
return sum
print(sum_of_multiples_ms(1000))