Find n-th term of series 3, 9, 21, 41, 71…

Given a mathematical series as 3, 9, 21, 41, 71… For a given integer n, you have to find the nth number of this series.

Examples :

Input : n = 4 
Output : 41

Input : n = 2
Output : 9

Our first task for solving this problem is to crack the series. If you will gave a closer look on the series, for a general n-th term the value will be (Σn2)+(Σn)+1, where

So, for calculating any n-th term of given series say f(n) we have:
f(n) = (Σn2)+(Σn)+1
= ( ((n*(n+1)*(2n+1))/6) + (n*(n+1)/2) + 1
= (n3+ 3n2 + 2n + 3 ) /3

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to calculate 
// nth term of a series
#include <bits/stdc++.h>
using namespace std;
  
// func for calualtion
int seriesFunc(int n)
{
    // for summation of square 
    // of first n-natural nos.
    int sumSquare = (n * (n + 1) 
                  * (2 * n + 1)) / 6;
  
    // summation of first n natural nos.
    int sumNatural = (n * (n + 1) / 2);
  
    // return result
    return (sumSquare + sumNatural + 1);
}
  
// Driver Code
int main()
{
    int n = 8;    
    cout << seriesFunc(n) << endl;
      
    n = 13;
    cout << seriesFunc(13);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to calculate 
// nth term of a series
import java.io.*;
  
class GFG 
{
    // func for calualtion
    static int seriesFunc(int n)
    {
        // for summation of square 
        // of first n-natural nos.
        int sumSquare = (n * (n + 1
                        * (2 * n + 1)) / 6;
      
        // summation of first n natural nos.
        int sumNatural = (n * (n + 1) / 2);
      
        // return result
        return (sumSquare + sumNatural + 1);
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int n = 8
        System.out.println(seriesFunc(n));
          
        n = 13;
        System.out.println(seriesFunc(13));
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to calculate 
# nth term of a series
  
# func for calualtion
def seriesFunc(n):
  
    # for summation of square 
    # of first n-natural nos.
    sumSquare = (n * (n + 1) * 
                (2 * n + 1)) / 6
  
    # summation of first n 
    # natural nos.
    sumNatural = (n * (n + 1) / 2)
  
    # return result
    return (sumSquare + sumNatural + 1)
  
# Driver Code
n = 8
print (int(seriesFunc(n)))
  
n = 13
print (int(seriesFunc(n)))
  
# This is code is contributed by Shreyanshi Arun. 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate 
// nth term of a series
using System;
  
class GFG 
{
    // Function for calualtion
    static int seriesFunc(int n)
    {
        // For summation of square 
        // of first n-natural nos.
        int sumSquare = (n * (n + 1) 
                        * (2 * n + 1)) / 6;
      
        // summation of first n natural nos.
        int sumNatural = (n * (n + 1) / 2);
      
        // return result
        return (sumSquare + sumNatural + 1);
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 8; 
        Console.WriteLine(seriesFunc(n));
          
        n = 13;
        Console.WriteLine(seriesFunc(13));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to calculate 
// nth term of a series
  
// func for calualtion
function seriesFunc($n)
{
    // for summation of square 
    // of first n-natural nos.
    $sumSquare = ($n * ($n + 1) 
                * (2 * $n + 1)) / 6;
  
    // summation of first n natural nos.
    $sumNatural = ($n * ($n + 1) / 2);
  
    // return result
    return ($sumSquare + $sumNatural + 1);
}
  
// Driver Code
$n = 8; 
echo(seriesFunc($n) . "\n");
      
$n = 13;
echo(seriesFunc($n) . "\n");
  
// This code is contributed by Ajit.
?>

chevron_right



Output :

241
911


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.