📜  二项式展开式系列的中期

📅  最后修改于: 2021-04-23 21:26:49             🧑  作者: Mango

给定三个整数A,X和n。任务是找到二项式展开式系列的中间项。
例子:

Input : A = 1, X = 1, n = 6
Output : MiddleTerm = 20

Input : A = 2, X = 4, n = 7
Output : MiddleTerm1 = 35840, MiddleTerm2 = 71680

方法

C++
// C++ program to find the middle term
// in binomial expansion series.
#include 
using namespace std;
 
// function to calculate
// factorial of a number
int factorial(int n)
{ 
    int fact = 1;
    for (int i = 1; i <= n; i++)
        fact *= i;
        
    return fact;
}
 
// Function to find middle term in
// binomial expansion series.
void findMiddleTerm(int A, int X, int n)
{
    int i, j, aPow, xPow;
    float middleTerm1, middleTerm2;
 
    if (n % 2 == 0)
    {
        // If n is even
         
        // calculating the middle term
        i = n / 2;
 
        // calculating the value of A to
        // the power k and X to the power k
        aPow = (int)pow(A, n - i);
        xPow = (int)pow(X, i);
 
        middleTerm1 = ((float)factorial(n) /
          (factorial(n - i) * factorial(i)))
                              * aPow * xPow;
                               
        cout << "MiddleTerm = "
             << middleTerm1 << endl;
    }
    else {
 
        // If n is odd
         
        // calculating the middle term
        i = (n - 1) / 2;
        j = (n + 1) / 2;
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = (int)pow(A, n - i);
        xPow = (int)pow(X, i);
 
        middleTerm1 = ((float)factorial(n) /
           (factorial(n - i) * factorial(i)))
                               * aPow * xPow;
 
        // calculating the value of A to the
        // power k and X to the power k
        aPow = (int)pow(A, n - j);
        xPow = (int)pow(X, j);
 
        middleTerm2 = ((float)factorial(n) /
           (factorial(n - j) * factorial(j)))
                               * aPow * xPow;
 
        cout << "MiddleTerm1 = "
                      << middleTerm1 << endl;
                       
        cout << "MiddleTerm2 = "
                      << middleTerm2 << endl;
    }
}
 
// Driver code
int main()
{
    int n = 5, A = 2, X = 3;
     
    // function call
    findMiddleTerm(A, X, n);
 
    return 0;
}


Java
// Java program to find the middle term
// in binomial expansion series.
import java.math.*;
 
class GFG {
 
    // function to calculate factorial
    // of a number
    static int factorial(int n)
    {
        int fact = 1, i;
        if (n == 0)
            return 1;
        for (i = 1; i <= n; i++)
            fact *= i;
             
        return fact;
    }
     
    // Function to find middle term in
    // binomial expansion series.
    static void findmiddle(int A, int X, int n)
    {
        int i, j, aPow, xPow;
        float middleTerm1, middleTerm2;
 
        if (n % 2 == 0)
        {
            // If n is even
             
            // calculating the middle term
            i = n / 2;
 
            // calculating the value of A to
            // the power k and X to the power k
            aPow = (int)Math.pow(A, n - i);
            xPow = (int)Math.pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
              (factorial(n - i) * factorial(i)))
                                  * aPow * xPow;
            System.out.println("MiddleTerm = "
                                 + middleTerm1);
        }
        else {
             
            // If n is odd
 
            // calculating the middle term
            i = (n - 1) / 2;
            j = (n + 1) / 2;
 
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.pow(A, n - i);
            xPow = (int)Math.pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
               (factorial(n - i) * factorial(i)))
                                   * aPow * xPow;
     
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.pow(A, n - j);
            xPow = (int)Math.pow(X, j);
     
            middleTerm2 = ((float)factorial(n) /
               (factorial(n - j) * factorial(j)))
                                   * aPow * xPow;
 
            System.out.println("MiddleTerm1 = "
                                  + middleTerm1);
                   
            System.out.println("MiddleTerm2 = "
                                  + middleTerm2);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 6, A = 2, X = 4;
 
