How to implement trinomial extension without nested loops.

I'm in the process of writing an equation simplification program. In this program, they want to use binomial and trinomial theorems.

With binomial extension:

(x + y) ^ r

Sum (k → r) x ^ [rk] y ^ [k],

where k is 0 and r is the degree of the binomial.

I can do it like this:

  for (k=0; k<=r; k++) {
      x_degree=r-k;
      y_degree=k;  
  } 

Otherwise, if I want to implement trinomial theorems, I must satisfy the constraints of the form:

(a + b + c) ^ n

Sum (n selects i, j, k) a ^ i b ^ jc ^ k,

where n is the degree of trinomial and i + j + k = n.

I have been thinking about this for a while, but I cannot understand something better than looping through all the possible combinations, as shown below:

for (int i=0; i<=n; i++)
    for (int j=0; j<=n; j++)
        for (int k=0; k<=n; k++) {
            if((i+j+k)==n) {
                find_coefficient(i,j,k);
                set_degree_values(i,j,k);
                proceed();
            } 
        } 

So, my questions are: how to implement three-dimensional extension without going through all possible combinations of degrees?

Thank.

+4
2

,

004, 013, 022, 031, 040, 
103, 112, 121, 130, 
202, 211, 220,
301, 310, 
400

- . r, reset ( ).

n . , . ( , .)

+1

:

for (int i=0; i<=n; i++)
    for (int j=0; j<=n-i; j++) {   // Note change to upper limit
        int k = n - i - j;         // Calculate k rather than loop
        find_coefficient(i,j,k);
        set_degree_values(i,j,k);
        proceed(); 
        } 
+1

Source: https://habr.com/ru/post/1654479/


All Articles