Search code examples
javacfor-loopnestedmathematical-expressions

Mathematical equivalent of multiple nested loops


When you have loop in the loop or nested loop, let`s say for-loop, regardless of the programming language(of course must be imperative)

for(int j = 1; j <= 100; j++){
  for(int k = 1; k <= 200; k++){
   \\body or terms
  }
}

is the mathematical equivalent, when I want to sum it for i = 1 with all j = {1, 200} and i = 2 with again all j = {1, 200} and so on :

enter image description here

And the red-circled condition is unnecessary, right?

And the same applies for multiple nested loops?


Solution

  • The code you provided will run as you explained

    sum it for i = 1 with all j = {1, 200} and i = 2 with again all j = {1, 200} and so on

    However, the mathematical equivalent is without the condition marked in red.

    The sigmas with the condition is equivalent to this code:

    for(int j = 1; j <= 100; j++){
      for(int k = 1; k < j; k++){
       \\body or terms
      }
    }
    

    Hope I helped.