Nice!

About the Math that seems confusing… See this:

How many divisions can (N-1) be splited into by 3 and 5? (-1 since we are looking for numbers "below" it).

var threes = Math.floor((N - 1) / 3);

var fives = Math.floor((N - 1) / 5);

Then, If we are accounting for numbers that are EITHER multiple by 3 and 5, `threes`

and `fives`

would be duplicated every 15 (3*5) steps (for instance: from 1–16, we have `[3,6,9,12,15]`

and `[5,10,15]`

, where '15' is duplicated in both. So we can find the amount of 15’s as well by:

var fifteens = Math.floor((N - 1) / 15);

After that, the total number of threes, fives and fifteens are discovered. If I'm not mistaken, just for a sake of understanding, the total "numbers" could be calculated as:

var totalNumbers = threes + fives - fifteens;

where the `-fifteens`

is used to account for duplicated numbers that are both 3/5 multiplers.

To continue, in order to know the SUM of those numbers (for instance: Given that we have 5 multiplers of 3; 3 multiplers of 5; and 1 multipler of fifteen; we can square it and multiply by the multipler (imagine that the bigger the number, even bigger the sum gets).

var sumThrees = (3 * threes * (threes + 1)) / 2;

For instance:`[3, 6, 9, 12, 15]`

, `(3*5*6)/2 = 45 = 3+6+9+12+15`

As you can see, this is basically, the "half-area" of a square scaled by the multiplier.

And to complete, we can do everything simplified:

var total =

+ 0.5 * ( 3 * threes * (threes + 1))

+ 0.5 * ( 5 * fives * (fives + 1))

- 0.5 * (15 * fifteen * (fifteen + 1));

And becomes in a much faster way (without floating point multiplication, and with the division placed as common divider outside parentesis:

(3 * threes * (threes + 1) + 5 * fives * (fives + 1) - 15 * fifteen * (fifteen + 1)) / 2;

Really clever.. And nice post!