Sum of Series (n^2-1^2) + 2(n^2-2^2) +….n(n^2-n^2)
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.
C++
// CPP Program to finding the// sum of the nth series#include <bits/stdc++.h>using namespace std;// function that calculate// the sum of the nth seriesint sum_series(int n){ int nSquare = n * n; // using formula of the nth term return nSquare * (nSquare - 1) / 4;}// driver functionint main(){ int n = 2; cout << sum_series(n) << endl; return 0;} |
Java
// javaProgram to finding the// sum of the nth seriesimport java.io.*;class GFG { // function that calculate // the sum of the nth series static int sum_series(int n) { int nSquare = n * n; // using formula of the nth term return nSquare * (nSquare - 1) / 4; } // Driver function public static void main (String[] args) { int n = 2; System.out.println( sum_series(n)) ; } }// This article is contributed by vt_m |
Python3
# Python 3 Program to finding# the sum of the nth series# function that calculate# the sum of the nth seriesdef sum_series(n): nSquare = n * n # Using formula of the # nth term return int(nSquare * (nSquare - 1) / 4)# Driver functionn = 2print(sum_series(n))# This code is contributed by Smitha Dinesh Semwal |
C#
// C# program to finding the// sum of the nth seriesusing System;class GFG { // Function that calculate // the sum of the nth series static int sum_series(int n) { int nSquare = n * n; // Using formula of the nth term return nSquare * (nSquare - 1) / 4; } // Driver Code public static void Main () { int n = 2; Console.Write( sum_series(n)) ; }}// This code is contributed by vt_m |
PHP
<?php// PHP Program to finding the// sum of the nth series// function that calculate// the sum of the nth seriesfunction sum_series($n){ $nSquare = $n * $n; // using formula of the nth term return $nSquare * ($nSquare - 1) / 4;}// Driver Code$n = 2;echo(sum_series($n));// This code is contributed by Ajit.?> |
Javascript
<script>// JavaScript Program to finding the// sum of the nth series // function that calculate // the sum of the nth series function sum_series(n) { let nSquare = n * n; // using formula of the nth term return nSquare * (nSquare - 1) / 4; } // Driver code let n = 2; document.write( sum_series(n)) ;</script> |
Output:
3
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


