Problem 1

Problem 1#

from typing import Iterable

from project_euler import get_problem_description

get_problem_description(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$.

This problem is perhaps the simplest on project euler. First, let’s devise a method for determining if a number is a multiple of another number.

def is_multiple_of(
    multiple: int,
    number: int,
) -> bool:
    return number % multiple == 0

Let’s see this function in action.

assert is_multiple_of(5, 5)
assert not is_multiple_of(2, 5)

Now, we should check our function with the simple question answer provided above.

def is_multiple_of_3_or_5(number: int) -> bool:
    return is_multiple_of(3, number) or is_multiple_of(5, number)


def get_numbers_multiple_of_3_or_5(numbers: Iterable[int]) -> Iterable[int]:
    return filter(is_multiple_of_3_or_5, (n for n in numbers))


sum(get_numbers_multiple_of_3_or_5(range(10)))
23

Our code looks good so far!

sum(get_numbers_multiple_of_3_or_5(range(1000)))
233168

And our answer is correct!