Find the sum of N terms of the series 1^2, (1^2+2^2), (1^2+2^2+3^2), …..
Last Updated :
20 Aug, 2022
Given a positive integer, N. Find the sum of the first N term of the series-
12, (12+22), (12+22+32),….,till N terms
Examples:
Input: N = 3
Output: 20
Input: N = 1
Output: 1
Approach: The sequence is formed by using the following pattern. For any value N-
Given 1^2, (1^2+2^2), (1^2+2^2+3^2),….,till N terms
SN = N * (N+1)2 * (N+2) / 12
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N){
return N*(N+1)*(N+1)*(N+2)/12;
}
int main()
{
int N = 3;
cout << findSum(N);
}
|
Java
class GFG {
static int findSum( int N) {
return N * (N + 1 ) * (N + 1 ) * (N + 2 ) / 12 ;
}
public static void main(String args[]) {
int N = 3 ;
System.out.print(findSum(N));
}
}
|
Python3
def findSum(N):
return N * (N + 1 ) * (N + 1 ) * (N + 2 ) / / 12
if __name__ = = "__main__" :
N = 3
print (findSum(N))
|
C#
using System;
class GFG
{
static int findSum( int N){
return N*(N+1)*(N+1)*(N+2)/12;
}
public static void Main()
{
int N = 3;
Console.Write(findSum(N));
}
}
|
Javascript
<script>
function findSum(N){
return N*(N+1)*(N+1)*(N+2)/12;
}
let N = 3;
document.write(findSum(N));
</script>
|
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...