Program segment question for combinatorics

AI Thread Summary
The program segment consists of nested loops that increment a counter based on the values of the integer variables i, j, and k, with n being a predefined positive integer. Initially misunderstood as a simple multiplication, it is clarified that the loops serve to calculate the number of combinations of triples from a set of n+2 elements. The final result of the counter reflects the binomial coefficient, specifically #{n+2 \choose 3}, which counts the distinct ways to choose three elements from n+2 while ensuring the order of selection does not repeat. This approach guarantees that combinations are unique by limiting the range of subsequent selections. The discussion emphasizes the significance of nested loops in combinatorial calculations rather than mere arithmetic operations.
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}##.
 
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top