给定三个整数A、X和n,任务是打印以下二项表达式系列的项。 (A+X) N = N C 0 A. N 十、 0 + N C 1. A. n-1 十、 1. + N C 2. A. n-2 十、 2. +….+ N C N A. 0 十、 N 例如:
null
Input : A = 1, X = 1, n = 5Output : 1 5 10 10 5 1Input : A = 1, B = 2, n = 6Output : 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-rXrSo 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 <bits/stdc++.h> 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
<?php // PHP program to print // terms of binomial // series and also // calculate sum of series. // function to calculate // factorial of a number function factorial( $n ) { $f = 1; for ( $i = 2; $i <= $n ; $i ++) $f *= $i ; return $f ; } // function to print the series function series( $A , $X , $n ) { // calculating the // value of n! $nFact = factorial( $n ); // loop to display // the series for ( $i = 0; $i < $n + 1; $i ++) { // 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 echo ( $nFact * $aPow * $xPow ) / ( $niFact * $iFact ) , " " ; } } // Driver Code $A = 3; $X = 4; $n = 5; series( $A , $X , $n ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to print terms of binomial // series and also calculate sum of series. // function to calculate factorial of // a number function factorial(n) { let f = 1; for (let i = 2; i <= n; i++) f *= i; return f; } // function to print the series function series(A, X, n) { // calculating the value of n! let nFact = factorial(n); // loop to display the series for (let i = 0; i < n + 1; i++) { // For calculating the // value of nCr let niFact = factorial(n - i); let iFact = factorial(i); // calculating the value of // A to the power k and X to // the power k let aPow = Math.pow(A, n - i); let xPow = Math.pow(X, i); // display the series document.write((nFact * aPow * xPow) / (niFact * iFact) + " " ); } } // Driver Code let A = 3, X = 4, n = 5; series(A, X, n); // This code is contributed by chinmoy1997pal. </script> |
输出:
243 1620 4320 5760 3840 1024
高效的解决方案: 这个想法是用上一个术语计算下一个术语。我们可以在O(1)时间内计算下一项。我们使用下面的属性 二项式系数 . N C i+1 = N C 我 *(n-i)/(i+1)
C++
// CPP program to print terms of binomial // series and also calculate sum of series. #include <bits/stdc++.h> 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
<?php // PHP program to print // terms of binomial // series and also // calculate sum of // series. // function to print // the series function series( $A , $X , $n ) { // Calculating and printing // first term $term = pow( $A , $n ); echo $term , " " ; // Computing and printing // remaining terms for ( $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 ); echo $term , " " ; } } // Driver Code $A = 3; $X = 4; $n = 5; series( $A , $X , $n ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to print terms of binomial // series and also calculate sum of series. // function to print the series function series(A, X, n) { // Calculating and printing first term let term = Math.pow(A, n); document.write(term + " " ); // Computing and printing remaining terms for (let 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); document.write(term + " " ); } } // main function started let A = 3, X = 4, n = 5; series(A, X, n); // This code is contributed by Surbhi Tyagi. </script> |
输出:
243 1620 4320 5760 3840 1024
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END