Sum of Series (n^2-1^2) + 2(n^2-2^2) +….n(n^2-n^2)
Last Updated :
23 Nov, 2022
Program for finding the sum of the nth term of the series (n^2-1^2) + 2(n^2-2^2) + 3(n^2-3^2) + ….n(n^2-n^2)
Examples:
Input : 2
Output :3
Input :5
Output :150
To solve this problem we have the formula ((1/4)*n2*(n2-1)). We can prove the formula using mathematical induction.
Example n = 2
result = ((1/4)*2^2*(2^2-1))
= ((0.25)*4*(4-1))
= ((0.25)*4*3
= 3 ans.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sum_series( int n)
{
int nSquare = n * n;
return nSquare * (nSquare - 1) / 4;
}
int main()
{
int n = 2;
cout << sum_series(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sum_series( int n)
{
int nSquare = n * n;
return nSquare * (nSquare - 1 ) / 4 ;
}
public static void main (String[] args)
{
int n = 2 ;
System.out.println( sum_series(n)) ;
}
}
|
Python3
def sum_series(n):
nSquare = n * n
return int (nSquare * (nSquare - 1 ) / 4 )
n = 2
print (sum_series(n))
|
C#
using System;
class GFG {
static int sum_series( int n)
{
int nSquare = n * n;
return nSquare * (nSquare - 1) / 4;
}
public static void Main ()
{
int n = 2;
Console.Write( sum_series(n)) ;
}
}
|
PHP
<?php
function sum_series( $n )
{
$nSquare = $n * $n ;
return $nSquare * ( $nSquare - 1) / 4;
}
$n = 2;
echo (sum_series( $n ));
?>
|
Javascript
<script>
function sum_series(n)
{
let nSquare = n * n;
return nSquare * (nSquare - 1) / 4;
}
let n = 2;
document.write( sum_series(n)) ;
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...