Find the sum of all the terms in the n-th row of the given series
Find the sum of all the terms in the nth row of the series given below.
1 2
3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18 19 20
..........................
............................
(so on)
Examples:
Input : n = 2
Output : 18
terms in 2nd row and their sum
sum = (3 + 4 + 5 + 6) = 18
Input : n = 4
Output : 132
Naive Approach: Using two loops. Outer loop executes for i = 1 to n times. Inner loop executes for j = 1 to 2 * i times. Counter variable k to keep track of the current term in the series. When i = n, the values of k are accumulated to the sum.
Time Complexity: O(k), where k is the total number of terms from the beginning till the end of the nth row.
Efficient Approach: The sum of all the terms in the nth row can be obtained by the formula:
Sum(n) = n * (2 * n2 + 1)
The proof for the formula is given below:
Prerequisite:
- Sum of n terms of an Arithmetic Progression series with a as the first term and d as the common difference is given as:
Sum = (n * [2*a + (n-1)*d]) / 2
-
- Sum of 1st n natural numbers is given as:
Sum = (n * (n + 1)) / 2
-
Proof:
Let the number of terms from the beginning
till the end of the nth row be p.
Here p = 2 + 4 + 6 + .....n terms
For the given AP series, a = 2, d = 2.
Using the above formula for the sum of
n terms of the AP series, we get,
p = n * (n + 1)
Similarly, let the number of terms from the
beginning till the end of the (n-1)th row be q.
Here q = 2 + 4 + 6 + .....n-1 terms
For the given AP series, a = 2, d = 2.
Using the above formula for the sum of
n-1 terms of the AP series, we get,
q = n * (n - 1)
Now,
Sum of all the terms in the nth row
= sum of 1st p natural numbers -
sum of 1st q natural numbers
= (p * (p + 1)) / 2 - (q * (q + 1)) / 2
Substituting the values of p and q and then solving
the equation, we will get,
Sum of all the terms in the nth row = n * (2 * n2 + 1)
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfTermsInNthRow( int n)
{
int sum = n * (2 * pow (n, 2) + 1);
return sum;
}
int main()
{
int n = 4;
cout << "Sum of all the terms in nth row = "
<< sumOfTermsInNthRow(n);
return 0;
}
|
Java
import static java.lang.Math.pow;
class Test {
static int sumOfTermsInNthRow( int n)
{
int sum = ( int )(n * ( 2 * pow(n, 2 ) + 1 ));
return sum;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println( "Sum of all the terms in nth row = "
+ sumOfTermsInNthRow(n));
}
}
|
Python3
from math import pow
def sumOfTermsInNthRow(n):
sum = n * ( 2 * pow (n, 2 ) + 1 )
return sum
if __name__ = = '__main__' :
n = 4
print ( "Sum of all the terms in nth row =" ,
int (sumOfTermsInNthRow(n)))
|
C#
using System;
class Test {
static int sumOfTermsInNthRow( int n)
{
int sum = ( int )(n * (2 * Math.Pow(n, 2) + 1));
return sum;
}
public static void Main()
{
int n = 4;
Console.Write( "Sum of all the terms in nth row = "
+ sumOfTermsInNthRow(n));
}
}
|
PHP
<?php
function sumOfTermsInNthRow( $n )
{
$sum = $n * (2 * pow( $n , 2) + 1);
return $sum ;
}
$n = 4;
echo "Sum of all the terms in nth row = " ,
sumOfTermsInNthRow( $n );
?>
|
Javascript
<script>
function sumOfTermsInNthRow( n)
{
let sum = n * (2 * Math.pow(n, 2) + 1);
return sum;
}
let n = 4;
document.write( "Sum of all the terms in nth row = "
+ sumOfTermsInNthRow(n));
</script>
|
Output:
Sum of all the terms in nth row = 132
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...