Given a number n, find the n-th term in the series 3, 13, 42, 108, 235…**Examples:**

Input : 3 Output : 42 Input : 4 Output : 108

Constraints:

1 <= T <= 100

1 <= N <= 100**Naive Approach :**

The series basically represents sums of natural numbers cube and number of terms multiplied by 2. The first term is the sum of the single number. The second term is the sum of two numbers, and so on. **Examples:**

n = 2 2nd term equals to sum of 1st term and 8 i.e A2 = A1 + 2^{3}+ n*2 = 1 + 8 + 4 = 13 Similarly, A3 = A2 + 3^{3}+ n*2 = 9 + 27 + 6 = 42 and so on..

A simple solution is to add the first n natural numbers cube and number of terms multiplied by 2.

## C++

`// C++ program to find n-th term of` `// series 3, 13, 42, 108, 235…` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to generate a fixed number` `int` `magicOfSequence(` `int` `N)` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 1; i <= N; i++)` ` ` `sum += (i*i*i + i*2);` ` ` `return` `sum; ` `}` `// Driver Method` `int` `main()` `{` ` ` `int` `N = 4;` ` ` `cout << magicOfSequence(N) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java Program to Finding n-th term` `// of series 3, 13, 42, 108, 235 ...` `class` `GFG {` `// Function to generate` `// a fixed number` `public` `static` `int` `magicOfSequence(` `int` `N)` `{` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++)` ` ` `sum += (i * i * i + i * ` `2` `);` ` ` `return` `sum;` `}` `// Driver Method` `public` `static` `void` `main(String args[])` `{` ` ` `int` `N = ` `4` `;` ` ` `System.out.println(magicOfSequence(N));` `}` `}` `// This code is contributed by Jaideep Pyne` |

## Python3

`# Python3 program to` `# find n-th term of` `# series 3, 13, 42, 108, 235…` `# Function to generate` `# a fixed number` `def` `magicOfSequence(N) :` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `) :` ` ` `sum` `+` `=` `(i ` `*` `i ` `*` `i ` `+` `i ` `*` `2` `)` ` ` `return` `sum` `;` `# Driver Code` `N ` `=` `4` `print` `(magicOfSequence(N))` `# This code is contributed by vij.` |

## C#

`// C# Program to Finding` `// n-th term of series` `// 3, 13, 42, 108, 235 ...` `using` `System;` `class` `GFG` `{` ` ` `// Function to generate` `// a fixed number` `public` `static` `int` `magicOfSequence(` `int` `N)` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 1; i <= N; i++)` ` ` `sum += (i * i * i + i * 2);` ` ` `return` `sum;` `}` `// Driver Code` `static` `public` `void` `Main ()` `{` ` ` `int` `N = 4;` ` ` `Console.WriteLine(magicOfSequence(N));` `}` `}` `// This code is contributed` `// by ajit` |

## PHP

`<?php` `// PHP program to find n-th term of` `// series 3, 13, 42, 108, 235…` `// Function to generate a fixed number` `function` `magicOfSequence(` `$N` `)` `{` ` ` `$sum` `= 0;` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$N` `; ` `$i` `++)` ` ` `$sum` `+= (` `$i` `*` `$i` `*` `$i` `+ ` `$i` `*2);` ` ` `return` `$sum` `;` `}` `// Driver Method` ` ` `$N` `= 4;` ` ` `echo` `magicOfSequence(` `$N` `);` `// This code is contributed by m_kit ` `?>` |

## Javascript

`<script>` `// JavaScript program to find n-th term of` `// series 3, 13, 42, 108, 235…` `// Function to generate a fixed number` `function` `magicOfSequence( N)` `{` ` ` `let sum = 0;` ` ` `for` `(let i = 1; i <= N; i++)` ` ` `sum += (i*i*i + i*2);` ` ` `return` `sum; ` `}` `// Driver Function` ` ` `let N = 4;` ` ` `document.write(magicOfSequence(N));` ` ` `// This code contributed by Rajput-Ji` `</script>` |

**Output:**

120

Time Complexity of this solution is **O(n).****Efficient approach :**

We know sum of cubes of first n natural numbers is (n*(n+1)/2)^{2}. We also know that if we multiply i-th term by 2 and add all, we get sum of n terms as 2*n.

So our result is (n*(n+1)/2)^{2} + 2*n.**Example :**

For

n = 4sum by the formula is(4 * (4 + 1 ) / 2)) ^ 2 + 2*4

= (4 * 5 / 2) ^ 2 + 8

= (10) ^ 2 + 8

= 100 + 8= 108

Forn = 6, sum by the formula is(6 * (6 + 1 ) / 2)) ^ 2 + 2*6

= (6 * 7 / 2) ^ 2 + 12

= (21) ^ 2 + 12

= 441 + 12= 453

## C++

`// A formula based C++ program to find sum` `// of series with cubes of first n natural` `// numbers` `#include <iostream>` `using` `namespace` `std;` `int` `magicOfSequence(` `int` `N)` `{` ` ` `return` `(N * (N + 1) / 2) + 2 * N;` `}` `// Driver Function` `int` `main()` `{` ` ` `int` `N = 6;` ` ` `cout << magicOfSequence(N);` ` ` `return` `0;` `}` |

## Java

`// A formula based Java program to find sum` `// of series with cubes of first n natural` `// numbers` `class` `GFG {` ` ` ` ` `static` `int` `magicOfSequence(` `int` `N)` ` ` `{` ` ` `return` `(N * (N + ` `1` `) / ` `2` `) + ` `2` `* N;` ` ` `}` ` ` ` ` `// Driver Function` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `6` `;` ` ` `System.out.println(magicOfSequence(N));` ` ` `}` `}` `// This code is contributed by Smitha.` |

## Python 3

`# A formula based Python program to find sum` `# of series with cubes of first n natural` `# numbers` `def` `magicOfSequence(N):` ` ` `return` `(N ` `*` `(N ` `+` `1` `) ` `/` `2` `) ` `+` `2` `*` `N` `# Driver Function` `N ` `=` `6` `print` `(` `int` `(magicOfSequence(N)))` `# This code is contributed by Smitha.` |

## C#

`// A formula based C# program to find sum` `// of series with cubes of first n natural` `// numbers` `using` `System;` `class` `GFG {` ` ` ` ` `static` `int` `magicOfSequence(` `int` `N)` ` ` `{` ` ` `return` `(N * (N + 1) / 2) + 2 * N;` ` ` `}` ` ` ` ` `// Driver Function` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `N = 6;` ` ` `Console.Write(magicOfSequence(N));` ` ` `}` `}` `// This code is contributed by Smitha.` |

## PHP

`<?php` `// A formula based PHP program` `// to find sum of series with` `// cubes of first n natural numbers` `function` `magicOfSequence(` `$N` `)` `{` ` ` `return` `(` `$N` `* (` `$N` `+ 1) / 2) + 2 * ` `$N` `;` `}` `// Driver Code` `$N` `= 6;` `echo` `magicOfSequence(` `$N` `) . ` `"\n"` `;` `// This code is contributed by mits` `?>` |

## Javascript

`<script>` `// A formula based Java program to find sum` `// of series with cubes of first n natural` `// numbers` ` ` `function` `magicOfSequence( N)` ` ` `{` ` ` `return` `(N * (N + 1) / 2) + 2 * N;` ` ` `}` ` ` `// Driver Function ` ` ` `let N = 6;` ` ` `document.write(magicOfSequence(N));` `// This code is contributed by 29AjayKumar ` `</script>` |

**Output:**

33

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**