Sum of all the numbers in the Nth row of the given triangle
Given a positive integer N, the task is to find the sum of all the numbers in the Nth row of the below triangle.
1
3 2
6 2 3
10 2 3 4
15 2 3 4 5
…
…
…
Examples:
Input: N = 2
Output: 5
3 + 2 = 5
Input: N = 3
Output: 11
6 + 2 + 3 = 11
Approach: Taking a closer look at the pattern, it can be observed that a series will be formed as 1, 5, 11, 19, 29, 41, 55, … whose Nth term is (N – 1) + N2.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the sum // of the nth row elements of // the given triangle int getSum( int n) { return ((n - 1) + pow (n, 2)); } // Driver code int main() { int n = 3; cout << getSum(n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the sum // of the nth row elements of // the given triangle static int getSum( int n) { return ((n - 1 ) + ( int )Math.pow(n, 2 )); } // Driver code public static void main(String[] args) { int n = 3 ; System.out.println(getSum(n)); } } // This code is contributed by Code_Mech |
Python3
# Python3 implementation of the approach # Function to return the sum # of the nth row elements of # the given triangle def getSum(n) : return ((n - 1 ) + pow (n, 2 )); # Driver code if __name__ = = "__main__" : n = 3 ; print (getSum(n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum // of the nth row elements of // the given triangle static int getSum( int n) { return ((n - 1) + ( int )Math.Pow(n, 2)); } // Driver code public static void Main(String[] args) { int n = 3; Console.WriteLine(getSum(n)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript implementation of the approach // Function to return the sum // of the nth row elements of // the given triangle function getSum(n) { return ((n - 1) + Math.pow(n, 2)); } // Driver code var n = 3; document.write(getSum(n)); </script> |
11
Time complexity: O(1) because constant operations are done
Auxiliary Space: O(1)
Example-2 in python:
Approach steps:
1.Define a function triangle_row_sum that takes an integer n as input. The function will return the sum of all the numbers in the nth row of a given triangle.
2.Calculate the sum of the nth row of the triangle by using the formula row_sum = 2 * (3^(n-1)). This formula is derived from the fact that the kth row of the triangle has 2 * 3^(k-1) numbers, and each number in the kth row is 3^(k-1) times the corresponding number in the 1st row.
3.Return the value of row_sum as the sum of all the numbers in the nth row of the triangle.
4.In the example usage, create an integer n and call the triangle_row_sum function with this argument. Finally, print the sum of all the numbers in the nth row of the triangle.
C++
#include <iostream> #include <cmath> using namespace std; // function to calculate the sum of the nth row of the triangle int triangleRowSum( int n) { // calculate the sum of the nth row of the triangle int rowSum = 2 * pow (3, n - 1); // return the sum of the nth row return rowSum; } // example usage int main() { int n = 4; int rowSum = triangleRowSum(n); cout << "Sum of all the numbers in the " << n << "th row of the triangle is " << rowSum << endl; return 0; } |
Java
// Java program to find the sum of all the numbers in the // Nth row of a triangle public class TriangleRowSum { public static int triangleRowSum( int n) { // calculate the sum of the nth row of the triangle int rowSum = 2 * ( int )Math.pow( 3 , n - 1 ); // return the sum of the nth row return rowSum; } // example usage public static void main(String[] args) { int n = 4 ; int rowSum = triangleRowSum(n); System.out.println( "Sum of all the numbers in the " + n + "th row of the triangle is " + rowSum); } } |
Python3
# program to find the sum of all the numbers in the Nth row of a triangle def triangle_row_sum(n): # calculate the sum of the nth row of the triangle row_sum = 2 * ( 3 * * (n - 1 )) # return the sum of the nth row return row_sum # example usage n = 4 row_sum = triangle_row_sum(n) print ( "Sum of all the numbers in the" , n, "th row of the triangle is" , row_sum) |
C#
// C# program to find the sum of all the numbers in the // Nth row of a triangle using System; public class TriangleRowSum { public static int triangleRowSum( int n) { // calculate the sum of the nth row of the triangle int rowSum = 2 * ( int )Math.Pow(3, n - 1); // return the sum of the nth row return rowSum; } // example usage public static void Main( string [] args) { int n = 4; int rowSum = triangleRowSum(n); Console.WriteLine( "Sum of all the numbers in the " + n + "th row of the triangle is " + rowSum); } } |
Javascript
function triangle_row_sum(n) { // calculate the sum of the nth row of the triangle const row_sum = 2 * (3 ** (n-1)); // return the sum of the nth row return row_sum; } // example usage const n = 4; const row_sum = triangle_row_sum(n); console.log(`Sum of all the numbers in the ${n}th row of the triangle is ${row_sum}`); |
Sum of all the numbers in the 4 th row of the triangle is 54
Time complexity: O(1)
Auxiliary Space: O(1).