# Project Euler #1: Multiples of 3 and 5

#### Original problem statement

If we list all the natural numbers below ** 10** that are multiples of

**or**

*3***, we get**

*5***,**

*3***,**

*5***and**

*6***. The sum of these multiples is**

*9***.**

*23*Find the sum of all the multiples of

**or**

*3***5**below

**.**

*1000**Original link from **ProjectEuler**.*

#### Generic version

Find the sum of all the multiples of ** 3** or

**below a given number**

*5*

*N.***Input Format:**

First line contains ** T**, the number of test cases. followed by

**lines each containing an integer**

*T***.**

*N***Constraints:**

*1 ≤ N ≤ 10⁹.*

*Link to the programming version from **Hackerrank**.*

#### Analysis

Before reading through this section, I encourage you to think of a possible solution of your own first ~🎀

The simple approach would be to iterate from ** 1** to

**and sum up any multiple of**

*N***or**

*3***5**, here’s a bonus simple code in C for it:

That’s obviously an ** O(N)** algorithm, can we do better ? Yes!

**kind of better.**

*O(1)*Let’s start by finding the sum of all multiples of

**below**

*3*

*N:**(1) ***Triangle number, **can also be expressed using the explicit format:

Same way, we define the sum of multiples of ** 5** below

**:**

*N*Now, you may think that the **Sum of all the multiples of 3 or 5 **is

*S = S₃ + S₅*, but that’s wrong, given that:

** 15, 30, ...** and all the common multiples of

**and**

*3***will be summed twice.**

*5*This can be be fixed by subtracting those common multiples. So a correct solution would be:

*S = S₃ + S₅ -(all common multiples of 3 and 5).*

The question now is, how to calculate those common multiples?

Any common multiple of A and B is a multiple of the least common multiple

We can get the least common multiple, using the below identity, then we calculate the sum of the duplicate numbers,

The correct solution will be then *S = S₃ + S₅ *−* S₁₅*

#### Implementation

- Using Python

Show your love and support by clicking the clap 👏