Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) 0 >>> solution(4) 3 >>> solution(10) 23 >>> solution(600) 83700
(n: int = 1000)
| 11 | |
| 12 | |
| 13 | def solution(n: int = 1000) -> int: |
| 14 | """ |
| 15 | Returns the sum of all the multiples of 3 or 5 below n. |
| 16 | |
| 17 | >>> solution(3) |
| 18 | 0 |
| 19 | >>> solution(4) |
| 20 | 3 |
| 21 | >>> solution(10) |
| 22 | 23 |
| 23 | >>> solution(600) |
| 24 | 83700 |
| 25 | """ |
| 26 | |
| 27 | total = 0 |
| 28 | terms = (n - 1) // 3 |
| 29 | total += ((terms) * (6 + (terms - 1) * 3)) // 2 # total of an A.P. |
| 30 | terms = (n - 1) // 5 |
| 31 | total += ((terms) * (10 + (terms - 1) * 5)) // 2 |
| 32 | terms = (n - 1) // 15 |
| 33 | total -= ((terms) * (30 + (terms - 1) * 15)) // 2 |
| 34 | return total |
| 35 | |
| 36 | |
| 37 | if __name__ == "__main__": |