Two-polynomial coefficient recursion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.