Project Euler (Multiples of 3 or 5)

Hey all,

I am still pretty new to programming and was suggested to try out Project Euler, I have just started the first problem ( Multiples of 3 or 5 ) and I have no idea how to do it… If someone could help me understand what the question is asking an how to start solving it that would be great.

Thanks,
Life.py

^^This is the question^^

Please don’t post screenshots of text, copy/paste the text. Screenshots are hard for the visually impaired and people on email (they do not receive the screenshot).

Can you write a loop which iterates over all the numbers below 10? Below 100? Just printing them out?

Can you test whether a number is divisble by 3? How?

Something that you need to decide is how much computation/thinking you want to leave to the computer and how much you will do yourself. One extreme is to note that the result is one number. You could compute it and make your program be a function that only returns that number. At the other extreme is the computer doing all or nearly all the thinking/computing for you. There are also intermediate positions. For example, it is possible to represent the required sum as a fixed number of arithmetic operations on the the upper limit n=1000. You could do the thinking/computing of that formula, and then make the computer only evaluate that formula for n=1000.

Some tools:

  1. 1+2+3+...+n=\frac{n(n+1)}{2}
  2. Therefore, 3+6+9+...+3n=3\frac{n(n+1)}{2}, 5+10+15+...+5n=5\frac{n(n+1)}{2} and similar formulas for sums of all multiples of any other integer.
  3. Inclusion-exclusion The required sum is the sum of all multiples of 3, plus the sum of all multiples of 5, minus the sum of all multiples of \operatorname{lcm}(3,5)=3\cdot 5=15.
  4. The largest multiple of 3 that is less than n is 3 * floor(n / 3) or 3 * (n // 3).
2 Likes

If the first formula seems like magic, then look at it like this:

1 +   2 +  3 +  4 +  5 +
10 +  9 +  8 +  7 +  6 =
11 + 11 + 11 + 11 + 11 = 5 * 11 = (10 / 2) * (1 + 10)