Home > Blockchain >  Could someone explain the difference between and -- in a while loop? Here is my example
Could someone explain the difference between and -- in a while loop? Here is my example

Time:07-21

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!

CodePudding user response:

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

  • Related