        // calling the function
        findmiddle(A, X, n);
    }
}


Python3
# Python3 program to find the middle term
# in binomial expansion series.
import math
 
# function to calculate
# factorial of a number
def factorial(n) :
     
    fact = 1
    for i in range(1, n+1) :
        fact = fact * i
 
    return fact;
 
# Function to find middle term in
# binomial expansion series.
def findMiddleTerm(A, X, n) :
 
    if (n % 2 == 0) :
         
        # If n is even
         
        # calculating the middle term
        i = int(n / 2)
 
        # calculating the value of A to
        # the power k and X to the power k
        aPow = int(math.pow(A, n - i))
        xPow = int(math.pow(X, i))
 
        middleTerm1 = ((math.factorial(n) /
                       (math.factorial(n - i)
                       * math.factorial(i)))
                       * aPow * xPow)
                                 
        print ("MiddleTerm = {}" .
                     format(middleTerm1))
 
    else :
 
        # If n is odd
         
        # calculating the middle term
        i = int((n - 1) / 2)
        j = int((n + 1) / 2)
 
        # calculating the value of A to the
        # power k and X to the power k
        aPow = int(math.pow(A, n - i))
        xPow = int(math.pow(X, i))
 
        middleTerm1 = ((math.factorial(n)
                    / (math.factorial(n - i)
                    * math.factorial(i)))
                        * aPow * xPow)
 
        # calculating the value of A to the
        # power k and X to the power k
        aPow = int(math.pow(A, n - j))
        xPow = int(math.pow(X, j))
 
        middleTerm2 = ((math.factorial(n)
                   / (math.factorial(n - j)
                   * math.factorial(j)))
                      * aPow * xPow)
 
        print ("MiddleTerm1 = {}" .
               format(int(middleTerm1)))
                         
        print ("MiddleTerm2 = {}" .
               format(int(middleTerm2)))
 
# Driver code
n = 5
A = 2
X = 3
 
# function call
findMiddleTerm(A, X, n)
 
# This code is contributed by
# manishshaw1.


C#
// C# program to find the middle term
// in binomial expansion series.
using System;
 
class GFG {
 
    // function to calculate factorial
    // of a number
    static int factorial(int n)
    {
        int fact = 1, i;
        if (n == 0)
            return 1;
        for (i = 1; i <= n; i++)
            fact *= i;
             
        return fact;
    }
     
    // Function to find middle term in
    // binomial expansion series.
    static void findmiddle(int A, int X, int n)
    {
        int i, j, aPow, xPow;
        float middleTerm1, middleTerm2;
 
        if (n % 2 == 0)
        {
            // If n is even
             
            // calculating the middle term
            i = n / 2;
 
            // calculating the value of A to
            // the power k and X to the power k
            aPow = (int)Math.Pow(A, n - i);
            xPow = (int)Math.Pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
            (factorial(n - i) * factorial(i)))
                                * aPow * xPow;
            Console.WriteLine("MiddleTerm = "
                                + middleTerm1);
        }
        else {
             
            // If n is odd
 
            // calculating the middle term
            i = (n - 1) / 2;
            j = (n + 1) / 2;
 
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.Pow(A, n - i);
            xPow = (int)Math.Pow(X, i);
     
            middleTerm1 = ((float)factorial(n) /
            (factorial(n - i) * factorial(i)))
                                * aPow * xPow;
     
            // calculating the value of A to the
            // power k and X to the power k
            aPow = (int)Math.Pow(A, n - j);
            xPow = (int)Math.Pow(X, j);
     
            middleTerm2 = ((float)factorial(n) /
            (factorial(n - j) * factorial(j)))
                                * aPow * xPow;
 
            Console.WriteLine("MiddleTerm1 = "
                                + middleTerm1);
                 
            Console.WriteLine("MiddleTerm2 = "
                                + middleTerm2);
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5, A = 2, X = 3;
 
        // calling the function
        findmiddle(A, X, n);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出:
MiddleTerm1 = 720
MiddleTerm2 = 1080