Find the sum of n terms of the series 1,8,27,64 ….
Last Updated :
07 Dec, 2023
Given a series, the task is to find the sum of the below series up to n terms:
1, 8, 27, 64, …
Examples:
Input: N = 2
Output: 9
9 = (2*(2+1)/2)^2
Input: N = 4
Output: 100
100 = (4*(4+1)/2)^2
Approach: We can solve this problem using the following formula:
Sn = 1 + 8 + 27 + 64 + .........up to n terms
Sn = (n*(n+1)/2)^2
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculateSum( int n)
{
return pow (n * (n + 1) / 2, 2);
}
int main()
{
int n = 4;
cout << calculateSum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int calculateSum( int n)
{
return ( int )Math.pow(n * (n + 1 ) / 2 , 2 );
}
public static void main (String[] args) {
int n = 4 ;
System.out.println( calculateSum(n));
}
}
|
Python3
def calculateSum(n):
return (n * (n + 1 ) / 2 ) * * 2
if __name__ = = '__main__' :
n = 4
print (calculateSum(n))
|
C#
using System;
class GFG
{
static int calculateSum( int n)
{
return ( int )Math.Pow(n * (n + 1) / 2, 2);
}
public static void Main ()
{
int n = 4;
Console.WriteLine(calculateSum(n));
}
}
|
Javascript
<script>
function calculateSum(n)
{
return parseInt(Math.pow(n * (n + 1) / 2, 2));
}
var n = 4;
document.write( calculateSum(n));
</script>
|
PHP
<?php
function calculateSum( $n )
{
return pow( $n * ( $n + 1) / 2 , 2);
}
$n = 4;
echo calculateSum( $n );
?>
|
Time complexity: O(logn) because using inbuilt function pow
Auxiliary Space: O(1)
Using Loop:
Approach:
- Define a function sum_of_series_1 that takes an integer n as input.
- Initialize a variable sum to 0.
- Use a for loop that iterates over the range of n values.
- Inside the loop, add the cube of (i+1) to the variable sum.
- Return the value of sum after the loop completes.
C++
#include <iostream>
int sumOfSeries( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += (i + 1) * (i + 1)
* (i + 1);
}
return sum;
}
int main()
{
std::cout << sumOfSeries(4) << std::endl;
std::cout << sumOfSeries(2) << std::endl;
return 0;
}
|
Java
public class SumOfSeries {
static int sumOfSeries( int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += Math.pow(
(i + 1 ), 3 );
}
return sum;
}
public static void main(String[] args)
{
System.out.println(sumOfSeries( 4 ));
System.out.println(sumOfSeries( 2 ));
}
}
|
Python3
def sum_of_series_1(n):
sum = 0
for i in range (n):
sum + = (i + 1 ) * * 3
return sum
print (sum_of_series_1( 4 ))
print (sum_of_series_1( 2 ))
|
C#
using System;
class Program {
static int SumOfSeries( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += ( int )Math.Pow(
i + 1, 3);
}
return sum;
}
static void Main()
{
Console.WriteLine(SumOfSeries(4));
Console.WriteLine(SumOfSeries(2));
}
}
|
Javascript
function sumOfSeries(n) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum += Math.pow(i + 1, 3);
}
return sum;
}
console.log(sumOfSeries(4));
console.log(sumOfSeries(2));
|
The time complexity of this approach is O(n) because we use a loop that iterates over n values.
The space complexity is O(1) because we use only one variable sum.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...