📜  二项式系数之和

📅  最后修改于: 2021-05-04 17:01:10             🧑  作者: Mango

给定一个正整数n ,任务是找到二项式系数的和,即
n C 0 + n C 1 + n C 2 +……。 + n C n-1 + n C n
例子:

Input : n = 4
Output : 16
4C0 + 4C1 + 4C2 + 4C3 + 4C4
= 1 + 4 + 6 + 4 + 1
= 16

Input : n = 5
Output : 32

方法1(蛮力):
想法是评估每个二项式系数项,即n C r ,其中0 <= r <= n并计算所有项的总和。
以下是此方法的实现:

C++
// CPP Program to find the sum of Binomial
// Coefficient.
#include 
using namespace std;
 
// Returns value of Binomial Coefficient Sum
int binomialCoeffSum(int n)
{
    int C[n + 1][n + 1];
 
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= min(i, n); j++) {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
 
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
 
    // Calculating the sum.
    int sum = 0;
    for (int i = 0; i <= n; i++)
        sum += C[n][i];
 
    return sum;
}
 
/* Driver program to test above function*/
int main()
{
    int n = 4;
    printf("%d", binomialCoeffSum(n));
    return 0;
}


Java
// Java Program to find the sum
// of Binomial Coefficient.
 
class GFG {
     
    // Returns value of Binomial
    // Coefficient Sum
    static int binomialCoeffSum(int n)
    {
        int C[][] = new int[n + 1][n + 1];
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= Math.min(i, n); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
     
                // Calculate value using previously
                // stored values
                else
                    C[i][j] = C[i - 1][j - 1] +
                              C[i - 1][j];
             
                 
            }
        }
     
        // Calculating the sum.
        int sum = 0;
        for (int i = 0; i <= n; i++)
            sum += C[n][i];
     
        return sum;
    }
     
    /* Driver program to test above function*/
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(binomialCoeffSum(n));
    }
}
 
// This code is contributed by prerna saini.


Python3
# Python  Program to find the sum
# of Binomial Coefficient.
  
import math   
  
# Returns value of Binomial
# Coefficient Sum
def binomialCoeffSum( n):
     
        C = [[0]*(n+2) for i in range(0,n+2)]
      
        # Calculate value of Binomial
        # Coefficient in bottom up manner
        for i in range(0,n+1):
            for j in range(0, min(i, n)+1):
             
                # Base Cases
                if (j == 0 or j == i):
                    C[i][j] = 1
      
                # Calculate value using previously
                # stored values
                else:
                    C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
      
        # Calculating the sum.
        sum = 0
        for i in range(0,n+1):
            sum += C[n][i]
      
        return sum
     
      
# Driver program to test above function
n = 4
print(binomialCoeffSum(n))
 
# This code is contributed by Gitanjali.


C#
// C# program to find the sum
// of Binomial Coefficient.
using System;
 
class GFG {
 
    // Returns value of Binomial
    // Coefficient Sum
    static int binomialCoeffSum(int n)
    {
        int[, ] C = new int[n + 1, n + 1];
 
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= Math.Min(i, n); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i, j] = 1;
 
                // Calculate value using previously
                // stored values
                else
                    C[i, j] = C[i - 1, j - 1] + C[i - 1, j];
            }
        }
 
        // Calculating the sum.
        int sum = 0;
        for (int i = 0; i <= n; i++)
            sum += C[n, i];
 
        return sum;
    }
 
    /* Driver program to test above function*/
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(binomialCoeffSum(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program to find sum of Binomial
// Coefficient.
#include 
using namespace std;
 
// Returns value of Binomial Coefficient Sum
// which is 2 raised to power n.
int binomialCoeffSum(int n)
{
    return (1 << n);
}
 
/* Driver program to test above function*/
int main()
{
    int n = 4;
    printf("%d", binomialCoeffSum(n));
    return 0;
}


Java
// Java Program to find sum
// of Binomial Coefficient.
import java.io.*;
 
class GFG
{
    // Returns value of Binomial
    // Coefficient Sum which is
    // 2 raised to power n.
    static int binomialCoeffSum(int n)
    {
        return (1 << n);
    }
 
    // Driver Code
    public static void main (String[] args)
    {
        int n = 4;
        System.out.println(binomialCoeffSum(n));
    }
}
 
// This code is contributed
// by akt_mit.


Python3
# Python  Program to find the sum
# of Binomial Coefficient.
  
import math    
# Returns value of Binomial
# Coefficient Sum
def binomialCoeffSum( n):
     
    return (1 << n);
 
# Driver program to test
# above function
n = 4
print(binomialCoeffSum(n))
 
# This code is contributed
# by Gitanjali.


C#
// C# Program to find sum of
// Binomial Coefficient.
using System;
 
class GFG {
 
    // Returns value of Binomial Coefficient Sum
    // which is 2 raised to power n.
    static int binomialCoeffSum(int n)
    {
        return (1 << n);
    }
 
    /* Driver program to test above function*/
    static public void Main()
    {
        int n = 4;
        Console.WriteLine(binomialCoeffSum(n));
    }
}
 
// This code is contributed by vt_m.


PHP


输出:

16

方法2(使用公式):

这可以用两种方式证明。
第一个证明:使用归纳原理。

第二证明:使用二项式定理展开

以下是此方法的实现:

C++

// CPP Program to find sum of Binomial
// Coefficient.
#include 
using namespace std;
 
// Returns value of Binomial Coefficient Sum
// which is 2 raised to power n.
int binomialCoeffSum(int n)
{
    return (1 << n);
}
 
/* Driver program to test above function*/
int main()
{
    int n = 4;
    printf("%d", binomialCoeffSum(n));
    return 0;
}

Java

// Java Program to find sum
// of Binomial Coefficient.
import java.io.*;
 
class GFG
{
    // Returns value of Binomial
    // Coefficient Sum which is
    // 2 raised to power n.
    static int binomialCoeffSum(int n)
    {
        return (1 << n);
    }
 
    // Driver Code
    public static void main (String[] args)
    {
        int n = 4;
        System.out.println(binomialCoeffSum(n));
    }
}
 
// This code is contributed
// by akt_mit.

Python3

# Python  Program to find the sum
# of Binomial Coefficient.
  
import math    
# Returns value of Binomial
# Coefficient Sum
def binomialCoeffSum( n):
     
    return (1 << n);
 
# Driver program to test
# above function
n = 4
print(binomialCoeffSum(n))
 
# This code is contributed
# by Gitanjali.

C#

// C# Program to find sum of
// Binomial Coefficient.
using System;
 
class GFG {
 
    // Returns value of Binomial Coefficient Sum
    // which is 2 raised to power n.
    static int binomialCoeffSum(int n)
    {
        return (1 << n);
    }
 
    /* Driver program to test above function*/
    static public void Main()
    {
        int n = 4;
        Console.WriteLine(binomialCoeffSum(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出:

16