How to obtain the result of n(n-1)(n-2) / 6
Question:
In my Python book, the question asks to prove the value of x
after running the following code:
x = 0
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
x += 1
What I could see is that:
i = 0; j=1; k=2: from 2 to n, x+=1, (n-2) times 1
i = 1; j=2; k=3: from 3 to n, x+=1, (n-3) times 1
...
i=n-3; j=n-2; k=n-1: from n-1 to n, x+=1, just 1
i=n-2; j=n-1; k=n doesn't add 1
So it seems that the x
is the sum of series of (n-2) + (n-3) + … + 1?
I am not sure how to get to the answer of n(n-1)(n-2)/6
.
Answers:
Just write the for
loops as a sigma: S = sum_{i=1}^n sum_{j=i+1}^n sum_{k = j + 1}^n (1)
.
Try to expand the sum from inner to outer:
S = sum_{i=1}^n sum_{j=i+1}^n (n - j) = sum_{i=1}^n n(n-i) - ((i+1) + (i+2) + ... + n) = sum_{i=1}^n n(n-i) - ( 1+2+...+n - (1+2+...+i)) = sum_{i=1}^n n(n-i) -(n(n+1)/2 - i(i+1)/2) = sum_{i=1}^n n(n+1)/2 + i(i+1)/2 - n*i = n^2(n+1)/2 + sum_{i=1}^n (i^2/2 + i/2 - n*i)
.
If open this sum and simplify it (it is straightforward) you will get S = n(n-1)(n-2)/6
.
One way to view this is that you have n
values and three nested loops which are constructed to have non-overlapping ranges. Thus the number of iterations possible is equal to the number of ways to choose three unique values from n
items, or n choose 3
= n!/(3!(n-3)!)
= n(n-1)(n-2)/3*2*1
= n(n-1)(n-2)/6
.
This is a mathematical model of the algorithm, known as binomial coefficient (combinations). In analysis of algorithms you can use this form : N^k/k!
In your code you have a case without repetition that’s why n*(n -1 )*(n -2 ) / 3!
In my Python book, the question asks to prove the value of x
after running the following code:
x = 0
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
x += 1
What I could see is that:
i = 0; j=1; k=2: from 2 to n, x+=1, (n-2) times 1
i = 1; j=2; k=3: from 3 to n, x+=1, (n-3) times 1
...
i=n-3; j=n-2; k=n-1: from n-1 to n, x+=1, just 1
i=n-2; j=n-1; k=n doesn't add 1
So it seems that the x
is the sum of series of (n-2) + (n-3) + … + 1?
I am not sure how to get to the answer of n(n-1)(n-2)/6
.
Just write the for
loops as a sigma: S = sum_{i=1}^n sum_{j=i+1}^n sum_{k = j + 1}^n (1)
.
Try to expand the sum from inner to outer:
S = sum_{i=1}^n sum_{j=i+1}^n (n - j) = sum_{i=1}^n n(n-i) - ((i+1) + (i+2) + ... + n) = sum_{i=1}^n n(n-i) - ( 1+2+...+n - (1+2+...+i)) = sum_{i=1}^n n(n-i) -(n(n+1)/2 - i(i+1)/2) = sum_{i=1}^n n(n+1)/2 + i(i+1)/2 - n*i = n^2(n+1)/2 + sum_{i=1}^n (i^2/2 + i/2 - n*i)
.
If open this sum and simplify it (it is straightforward) you will get S = n(n-1)(n-2)/6
.
One way to view this is that you have n
values and three nested loops which are constructed to have non-overlapping ranges. Thus the number of iterations possible is equal to the number of ways to choose three unique values from n
items, or n choose 3
= n!/(3!(n-3)!)
= n(n-1)(n-2)/3*2*1
= n(n-1)(n-2)/6
.
This is a mathematical model of the algorithm, known as binomial coefficient (combinations). In analysis of algorithms you can use this form : N^k/k!
In your code you have a case without repetition that’s why n*(n -1 )*(n -2 ) / 3!