Wednesday, October 9, 2024
Google search engine
HomeData Modelling & AIFind sum of N terms of the series 3^3 – 2^3, 5^3...

Find sum of N terms of the series 3^3 – 2^3, 5^3 – 4^3, 7^3 – 6^3, …

Given a positive integer N, the task is to find the sum upto Nth term of the series:

 33 – 23, 53 – 43, 73 – 63, …., till N terms

Examples:

Input: N = 10
Output: 4960

Input: N = 1
Output: 19

 

Naive Approach

  • Initialize two int variables odd and even. Odd with value 3 and even with value 2.
  • Now Iterate the for loop n times each time will calculate the current term and add it to the sum.
  • In each iteration  increase odd and even value with 2.
  • Return the resultant sum

C++




// C++ program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
        // Calculate the current term
        // and add it to the sum
        Sum += (pow(Odd, 3)
                - pow(Even, 3));
 
        // Increase the odd and
        // even with value 2
        Odd += 2;
        Even += 2;
    }
    return Sum;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}


Java




// JAVA program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (Math.pow(Odd, 3) - Math.pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by Taranpreet


Python3




# Python 3 program for the above approach
 
# Function to calculate the sum
# of first N term
def findSum(N):
    # Initialize the variable
    Odd = 3
    Even = 2
    Sum = 0
 
    # Run a loop for N number of times
    for i in range(N):
 
        # Calculate the current term
        # and add it to the sum
        Sum += (pow(Odd, 3) - pow(Even, 3))
 
        # Increase the odd and
        # even with value 2
        Odd += 2
        Even += 2
         
    return Sum
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#




// C# program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static int findSum(int N)
  {
 
    // Initialize the variable
    int Odd = 3;
    int Even = 2;
    int Sum = 0;
 
    // Run a loop for N number of times
    for (int i = 0; i < N; i++) {
 
      // Calculate the current term
      // and add it to the sum
      Sum += (int)(Math. Pow(Odd, 3) - Math.Pow(Even, 3));
 
      // Increase the odd and
      // even with value 2
      Odd += 2;
      Even += 2;
    }
    return Sum;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
 
// Javascript program to find sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
// Function to return sum of
// N term of the series
function findSum(N)
{
 
    // Initialize the variable
    let Odd = 3;
    let Even = 2;
    let Sum = 0;
 
    // Run a loop for N number of times
    for (let i = 0; i < N; i++) {
 
        // Calculate the current term
        // and add it to the sum
        Sum += (Math.pow(Odd, 3)
            - Math.pow(Even, 3));
 
        // Increase the odd and
        // even with value 2
        Odd += 2;
        Even += 2;
    }
    return Sum;
}
 
// Driver Code
 
let N = 10;
document.write(findSum(N));
 
// This code is contributed by gfgking.
</script>


 
 

Output

4960

 

Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.

 

Efficient Approach:

 

The sequence is formed by using the following pattern.

 

For any value N the generalise form of the given sequence is- 
 

SN = 4*N3 + 9*N2 + 6*N

 

Below is the implementation of the above approach:

C++




// C++ program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
int findSum(int N)
{
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N;
}
 
// Driver Code
int main()
{
    int N = 10;
    cout << findSum(N);
}


Java




// Java program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
import java.util.*;
 
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return (int) (4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 10;
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python 3 program to find the sum of N terms of the
# series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
 
# Function to calculate the sum
# of first N term
def findSum(N):
    return 4 * pow(N, 3) + 9 * pow(N, 2) + 6 * N
 
 
# Driver Code
if __name__ == "__main__":
 
    # Value of N
    N = 10
     
    # Function call to calculate
    # sum of the series
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#




// C# program to find the sum of N terms of the
// series 3^3-2^3, 5^3 - 4^3, 7^3 - 6^3, ...
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  static int findSum(int N)
  {
    return 4 * (int)Math.Pow(N, 3)
      + 9 * (int)Math.Pow(N, 2) + 6 * N;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 10;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by ukasp.


Javascript




<script>
        // JavaScript code for the above approach
 
 
        // Function to return sum of
        // N term of the series
 
        function findSum(N) {
            return 4 * Math.pow(N, 3) + 9 * Math.pow(N, 2) + 6 * N;
        }
 
        // Driver Code
 
        let N = 10;
        document.write(findSum(N));
 
 
 
 
      // This code is contributed by Potta Lokesh
 
    </script>


Output

4960

Time Complexity: O(1)
Auxiliary Space: O(1)

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!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
16 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments