📜  帕斯卡三角形中直到N行的所有元素的总和

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

由于连续数n,任务是计算每一行向上的所有元素的总和n行。
例子:

Input  : 2
Output : 7 
Explanation:  row 0 have element 1
              row 1 have elements 1, 1
              row 2 have elements 1, 2, 1
              so, sum will be ((1) + (1 + 1) + (1 + 2 + 1)) = 7

Input  : 4
Output : 31
Explanation:  row 0 have element 1
              row 1 have elements 1, 1
              row 2 have elements 1, 2, 1
              row 3 have elements 1, 3, 3, 1
              row 4 have elements 1, 4, 6, 4, 1
              so, sum will be ((1) + (1 + 1) + (1 + 2 + 1)
              + (1 + 3 + 3 + 1) + (1 + 4 + 6 + 4 + 1)) = 31

以下是具有11行的Pascal三角形的示例:

Pascal's triangle
                                                                  
0th row                             1                                
1st row                           1   1                              
2nd row                         1   2   1                            
3rd row                       1   3   3   1                          
4th row                     1   4   6   4   1                        
5th row                   1   5   10  10  5   1                     
6th row                 1   6   15  20  15  6   1                   
7th row               1   7   21  35  35  21  7   1                  
8th row             1  8   28   56  70   56  28  8  1                
9th row           1   9  36  84  126  126  84  36  9  1              
10th row        1  10  45  120 210  256  210 120 45 10  1            

天真的方法:在Pascal三角形中,一行的每个条目都是二项式系数的值。因此,一种简单的解决方案是生成直到第n行的所有行元素并将其相加。但是这种方法将具有O(n 3 )时间复杂度。但是,可以将其优化到O(n 2 )时间复杂度。请参考以下文章以生成Pascal三角形的元素:

  • 帕斯卡三角形

更好的解决方案:让我们看一下帕斯卡的三角形图案

sum of elements in ith row
0th row                             1                                1    -> 20
1st row                           1   1                              2    -> 21
2nd row                         1   2   1                            4    -> 22
3rd row                       1   3   3   1                          8    -> 23
4th row                     1   4   6   4   1                        16   -> 24
5th row                   1   5   10  10  5   1                      32   -> 25
6th row                 1   6   15  20  15  6   1                    64   -> 26
7th row               1   7   21  35  35  21  7   1                  128  -> 27
8th row             1  8   28   56  70   56  28  8  1                256  -> 28
9th row           1   9  36  84  126  126  84  36  9  1              512  -> 29
10th row        1  10  45  120 210  256  210 120 45 10  1            1024 -> 210

如上所示,第i行中的元素之和等于2 I。现在,通过加2的幂可以很容易地计算出直到n行的所有元素的总和。
下面是上述方法的实现:

C++
// C++ program to find sum of all elements
// upto nth row in Pascal triangle.
#include 
using namespace std;
 
// Function to find sum of aal elements
// upto nth row.
long long int calculateSum(int n)
{
 
    // Initialize sum with 0
    long long int sum = 0;
 
    // Loop to calculate power of 2
    // upto n and add them
    for (int row = 0; row < n; row++) {
        sum = sum + (1 << row);
    }
 
    return sum;
}
 
// Driver function
int main()
{
    int n = 10;
    cout << " Sum of all elements:" << calculateSum(n);
    return 0;
}


Java
// Java program to find sum of all elements
// upto nth row in Pascal triangle.
import java.io.*;
 
class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Loop to calculate power of 2
        // upto n and add them
        for (int row = 0; row < n; row++) {
            sum = sum + (1 << row);
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println("Sum of all elements:"
                           + calculateSum(n));
    }
}


Python3
# Python program to find sum of all elements
# upto nth row in Pascal triangle.
 
# Function to find sum of aal elements
# upto nth row.
def calculateSum(n) :
         
    # Initialize sum with 0
    sum = 0
     
    # Loop to calculate power of 2
    # upto n and add them
    for row in range(n):
        sum = sum + (1 << row)
 
    return sum
     
# Driver code   
n = 10
print("Sum of all elements:", calculateSum(n))


C#
// C# program to find sum of all elements
// upto nth row in Pascal triangle.
using System;
 
public class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Loop to calculate power of 2
        // upto n and add them
        for (int row = 0; row < n; row++) {
            sum = sum + (1 << row);
        }
 
        return sum;
    }
 
    static public void Main()
    {
        int n = 10;
        Console.WriteLine("Sum of all elements:"
                          + calculateSum(n));
    }
}


PHP


Javascript


