Central polygonal numbers
Last Updated :
17 Mar, 2021
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
Below is the implementation of above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
void findNthTerm( int n)
{
cout << n * n - n + 1 << endl;
}
int main()
{
int N = 4;
findNthTerm(N);
return 0;
}
|
Java
class GFG{
static void findNthTerm( int n)
{
System.out.println(n * n - n + 1 );
}
public static void main(String[] args)
{
int N = 4 ;
findNthTerm(N);
}
}
|
Python3
def findNthTerm(n):
print (n * n - n + 1 )
N = 4
findNthTerm(N)
|
C#
using System;
class GFG{
static void findNthTerm( int n)
{
Console.Write(n * n - n + 1);
}
public static void Main()
{
int N = 4;
findNthTerm(N);
}
}
|
Javascript
<script>
function findNthTerm(n)
{
document.write(n * n - n + 1);
}
N = 4;
findNthTerm(N);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...