Thursday, January 9, 2025
Google search engine
HomeData Modelling & AIFind the sum of n terms of the series 1,8,27,64 ….

Find the sum of n terms of the series 1,8,27,64 ….

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++




// C++ program to find the sum of n terms
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the sum
int calculateSum(int n)
{
 
    // Return total sum
    return pow(n * (n + 1) / 2, 2);
}
 
// Driver code
int main()
{
 
    int n = 4;
    cout << calculateSum(n);
 
    return 0;
}


Java




// Java program to find the sum of n terms
import java.io.*;
 
class GFG {
 
 
// Function to calculate the sum
static int calculateSum(int n)
{
 
    // Return total sum
    return (int)Math.pow(n * (n + 1) / 2, 2);
}
 
// Driver code
 
 
    public static void main (String[] args) {
        int n = 4;
    System.out.println( calculateSum(n));
 
    }
}
// This code is contributed by inder_verma..


Python3




# Python3 program to find the
# sum of n terms
 
#Function to calculate the sum
def calculateSum(n):
     
    #return total sum
    return (n * (n + 1) / 2)**2
     
#Driver code
if __name__=='__main__':
    n = 4
    print(calculateSum(n))
 
#this code is contributed by Shashank_Sharma


C#




// C# program to find the sum of n terms
using System;
 
class GFG
{
 
// Function ot calculate the sum
static int calculateSum(int n)
{
 
    // Return total sum
    return (int)Math.Pow(n * (n + 1) / 2, 2);
}
 
// Driver code
public static void Main ()
{
    int n = 4;
    Console.WriteLine(calculateSum(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Javascript




<script>
 
// javascript program to find the sum of n terms
 
// Function to calculate the sum
function calculateSum(n)
{
 
    // Return total sum
    return parseInt(Math.pow(n * (n + 1) / 2, 2));
}
 
// Driver code
 var n = 4;
document.write( calculateSum(n));
 
 
// This code contributed by shikhasingrajput
 
</script>


PHP




<?php
// PHP program to find
// the sum of n terms
     
// Function to calculate the sum
function calculateSum($n)
{
    // Return total sum
    return pow($n * ($n + 1) / 2 , 2);
}
     
// Driver code
$n = 4;
echo calculateSum($n);
     
// This code is contributed
// by ANKITRAI1
?>


Output

100


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>
 
// Function to calculate the sum of the series 1^3 + 2^3 +
// 3^3 + ... + n^3
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += (i + 1) * (i + 1)
               * (i + 1); // Calculate the cube of each
                          // number and add to the sum
    }
    return sum;
}
 
int main()
{
    std::cout << sumOfSeries(4) << std::endl; // Output: 100
    std::cout << sumOfSeries(2) << std::endl; // Output: 9
    return 0;
}


Python3




def sum_of_series_1(n):
    sum = 0
    for i in range(n):
        sum += (i+1)**3
    return sum
 
# Example usage:
print(sum_of_series_1(4))  # Output: 100
print(sum_of_series_1(2))  # Output: 9


Output

100
9


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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments