Program to print binomial expansion series


Binomial expansion is a mathematical formula used to expand the expressions of the form (a+b)^n, where n is a positive integer and a and b can be any real or complex numbers. The expansion gives the coefficients of the terms in the expansion.

A binomial expansion can be represented as

$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$

where $\mathrm{^nC_r}$ are the binomial coefficients and is given by

$\mathrm{^nC_r=\frac{n!}{r!\times(n−r)!}}$ where n! is the factorial of n

The expansion can be used for calculating all the binomial terms using the formula above and putting it into the expansion equation.

Problem Statement

Given three integers a, b and n. Find the terms of the binomial expansion of (a+b)^n.

Sample Example 1

Input −

a = 1, b = 2, n = 3

Output −

[1, 6, 12, 8]

Explanation

Binomial expansion of (1+2)^3 is as follows

$\mathrm{(1+2)^3 = C(3,0)a^3b^0 + C(3,1)a^2b^1 + C(3,2)a^1b^2 + C(3,3)a^0b^3}$

= 1*1*1 + 3*1*2 + 3*1*4 + 1*1*8

Thus, [1, 6, 12, 8] are the terms of the binomial expansion.

Sample Example 2

Input −

a = 7, b = 2, n = 11

Output −

[2401, 2744, 1176, 224, 16]

Approach 1: Recursion Binomial Expansion

Using the binomial expansion formula,

$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$

We can find the value of each term by recursively calculating the binomial coefficients.

Pseudocode

procedure binomialCoeff (n, r)
   if r == 0 or r == n
      ans = 1
   else
      ans = binomialCoeff (n - 1, r - 1) + binomialCoeff (n - 1, r)
end procedure

procedure binomialTerms (a, b, n)
   Initialize vector: arr
   for r = 0 to n
      coeff = binomialCoeff(n, r)
      term = coeff + a^n-r + b^r
      add the term to arr
   ans = arr
end procedure

Example: C++ Implementation,

In the following program, binomialCoeff() function recursively calculates the value of rth binomial coefficient and the binomialTerms() function calculates the value of binomial terms in the expansion.

#include <bits/stdc++.h>
using namespace std;
// Function for calculating binomial coefficients
int binomialCoeff(int n, int r){
   if (r == 0 || r == n) {
      return 1;
   } else {
      return binomialCoeff(n - 1, r - 1) + binomialCoeff(n - 1, r);
   }
}

// Function for calculating the binomial terms
vector<int> binomialTerms(int a, int b, int n){
   vector<int> ans;
   for (int r = 0; r <= n; r++) {
   
      // Calculate the rth binomial coefficients
      int coeff = binomialCoeff(n, r);
      
      // Calculate the rth binomial expansion term
      int term = coeff * pow(a, n - r) * pow(b, r);
      ans.push_back(term);
   }
   return ans;
}
int main(){
   int a = 2, b = 3, n = 4;
   vector<int> res = binomialTerms(a, b, n);
   cout << "The binomial terms are : ";
   for (int i = 0; i < res.size(); i++) {
      cout << res[i] << " ";
   }
   return 0;
}

Output

The binomial terms are : 16 96 216 216 81

Time Complexity − O(2^n) where the time complexity of binomialCoeff() function is O(2^n) due to 2^n nodes in the recursive tree and the binomialTerms() function has a complexity of O(n^2) due to nested loop calling binomialCoeff() n+1 times. Thus overall complexity is O(2^n).

Space Complexity − O(n) due to the recursive call stack.

Approach 2: Iterative Binomial Expansion

Using the binomial expansion formula,

$$\mathrm{(a+b)^n= ^nC_0a^nb^0+ ^nC_1a^{n-1}b^1 + ^nCa^{n-2}b^2+...+ ^nC_ra^{n-r}b^r+...+ ^nC_na^0b^n}$$

We can find the value of each term of this expansion by combining iterations and divisions.

We’ll create 2 functions, where the first function calculates the binomial coefficients and the second function multiplies the powers of a and b to get the desired binomial terms.

Pseudocode

procedure binomialCoeff (n, r)
   res = 1
   if r > n - r
      r = n - r
   end if
   for i = 0 to r-1
      res = res * (n - i)
      res = res / (i + 1)
   ans = res
end procedure

procedure binomialTerms (a, b, n)
   Initialize vector: arr
   for r = 0 to n
      coeff = binomialCoeff(n, r)
      term = coeff + a^n-r + b^r
      add the term to arr
   ans = arr
end procedure

Example: C++ Implementation

In the following program, the binomialCoeff() function calculates the rth binomial coefficient and the binomialTerms() function calculates all the terms of the binomial expansion of given a, b and n.

#include <bits/stdc++.h>
using namespace std;
// Function for calculating binomial coefficients
int binomialCoeff(int n, int r){
   int res = 1;
   if (r > n - r)  {
      r = n - r;
   }
   for (int i = 0; i < r; i++) {
      res *= (n - i);
      res /= (i + 1);
   }
   return res;
}

// Function for calculating the binomial terms
vector<int> binomialTerms(int a, int b, int n){
   vector<int> ans;
   for (int r = 0; r <= n; r++){
   
      // Calculate the rth binomial coefficients
      int coeff = binomialCoeff(n, r);
      
      // Calculate the rth binomial expansion term
      int term = coeff * pow(a, n - r) * pow(b, r);
      ans.push_back(term);
   }
   return ans;
}
int main(){
   int a = 2, b = 3, n = 4;
   vector<int> res = binomialTerms(a, b, n);
   cout << "The binomial terms are : ";
   for (int i = 0; i < res.size(); i++){
      cout << res[i] << " ";
   }
   return 0;
}

Output

The binomial terms are : 16 96 216 216 81

Time Complexity − O(n^2), where binomialCoeff() function has the time complexity of O(r) where r is teh smaller number among r and n-r and the binomialTerms() function has a complexity of O(n^2) due to nested loop calling binomialCoeff() n+1 times. Thus overall complexity is O(n^2).

Space Complexity − O(n) due to the vector storing the binomial terms.

Conclusion

In conclusion, to find the binomial terms of a binomial expansion, we can implement either of the two above-mentioned approaches with time complexities ranging from O(2^n) to O(n^2) where the iterative approach is more optimized than the recursive approach.

Updated on: 25-Jul-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements