📜  程序打印二项式展开式

📅  最后修改于: 2021-04-26 06:25:16             🧑  作者: Mango

给定三个整数A,X和n,任务是打印以下二项式表达式系列的项。

(A + X) n = n C 0 A n X 0 + n C 1 A n-1 X 1 + n C 2 A n-2 X 2 +…。+ n C n A 0 X n

例子:

Input : A = 1, X = 1, n = 5
Output : 1 5 10 10 5 1

Input : A = 1, B = 2, n = 6
Output : 1 12 60 160 240 192 64 

简单的解决方案:我们知道对于n的每个值,在二项式序列中将有(n + 1)个项。因此,现在我们使用一种简单的方法,计算系列中每个元素的值并打印出来。

nCr = (n!) / ((n-r)! * (r)!)

Below is value of general term. 
Tr+1 = nCn-rAn-rXr
So at each position we have to find the value 
of the general term and print that term .
C++
// CPP program to print terms of binomial
// series and also calculate sum of series.
#include 
using namespace std;
 
// function to calculate factorial of
// a number
int factorial(int n)
{
    int f = 1;
    for (int i = 2; i <= n; i++)
        f *= i;       
    return f;
}
 
// function to print the series
void series(int A, int X, int n)
{    
    // calculating the value of n!
    int nFact = factorial(n);
 
    // loop to display the series
    for (int i = 0; i < n + 1; i++) {
         
        // For calculating the
        // value of nCr
        int niFact = factorial(n - i);
        int iFact = factorial(i);
 
        // calculating the value of
        // A to the power k and X to
        // the power k
        int aPow = pow(A, n - i);
        int xPow = pow(X, i);
 
        // display the series
        cout << (nFact * aPow * xPow) /
                 (niFact * iFact) << " ";
    }
}
 
// main function started
int main()
{
    int A = 3, X = 4, n = 5;
    series(A, X, n);
    return 0;
}


Java
// Java program to print terms of binomial
// series and also calculate sum of series.
 
import java.io.*;
 
class GFG {
     
    // function to calculate factorial of
    // a number
    static int factorial(int n)
    {
        int f = 1;
        for (int i = 2; i <= n; i++)
            f *= i;
             
        return f;
    }
 
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // calculating the value of n!
        int nFact = factorial(n);
 
        // loop to display the series
        for (int i = 0; i < n + 1; i++) {
 
            // For calculating the
            // value of nCr
            int niFact = factorial(n - i);
            int iFact = factorial(i);
 
            // calculating the value of
            // A to the power k and X to
            // the power k
            int aPow = (int)Math.pow(A, n - i);
            int xPow = (int)Math.pow(X, i);
 
            // display the series
            System.out.print((nFact * aPow * xPow)
                         / (niFact * iFact) + " ");
        }
    }
 
    // main function started
    public static void main(String[] args)
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to print terms of binomial
# series and also calculate sum of series.
 
# function to calculate factorial
# of a number
def factorial(n):
 
    f = 1
    for i in range(2, n+1):
        f *= i
         
    return f
 
# Function to print the series
def series(A, X, n):
     
    # calculating the value of n!
    nFact = factorial(n)
 
    # loop to display the series
    for i in range(0, n + 1):
         
        # For calculating the
        # value of nCr
        niFact = factorial(n - i)
        iFact = factorial(i)
 
        # calculating the value of
        # A to the power k and X to
        # the power k
        aPow = pow(A, n - i)
        xPow = pow(X, i)
 
        # display the series
        print (int((nFact * aPow * xPow) /
                   (niFact * iFact)), end = " ")
     
# Driver Code
A = 3; X = 4; n = 5
series(A, X, n)
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to print terms of binomial
// series and also calculate sum of series.
using System;
 
class GFG {
     
    // function to calculate factorial of
    // a number
    static int factorial(int n)
    {
        int f = 1;
        for (int i = 2; i <= n; i++)
            f *= i;
             
        return f;
    }
 
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // calculating the value of n!
        int nFact = factorial(n);
 
        // loop to display the series
        for (int i = 0; i < n + 1; i++) {
 
            // For calculating the
            // value of nCr
            int niFact = factorial(n - i);
            int iFact = factorial(i);
 
            // calculating the value of
            // A to the power k and X to
            // the power k
            int aPow = (int)Math.Pow(A, n - i);
            int xPow = (int)Math.Pow(X, i);
 
            // display the series
            Console.Write((nFact * aPow * xPow)
                     / (niFact * iFact) + " ");
        }
    }
 
    // main function started
    public static void Main()
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// CPP program to print terms of binomial
// series and also calculate sum of series.
#include 
using namespace std;
 
// function to print the series
void series(int A, int X, int n)
{
    // Calculating and printing first term
    int term = pow(A, n);
    cout << term << " ";
 
    // Computing and printing remaining terms
    for (int i = 1; i <= n; i++) {
 
        // Find current term using previous terms
        // We increment power of X by 1, decrement
        // power of A by 1 and compute nCi using
        // previous term by multiplying previous
        // term with (n - i + 1)/i
        term = term * X * (n - i + 1)/(i * A);
 
        cout << term << " ";
    }
}
 
// main function started
int main()
{
    int A = 3, X = 4, n = 5;
    series(A, X, n);
    return 0;
}


