1

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?

Dimitar
  • 4,402
  • 4
  • 31
  • 47
  • 2
    Sigma (Σ) stands for sum in maths, not for loops (are you referring to complexity?) – Kostas Kryptos May 07 '15 at 20:58
  • Why would it be a sum ? And what do you mean by `the condition is unnecessary` ? By the way, with the condition, this is not equivalent – Dici May 07 '15 at 20:58
  • 2
    Whether it's a sum or anything else is dependent on the code in the body of the loop. In your example, the looping of `j` and `k` are unrelated, so the summation formula indicating `j > k` is irrelavant to the nested loops. – Weather Vane May 07 '15 at 21:05
  • @Dici so what is the equivalent to this concrete example `for(int i = 0; i <= (x / 2); i++){ first = first.add( fac(x - i).divide((fac(x - 2*i).multiply(fac(i)))) ); for(int j = 0; j <= (y / 2); j++){ second = second.add( fac(y - j).divide((fac(y - 2*j).multiply(fac(j)))) ); } }` – Dimitar May 07 '15 at 21:05
  • this falls to mathematics - check this related post: http://math.stackexchange.com/questions/750275/convert-nested-for-loop-to-mathematical-expression – Kostas Kryptos May 07 '15 at 21:05
  • 2
    @user4325010 are you trying to change the question in comment? – Weather Vane May 07 '15 at 21:06
  • I want for i = 0 to combine it with j = {0,..., n} and to SUM it up with the next i = 1 for j = {0,..., n}? Then is it sigma(s)? – Dimitar May 07 '15 at 21:07
  • Is your commented code even C? – Weather Vane May 07 '15 at 21:09
  • @WeatherVane No I give an example to clarify it, because I have found that I am not good with explanations. Actually the code is in Java, but my question is in general. – Dimitar May 07 '15 at 21:09
  • I am not sure if I get the question correctly. But, I would say that the code you are showing should be expressed in time complexity if you are trying to move code into maths. In that case, the time complexity of this algorithm would be O(n*m) @user4325010 – Llogari Casas May 07 '15 at 21:19
  • I'm voting to close this question as off-topic because the topic is fundamentally about mathematical representation of code, which is more of a CS theory question than a programming question. Answers to the question must therefore expound on CS theory rather than programming concepts. – user268396 May 07 '15 at 21:23

2 Answers2

3

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.

Shay Gold
  • 403
  • 4
  • 14
1

Sigma stands for summation, which means that if you're dealing with sigma for a range, i=1,n, which is defined as x, then the result is going to be x * n (x + x + x + ... + x n times). Transcribed to pseudocode, it would be like this:

result = 0
for i=1,n:
     result = result + x

So it doesn't really translate to a general for loop which is more about doing something a certain number or times or until a condition is met.

Often when you see mathematicians researching algorithms that relate directly to software fields, they use the more flexible functional notation and recursion a lot more than summation since such a functional notation actually translates a bit more directly to general loop computations than summation.