C++
// C++ program to find sum of all elements
// upto nth row in Pascal triangle.
#include 
using namespace std;
 
// Function to find sum of aal elements
// upto nth row.
long long int calculateSum(int n)
{
 
    // Initialize sum with 0
    long long int sum = 0;
 
    // Calculate 2^n
    sum = 1 << n;
 
    return (sum - 1);
}
 
// Driver function
int main()
{
 
    int n = 10;
    cout << " Sum of all elements:" << calculateSum(n);
    return 0;
}


Java
// Java program to find sum of all elements
// upto nth row in Pascal triangle.
import java.io.*;
 
class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Calculate 2^n
        sum = 1 << n;
 
        return (sum - 1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println("Sum of all elements:"
                           + calculateSum(n));
    }
}


Python3
# Python3 program to find sum of all elements
# upto nth row in Pascal triangle.
 
# Function to find sum of aal elements
# upto nth row.
def calculateSum(n) :
     
    # Initialize sum with 0
    sum = 0
     
    # Calculate 2 ^ n
    sum = 1 << n;
     
    return (sum - 1)
 
# Driver unicode
n = 10
print("Sum of all elements:", calculateSum(n))


C#
// C# program to find sum of all elements
// upto nth row in Pascal triangle.
using System;
 
public class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Calculate 2^n
        sum = 1 << n;
 
        return (sum - 1);
    }
 
    // Driver code
    static public void Main()
    {
        int n = 10;
        Console.WriteLine("Sum of all elements:"
                          + calculateSum(n));
    }
}


PHP


Javascript
// Javascript program to find sum
// of all elements upto nth
// row in Pascal triangle.
 
// Function to find
// sum of all elements
// upto nth row.
 
function calculateSum(n)
{
    // Initialize sum with 0
    sum = 0;
     
    // Calculate 2^n
    sum = 1 << n;
 
    return (sum - 1);
}
 
// Driver Code
let n = 10;
document.write(" Sum of all elements:" + calculateSum(n));
 
// This code is contributed _saurabh_jaiswal


输出:
Sum of all elements:1023

时间复杂度: O(n)
高效的解决方案:

因此,计算2 n而不是计算直到(n – 1)的每个2的幂,并且从上面的示例中,直到(n – 1)的2的幂的总和为(2 n – 1)。

C++

// C++ program to find sum of all elements
// upto nth row in Pascal triangle.
#include 
using namespace std;
 
// Function to find sum of aal elements
// upto nth row.
long long int calculateSum(int n)
{
 
    // Initialize sum with 0
    long long int sum = 0;
 
    // Calculate 2^n
    sum = 1 << n;
 
    return (sum - 1);
}
 
// Driver function
int main()
{
 
    int n = 10;
    cout << " Sum of all elements:" << calculateSum(n);
    return 0;
}

Java

// Java program to find sum of all elements
// upto nth row in Pascal triangle.
import java.io.*;
 
class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Calculate 2^n
        sum = 1 << n;
 
        return (sum - 1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println("Sum of all elements:"
                           + calculateSum(n));
    }
}

Python3

# Python3 program to find sum of all elements
# upto nth row in Pascal triangle.
 
# Function to find sum of aal elements
# upto nth row.
def calculateSum(n) :
     
    # Initialize sum with 0
    sum = 0
     
    # Calculate 2 ^ n
    sum = 1 << n;
     
    return (sum - 1)
 
# Driver unicode
n = 10
print("Sum of all elements:", calculateSum(n))

C#

// C# program to find sum of all elements
// upto nth row in Pascal triangle.
using System;
 
public class GFG {
 
    // Function to find sum of aal elements
    // upto nth row.
    static long calculateSum(int n)
    {
 
        // Initialize sum with 0
        long sum = 0;
 
        // Calculate 2^n
        sum = 1 << n;
 
        return (sum - 1);
    }
 
    // Driver code
    static public void Main()
    {
        int n = 10;
        Console.WriteLine("Sum of all elements:"
                          + calculateSum(n));
    }
}

的PHP


Java脚本

// Javascript program to find sum
// of all elements upto nth
// row in Pascal triangle.
 
// Function to find
// sum of all elements
// upto nth row.
 
function calculateSum(n)
{
    // Initialize sum with 0
    sum = 0;
     
    // Calculate 2^n
    sum = 1 << n;
 
    return (sum - 1);
}
 
// Driver Code
let n = 10;
document.write(" Sum of all elements:" + calculateSum(n));
 
// This code is contributed _saurabh_jaiswal
输出:
Sum of all elements:1023

时间复杂度: O(1)