Java
// Java program to print terms of binomial
// series and also calculate sum of series.
 
import java.io.*;
 
class GFG {
     
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // Calculating and printing first
        // term
        int term = (int)Math.pow(A, n);
        System.out.print(term + " ");
 
        // Computing and printing
        // remaining terms
        for (int i = 1; i <= n; i++) {
 
            // Find current term using
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute
            // nCi using previous term by
            // multiplying previous term
            // with (n - i + 1)/i
            term = term * X * (n - i + 1)
                                / (i * A);
 
            System.out.print(term + " ");
        }
    }
 
    // main function started
    public static void main(String[] args)
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by vt_m.


Python3
# Python 3 program to print terms of binomial
# series and also calculate sum of series.
 
# Function to print the series
def series(A, X, n):
 
    # Calculating and printing first term
    term = pow(A, n)
    print(term, end = " ")
 
    # Computing and printing remaining terms
    for i in range(1, n+1):
 
        # Find current term using previous terms
        # We increment power of X by 1, decrement
        # power of A by 1 and compute nCi using
        # previous term by multiplying previous
        # term with (n - i + 1)/i
        term = int(term * X * (n - i + 1)/(i * A))
 
        print(term, end = " ")
     
# Driver Code
A = 3; X = 4; n = 5
series(A, X, n)
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to print terms of binomial
// series and also calculate sum of series.
 
using System;
 
public class GFG {
     
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // Calculating and printing first
        // term
        int term = (int)Math.Pow(A, n);
        Console.Write(term + " ");
 
        // Computing and printing
        // remaining terms
        for (int i = 1; i <= n; i++) {
 
            // Find current term using
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute
            // nCi using previous term by
            // multiplying previous term
            // with (n - i + 1)/i
            term = term * X * (n - i + 1)
                                / (i * A);
 
          Console.Write(term + " ");
        }
    }
 
    // main function started
    public static void Main()
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出:
243 1620 4320 5760 3840 1024 

高效的解决方案:
这个想法是使用上一个术语来计算下一个术语。我们可以计算O(1)时间中的下一项。我们使用下面的二项式系数的属性。
n C i + 1 = n C i *(ni)/(i + 1)

C++

// CPP program to print terms of binomial
// series and also calculate sum of series.
#include 
using namespace std;
 
// function to print the series
void series(int A, int X, int n)
{
    // Calculating and printing first term
    int term = pow(A, n);
    cout << term << " ";
 
    // Computing and printing remaining terms
    for (int i = 1; i <= n; i++) {
 
        // Find current term using previous terms
        // We increment power of X by 1, decrement
        // power of A by 1 and compute nCi using
        // previous term by multiplying previous
        // term with (n - i + 1)/i
        term = term * X * (n - i + 1)/(i * A);
 
        cout << term << " ";
    }
}
 
// main function started
int main()
{
    int A = 3, X = 4, n = 5;
    series(A, X, n);
    return 0;
}

Java

// Java program to print terms of binomial
// series and also calculate sum of series.
 
import java.io.*;
 
class GFG {
     
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // Calculating and printing first
        // term
        int term = (int)Math.pow(A, n);
        System.out.print(term + " ");
 
        // Computing and printing
        // remaining terms
        for (int i = 1; i <= n; i++) {
 
            // Find current term using
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute
            // nCi using previous term by
            // multiplying previous term
            // with (n - i + 1)/i
            term = term * X * (n - i + 1)
                                / (i * A);
 
            System.out.print(term + " ");
        }
    }
 
    // main function started
    public static void main(String[] args)
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by vt_m.

Python3

# Python 3 program to print terms of binomial
# series and also calculate sum of series.
 
# Function to print the series
def series(A, X, n):
 
    # Calculating and printing first term
    term = pow(A, n)
    print(term, end = " ")
 
    # Computing and printing remaining terms
    for i in range(1, n+1):
 
        # Find current term using previous terms
        # We increment power of X by 1, decrement
        # power of A by 1 and compute nCi using
        # previous term by multiplying previous
        # term with (n - i + 1)/i
        term = int(term * X * (n - i + 1)/(i * A))
 
        print(term, end = " ")
     
# Driver Code
A = 3; X = 4; n = 5
series(A, X, n)
 
# This code is contributed by Smitha Dinesh Semwal.

C#

// C# program to print terms of binomial
// series and also calculate sum of series.
 
using System;
 
public class GFG {
     
    // function to print the series
    static void series(int A, int X, int n)
    {
         
        // Calculating and printing first
        // term
        int term = (int)Math.Pow(A, n);
        Console.Write(term + " ");
 
        // Computing and printing
        // remaining terms
        for (int i = 1; i <= n; i++) {
 
            // Find current term using
            // previous terms We increment
            // power of X by 1, decrement
            // power of A by 1 and compute
            // nCi using previous term by
            // multiplying previous term
            // with (n - i + 1)/i
            term = term * X * (n - i + 1)
                                / (i * A);
 
          Console.Write(term + " ");
        }
    }
 
    // main function started
    public static void Main()
    {
        int A = 3, X = 4, n = 5;
         
        series(A, X, n);
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出:
243 1620 4320 5760 3840 1024