- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of Series (n^2-1^2) + 2(n^2-2^2) +β¦.n(n^2-n^2)
In this article, we will study different approaches to calculate the sum of the series- (n^2 - 1^2) + 2(n^2 - 2^2) + β¦. n(n^2 - n^2). In the first approach, we will calculate the series sum one by one for each i in the range 1 to n and keep adding it to the final sum.
In the second approach, we will derive a mathematical formula to calculate the sum of the given series which will result in the reduced time complexity of the program from O(n) to O(1).
Problem statement β We are given a number βn βand our task to calculate the sum of the given series (n^2 - 1^2) + 2(n^2 - 2^2) + β¦. n (n^2 - n^2).
Example
Input β Number = 5
Output β The sum of the series (n^2 - 1^2) + 2(n^2 - 2^2) + β¦. n(n^2 - n^2) for n = 5 is 150.
Input β Number = 3
Output β The sum of the series (n^2 - 1^2) + 2(n^2 - 2^2) + β¦.n (n^2 - n^2) for n = 3 is 18 .
Approach 1
This is the simplest that is brute force approach for the series sum problem.
After closely analysing the series, we may conclude that for any number n, we have
Sum = β i*(n^2 - i^2) for i = 1 to i = n.
So, for brute force approach, we can use the above formula in a loop for i=1 to n, In order to generate the required summation.
Example
The code of this approach is given below:
#include <bits/stdc++.h> using namespace std; int main () { int num = 3; long long sum=0; for (int i=1 ; i<num ; i++ ) { sum = sum+i*( num*num - i*i ); } cout<< " The sum of the series (n^2 - 1^2) + 2(n^2 - 2^2) + β¦. n(n^2 - n^2) for n = " << num << " is " <<sum; return 0; }
Output
The sum of the series (n^2 - 1^2) + 2(n^2 - 2^2) + β¦. n(n^2 - n^2) for n = 3 is 18
Complexities
Time complexity β O(n) as we are running a loop to iterate over the numbers from 1 to n.
Space complexity β As we are not using any external space, space complexity for this approach is O(1).
Approach 2
In this approach, we will deduce a formula to get the required series sum directly so there is no iteration needed, this approach will solve the given problem in constant time complexity.
As analysed earlier, we had the general version of the series given by
Sum = β i*(n^2 - i^2) for i = 1 to i = n.
The same series can be written as:
Sum = n^2β i - β i^3
We already know the formula to calculate the sum of all the numbers till n and sum of cubes of the number till n which is given by:
Sum of all numbers from 1 to n
n* ( n+1 )/2
where n is the given number.
Now, sum of all the cubes of numbers from 1 to n
(n*( n+1 )/2)^2
So the given series can be written as-
Sum = n^2 * ( n*( n+1 )/2 ) β ( n*( n+1 )/2 )^2
Sum can be further simplified as-
Sum = ( n * (n+1)/2 )*( n^2 - ( n * (n+1)/2 )) Sum = n^2 * ( n+1 )/2 * ( n^2 β (n * ( n+1))/2) Sum = n^2 * ( n+1 ) * ( n-1 )/4 Sum = n^2 * ( n^2 -1 )/4 Sum = (n^4)/4 β (n^2)/4
Hence we just need to calculate Sum = (n^4)/4 β (n^2)/4 for any n to get the desired sum of the series.
Example
The code for this approach is given below:
#include <bits/stdc++.h> using namespace std; int main () { int num = 5; long long sum = 0; sum = num*num*(num*num-1)/4; cout<< " The sum of the series (n^2-1^2) + 2(n^2-2^2) + β¦. n(n^2-n^2) for n = " << num << " is " <<sum; return 0; }
Output
The sum of the series (n^2-1^2) + 2(n^2-2^2) + β¦. n(n^2-n^2) for n = 5 is 150
Complexities
Time complexity β O(1) as we are just calculating the desired sum using the formula we derived.
Space complexity β As we are not using any external space, space complexity for this approach is O(1).
Conclusion β In this article, we have discussed two approaches to calculate the desired series sum and in the second approach, we reduced the time complexity to constant.