Open In App

Central polygonal numbers

Last Updated : 17 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number N, the task is to write a program to find the N-th term of the Central polygonal numbers series: 
 

1, 1, 3, 7, 13, 21, 31, 43, 57.....


Examples
 

Input: N = 0
Output: 1

Input: N = 3
Output: 7


 


Approach: The Nth term can be formalized as: 
 

Series = 1, 3, 7, 13, 21, 31, 43, 57...... 
Difference = 3-1, 7-3, 13-7, 21-13................ 
Difference = 2, 4, 6, 8......which is a AP
So nth term of given series 
= 1 + (2, 4, 6, 8 ...... (n-1)terms) 
= 1 + (n-1)/2*(2*2+(n-1-1)*2) 
= 1 + (n-1)/2*(4+2n-4) 
= 1 + (n-1)*n 
= n^2 - n + 1 
 


Therefore, the Nth term of the series is given as
 

n^2 - n + 1 


Below is the implementation of above approach: 
 

C++
// C++ program to find N-th term
// in the series
#include <iostream>
#include <math.h>
using namespace std;

// Function to find N-th term
// in the series
void findNthTerm(int n)
{
    cout << n * n - n + 1 << endl;
}

// Driver code
int main()
{
    int N = 4;
    findNthTerm(N);

    return 0;
}
Java
// Java program to find N-th term
// in the series
class GFG{

// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
    System.out.println(n * n - n + 1);
}

// Driver code
public static void main(String[] args)
{
    int N = 4;
    
    findNthTerm(N);
}
}

// This code is contributed by Ritik Bansal
Python3
# Python3 program to find N-th term 
# in the series 

# Function to find N-th term 
# in the series 
def findNthTerm(n):
    print(n * n - n + 1)

# Driver code
N = 4

# Function Call
findNthTerm(N)

# This code is contributed by Vishal Maurya
C#
// C# program to find N-th term
// in the series
using System;
class GFG{

// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
    Console.Write(n * n - n + 1);
}

// Driver code
public static void Main()
{
    int N = 4;
    
    findNthTerm(N);
}
}

// This code is contributed by nidhi_biet
JavaScript
<script>

// Javascript program to find N-th term
// in the series

// Function to find N-th term
// in the series
function findNthTerm(n)
{
    document.write(n * n - n + 1);
}

// Driver code
N = 4;
findNthTerm(N);


</script>

Output: 
13

 

Article Tags :
Practice Tags :

Similar Reads