Sum of all the numbers in the Nth row of the given triangle
Last Updated :
28 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int getSum( int n)
{
return ((n - 1) + pow (n, 2));
}
int main()
{
int n = 3;
cout << getSum(n);
return 0;
}
|
Java
class GFG
{
static int getSum( int n)
{
return ((n - 1 ) + ( int )Math.pow(n, 2 ));
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(getSum(n));
}
}
|
Python3
def getSum(n) :
return ((n - 1 ) + pow (n, 2 ));
if __name__ = = "__main__" :
n = 3 ;
print (getSum(n));
|
C#
using System;
class GFG
{
static int getSum( int n)
{
return ((n - 1) + ( int )Math.Pow(n, 2));
}
public static void Main(String[] args)
{
int n = 3;
Console.WriteLine(getSum(n));
}
}
|
Javascript
<script>
function getSum(n)
{
return ((n - 1) + Math.pow(n, 2));
}
var n = 3;
document.write(getSum(n));
</script>
|
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;
int triangleRowSum( int n) {
int rowSum = 2 * pow (3, n - 1);
return rowSum;
}
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
public class TriangleRowSum {
public static int triangleRowSum( int n)
{
int rowSum = 2 * ( int )Math.pow( 3 , n - 1 );
return rowSum;
}
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
def triangle_row_sum(n):
row_sum = 2 * ( 3 * * (n - 1 ))
return row_sum
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#
using System;
public class TriangleRowSum {
public static int triangleRowSum( int n)
{
int rowSum = 2 * ( int )Math.Pow(3, n - 1);
return rowSum;
}
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)
{
const row_sum = 2 * (3 ** (n-1));
return row_sum;
}
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}`);
|
Output
Sum of all the numbers in the 4 th row of the triangle is 54
Time complexity: O(1)
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...