Two-polynomial coefficient recursion
The result of this algorithm is that the values of N and K are given, and the value of the two-item coefficients is calculated according to the formula .
Algorithm Purpose : practice using recursive algorithms
So what is recursion ?
In an algorithm, if there is a process that calls itself directly or calls itself indirectly, it is a recursive algorithm.
Recursive steps :
The 1> corresponds to one or more termination conditions for some parameter evaluation.
2> a recursive step. It evaluates to the current value based on a previous value. The recursive step eventually results in a termination condition.
As an example:
The recursion of a power function has a terminating condition, that is, when n=0. The recursive steps describe the general situation:
After the introduction of recursion, we introduce some two-item content .
In elementary mathematics, we have learned about some of the properties of the two-item formula, and here are the two items we need:
OK, the preparation is done, now we have the program!
Main program thought:
Termination condition: by two-item coefficient property (1), when i=0 or i=n, returns 1
Recursive step: By the two-item coefficient property (2), otherwise, the subscript minus 1, superscript unchanged =n1, subscript minus 1, superscript minus 1=n2, to call themselves.
Body Code: (C language)
int binom (int n,int i) {int n1;int n2;if ((i = = 0) | | (i = = N)) {return 1;} else {n1 = Binom (n-1,i); n2 = Binom (n-1,i-1); return n1+n2;}}
Finally, we enclose all the code:
#include <stdio.h>int binom (int n,int i), int main () {int int1;int int2;printf ("\nenter an integer: \ n"), scanf ("%d", &INT1);p rintf ("\nenter a second integer: \ n"), scanf ("%d", &int2);p rintf ("\ n");p rintf ("Binomial coefficiant:% D\n ", Binom (Int1,int2)); return 0;} int binom (int n,int i) {int n1;int n2;if ((i = = 0) | | (i = = N)) {return 1;} else {n1 = Binom (n-1,i); n2 = Binom (n-1,i-1); return n1+n2;}}
Run:
All right, it's over, I'll teach you a lot.
Two-polynomial coefficient recursion