-1

So i am trying to solve this problem: Given a number, count how many numbers below it are multiples of EITHER 3 OR 5 Then, sum those multiples together

Ex: sumMultiples(10) -> 3 + 5 + 6 + 9 -> returns 23

Ex: countOdd(5) -> 1, 3 -> returns 2

======= The solution:

var sumMultiples = function(n) {
  
  let counter = 0
  
  while (n--) {
 if (n % 3 === 0 || n % 5 === 0) {
   counter += n
  }
  }
  return counter
}

SumMultiples(10)

What i am confused about is, i understand (n--) in a while loop will count down the numbers with each iteration.......going with that logic, if i was to do (n++), it would instead count up with each iteration until it hits 9, but that doesn't work.

So my question is, can someone please explain the difference between having it as (n--) and (n++). What is happening exactly so i can see the difference? I tried googling but couldn't understand.

Also, for example i can probably use FOR loop as well here with i++?

Thank you for your time!

Axnyc
  • 117
  • 6
  • 5
    _"until it hits 9"_ - why do you think this? – evolutionxbox Jul 21 '22 at 09:04
  • 2
    The reason for `n--` is that when `n` is zero `n--` will be evaluated as false and quit the loop. – phuzi Jul 21 '22 at 09:05
  • The `n++` version would require another variable to keep track of the current number... `var i = 0; while (++i < n) {` which is basically the same as the for loop version! Note that it also use a pre-increment – phuzi Jul 21 '22 at 09:09
  • @Kooilinc Why that duplicate? this question is about increment vs decrement, not about pre vs post. – Barmar Jul 21 '22 at 09:12
  • @Barmar you're right. Will reopen. – KooiInc Jul 21 '22 at 09:13
  • Ha, thank you evolutionxbox, i clearly had my loops fully confused. Iv been studying the for loop so much that in my head i assumed there was a break condition and did not understand that n++ had no break condition and would create an infinite loop. Everything is a lot more clear. Also the purpose of --, now i understand why it makes sense and more convenient to use when possible. – Axnyc Jul 21 '22 at 10:07

1 Answers1

1

The idea of a while condition is that at some point it should evaluate to a falsy value (or else the body of the loop should have an exit point, but that is not the case here). For number-type values, like n, the only possible falsy values are 0 or NaN.

When using n-- as while condition, and n is a non-negative number(!) we can be sure that at a certain point it will evaluate to 0, which is the moment the while loop will stop.

If however you would use n++ as condition, and n is a non-negative number, we will not ever reach 0. On the contrary, the value will just keep increasing (until Infinity), and at every step the while condition will be truthy (not falsy). It represents an infinite loop.

You can indeed write it as a for loop, using another loop variable, with a condition that guarantees that the loop will finish when the loop variable has reached the value of n:

function sumMultiples(n) {
    let counter = 0;
  
    for (let i = 0; i < n; i++) {
        if (i % 3 === 0 || i % 5 === 0) {
            counter += i;
        }
    }
    return counter;
}

console.log(sumMultiples(10)); // 23

Performance

Both versions of the code are not optimal. For very large n it is inefficient to check each separate value between 0 and n. Consider that we know how many numbers below n divide by 3... and which they are. We can use the formula for triangular numbers to get their sum without iteration.

So we could quickly get the sum of all numbers that divide by 3 and those that divide by 5 and add these two results. However, care has to be taken for numbers that divide by both 3 and 5, i.e. those that divide by 15. We would count them double. The solution is then to sum the numbers that divide by 15 and subtract that sum from the earlier sum.

Implementation:

function sumSteps(step, n) {
    n = Math.floor((n - 1) / step);
    return step * n * (n + 1) / 2; // Triangular formula
}

function sumMultiples(n) {
    return sumSteps(3, n) + sumSteps(5, n) - sumSteps(15, n);
}

console.log(sumMultiples(10)); // 23
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you so much for such a detailed answer. I really appreciate it. It cleared up a lot of things for me, especially the diff between ++ and -- which i didn't understand. That makes sense. I am new to Stack overflow, if i can add something to your reputation or help any way for this answer that i haven't done yet, please let me know. – Axnyc Jul 21 '22 at 10:05