[Solved] Formula for counting loop iteration


I have a feeling that you are requiring a formula to get number of iteration the nested loops will.

  for( i = 1; i <= n; i++ )
     for ( j = i+1; j <= n; j++ )
        for( k = j+1; k <= n; k++ )

For those loops, the number of iterations will be:

(n*(n-1)*(n-2))/6, where n > 2.

Generic formula for above kind nested loops:

(n*(n-1)* ... *(n-r+1)) / r!, where n > r-1.

Here, r = number of nested loops

For example: when n = 20 and r = 3, number of iterations will be = (20*19*18) / 3! = 1140

3

solved Formula for counting loop iteration