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

• Last Updated : 12 Jul, 2022

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

 ``

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

 ``

Output

`4960`

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

My Personal Notes arrow_drop_up