Program segment question for combinatorics

  • Context: Undergrad 
  • Thread starter Thread starter Magenta55
  • Start date Start date
  • Tags Tags
    Combinatorics Program
Click For Summary
SUMMARY

The discussed program segment utilizes nested loops to calculate the number of combinations of three elements from a set of size n+2. The outer loop iterates from 1 to n, while the inner loops limit their ranges based on the current values of the outer loop variables. This results in a final count represented by the binomial coefficient #{n+2 \choose 3}, effectively counting unique triples without repetition. The segment serves as an illustrative example of nested loops in combinatorial contexts.

PREREQUISITES
  • Understanding of nested loops in programming
  • Familiarity with combinatorial mathematics
  • Knowledge of binomial coefficients
  • Basic programming concepts (variables, loops)
NEXT STEPS
  • Study the concept of binomial coefficients in detail
  • Learn about nested loop optimization techniques
  • Explore combinatorial algorithms and their applications
  • Investigate the mathematical foundations of permutations and combinations
USEFUL FOR

Students of computer science, programmers interested in algorithm design, and mathematicians focusing on combinatorial theory will benefit from this discussion.

Magenta55
Messages
4
Reaction score
0
Consider the following segment where i,j,k,n and counter are integer variables and the value of n (a positive integer) is set prior to this segment.

counter := 0
for i := 1 to n do
for j :=1 1 to i do
for k :=1 to j do
counter := counter + 1

I am so lost as to what this program is even doing. Can anyone help explain what is does? I just need to know what it does upon first execution, second execution, etc.. and I may be able to solve it from there.
 
Mathematics news on Phys.org
Magenta55 said:
Consider the following segment where i,j,k,n and counter are integer variables and the value of n (a positive integer) is set prior to this segment.

counter := 0
for i := 1 to n do
for j :=1 1 to i do
for k :=1 to j do
counter := counter + 1

I am so lost as to what this program is even doing. Can anyone help explain what is does? I just need to know what it does upon first execution, second execution, etc.. and I may be able to solve it from there.
It's a simple nested loop that will end up with counter = j*i*n, which is a waste of time since you could just do the multiplication. Where nested loops are useful is where you DO something useful inside the loops, so I assume this is just an example of a nested loop for study purposes.

EDIT: OOPS ... no, that's NOT what it is. I see now that the inner loops are using the outer loop variables for a limit, SO ... this is something like a bubble sort nested loop and can be used for something LIKE(*) # of combinations or permutations. I'd have to look more closely to see how it might relate to combinatorial forumlae, but basically it's just a nested loop which, as I said, uses the outer loop variable as the inner loop limit.

(*) LIKE is emphasized to mean "not necessarily exactly" since I have not looked at the results in detail.
 
Last edited:
This gives you ##{n+2 \choose 3}##. You have n+2 elements going from 1 to n+2. first you pick the element 2+i from range ##[3;n+2]##, then element 1+j from range ##[2;1+i]##, then k from ##[1;j]##. You are choosing all such possible combinations and by making the next choice in the range smaller than the last choice, you guarantee that you don't pick the same triple many times in a different order. The shifting starting point is necessary so that you have something to pick next, if you pick the first element at the first step, there won't be any left to pick in the next step, because in your formula the next index goes up to the previous one. So you are getting how many different triples can you take from n+2, which is the binomial coefficient ##{n+2 \choose 3}##.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K