# Problem 1: Multiples of 3 and 5

Problem 1: Multiples of 3 and 5
0

#1

Tell us what’s happening:

``````
var sum = 0;
function multiplesOf3and5(number) {
// Good luck!

for(var i = 1; i < number; i++){
if((i % 3 === 0 )||(i % 5 === 0)||(i % 3 === 0 && i % 5 === 0)){
sum = sum + i;
}
}
return sum;
}

multiplesOf3and5(1000);
``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/66.0.3359.181 Chrome/66.0.3359.181 Safari/537.36`.

#2

first thought: why do you start from i=1 ? The function is called multiples of 3 and 5, therefore it seems logical to start from 3?

Multiples of three { 3, 6, 9…}
Multiples of five {5, 10, 15…}

Also you’ve defined a global variable which seems like overkill. Move the sum variable down into the function scope.

So then you can use console.log to print out the value of sum as you go and change 1000 to something like 20 so it is a manageable set of results for you to debug.
(open the console window for your browser to see what you are logging and then determine any further action based on what your log statement gives you)

hope this helps.

#3

Try moving

`var sum = 0;`

inside the multipliesOf3and5 function. I think this global variable is messing up the tests.

#4

I should also point out that for this if statement, the last term of your if statement is not needed. That is because when we use logical OR , the first term to give true shortcuts the if statement. So if the number 15 for eg is a multiple of 3, it will return after the first term returns true right away. If the number is not a multiple (say 10), then it will return after the second term right away. So the extra check is not needed.

#5

This is for Project Euler, right?

Project Euler problems are designed to have a trick to solving them more efficiently

I’ll give you a clue for this one:

`3 + 6 + 9 + ... = 3 * (1 + 2 + 3 + ...)`

Similarly for `5`s case. See if you can see how to use this fact to solve the problems in constant time

#6

Thank you for your help,but I already solve it after posting it in forum.

#7

#8

It’s worth redoing it in a more efficient way

In a sense it’s the point of PE questions

You can reduce your `O(n)` solution down to `O(1)`! That’s worth doing!

#9

Thanks… This is pretty helpful to reduce complexity.