Program to Find N’th Pentagonal Number

Python Program to Find N’th Pentagonal Number

In the previous article, we have discussed Python Program to Check if Three Points are Collinear
Given a number N the task is to find the Nth Pentagonal Number in Python.

Nth Pentagonal Number:

n is the nth pentagonal number. When the pentagons are overlapped so that they share one vertex, Pn is the number of different dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots.

A polygonal number (triangular number, square number, and so on) is a number that is represented as dots or pebbles ordered in the shape of a regular polygon. The first few pentagonal numbers are as follows: 1, 5, 12, and so on.

Examples:

Example1:

Input:

Given Number = 4

Output:

The { 4 } pentagonal number =  22

Example2:

Input:

Given Number = 6

Output:

The { 6 } pentagonal number = 51

Program to Find N’th Pentagonal Number in Python

Below are the ways to find the Nth Pentagonal Number in Python:

Nth Pentagonal Number Formula = 3*n*(n-1)/2 + n     (or)  (3*n*n – n)/2

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the number N as static Input and store it in a variable.
  • Create a function nthPentagonalNumb() which accepts the given number as an argument and returns the nth pentagonal number.
  • Inside the nthPentagonalNumb() function.
  • Calculate the value of (3*n*n – n)/2 where n is the argument and store it in a variable say reslt.
  • Return the reslt.
  • Pass the given Number as an argument to nthPentagonalNumb() function and store the result returned from the function in a variable say nthNumb.
  • Print the nthNumb value.
  • The Exit of the Program.

Below is the implementation:

# Create a function nthPentagonalNumb() which accepts the given number
# as an argument and returns the nth pentagonal number.


def nthPentagonalNumb(numb):
        # Inside the nthPentagonalNumb() function.
        # Calculate the value of (3*n*n - n)/2 where n is the argument and
    # store it in a variable say reslt.
    reslt = (3*numb*numb - numb)/2
    # Return the reslt.
    return int(reslt)


# Give the number N as static Input and store it in a variable.
gvnnumb = 4
# Pass the given Number as an argument to nthPentagonalNumb() 
# function and store the result returned from the function in a variable say nthNumb.
nthNumb = nthPentagonalNumb(gvnnumb)
# Print the nthNumb value.
print('The {', gvnnumb, '} pentagonal number = ', nthNumb)

Output:

The { 4 } pentagonal number =  22

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the number N as user Input using the int(input()) function and store it in a variable.
  • Create a function nthPentagonalNumb() which accepts the given number as an argument and returns the nth pentagonal number.
  • Inside the nthPentagonalNumb() function.
  • Calculate the value of (3*n*n – n)/2 where n is the argument and store it in a variable say reslt.
  • Return the reslt.
  • Pass the given Number as an argument to nthPentagonalNumb() function and store the result returned from the function in a variable say nthNumb.
  • Print the nthNumb value.
  • The Exit of the Program.

Below is the implementation:

# Create a function nthPentagonalNumb() which accepts the given number
# as an argument and returns the nth pentagonal number.


def nthPentagonalNumb(numb):
        # Inside the nthPentagonalNumb() function.
        # Calculate the value of (3*n*n - n)/2 where n is the argument and
    # store it in a variable say reslt.
    reslt = (3*numb*numb - numb)/2
    # Return the reslt.
    return int(reslt)


# Give the number N as user Input using the int(input()) function
# and store it in a variable.
gvnnumb = int(input('Enter some random Number = '))
# Pass the given Number as an argument to nthPentagonalNumb() 
# function and store the result returned from the function in a variable say nthNumb.
nthNumb = nthPentagonalNumb(gvnnumb)
# Print the nthNumb value.
print('The {', gvnnumb, '} pentagonal number = ', nthNumb)

Output:

Enter some random Number = 6
The { 6 } pentagonal number = 51

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.