Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, …
Given an integer N, the task is to find the sum of first N terms of the series:
(2 * 3 * 5), (3 * 5 * 7), (4 * 7 * 9), …
Examples:
Input: N = 3
Output: 387
S3 = (2 * 3 * 5) + (3 * 5 * 7) + (4 * 7 * 9) = 30 + 105 + 252 = 387
Input: N = 5
Output: 1740
Approach: Let the Nth term of the series be Tn. Sum of the series can be easily found by observing the Nth term of the series:
Tn = {nth term of 2, 3, 4, …} * {nth term of 3, 5, 7, …} * {nth term of 5, 7, 9, …}
Tn = (n + 1) * (2 * n + 1) * (2* n + 3)
Tn = 4n3 + 12n2 + 11n + 3
Sum(Sn) of first n terms can be found by
Sn = ?Tn
Sn = ?[4n3 + 12n2 + 11n + 3]
Sn = (n / 2) * [2n3 + 12n2 + 25n + 21]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calSum( int n)
{
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
int main()
{
int n = 3;
cout << calSum(n);
return 0;
}
|
Java
class GFG {
static int calSum( int n)
{
return (n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 )) / 2 ;
}
public static void main(String args[])
{
int n = 3 ;
System.out.println(calSum(n));
}
}
|
Python
def calSum(n):
return (n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 )) / 2 ;
n = 3
print (calSum(n))
|
C#
using System;
class GFG {
static int calSum( int n)
{
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
static public void Main()
{
int n = 3;
Console.WriteLine(calSum(n));
}
}
|
PHP
<?php
function calculateSum( $n )
{
return ( $n *(2* $n * $n * $n +12* $n * $n +25* $n +21))/2;
}
$n = 3;
echo calculateSum( $n );
?>
|
Javascript
<script>
function calSum( n) {
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
let n = 3;
document.write(calSum(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...