{"id":22662,"date":"2021-09-27T08:36:54","date_gmt":"2021-09-27T03:06:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=22662"},"modified":"2021-11-22T18:35:38","modified_gmt":"2021-11-22T13:05:38","slug":"python-program-to-find-nth-pentagonal-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-nth-pentagonal-number\/","title":{"rendered":"Python Program to Find N\u2019th Pentagonal Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check if Three Points are Collinear<\/a> Nth Pentagonal Number:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the Nth Pentagonal Number in Python:<\/p>\n Nth Pentagonal Number Formula = 3*n*(n-1)\/2 + n\u00a0 \u00a0 \u00a0(or)\u00a0 (3*n*n – n)\/2<\/strong><\/p><\/blockquote>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n 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 …<\/p>\n
\nGiven a number N the task is to find the Nth Pentagonal Number in Python.<\/p>\nGiven Number = 4<\/pre>\n
The { 4 } pentagonal number = 22<\/pre>\n
Given Number = 6<\/pre>\n
The { 6 } pentagonal number = 51<\/pre>\n
Program to Find N\u2019th Pentagonal Number in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Create a function nthPentagonalNumb() which accepts the given number\r\n# as an argument and returns the nth pentagonal number.\r\n\r\n\r\ndef nthPentagonalNumb(numb):\r\n # Inside the nthPentagonalNumb()\u00a0function.\r\n # Calculate the value of (3*n*n - n)\/2 where n is the argument and\r\n # store it in a variable say reslt.\r\n reslt = (3*numb*numb - numb)\/2\r\n # Return the reslt.\r\n return int(reslt)\r\n\r\n\r\n# Give the number N as static Input and store it in a variable.\r\ngvnnumb = 4\r\n# Pass the given Number as an argument to nthPentagonalNumb()\u00a0\r\n# function and store the result returned from the function in a variable say nthNumb.\r\nnthNumb = nthPentagonalNumb(gvnnumb)\r\n# Print the nthNumb\u00a0value.\r\nprint('The {', gvnnumb, '} pentagonal number = ', nthNumb)\r\n<\/pre>\n
The { 4 } pentagonal number = 22<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Create a function nthPentagonalNumb() which accepts the given number\r\n# as an argument and returns the nth pentagonal number.\r\n\r\n\r\ndef nthPentagonalNumb(numb):\r\n # Inside the nthPentagonalNumb()\u00a0function.\r\n # Calculate the value of (3*n*n - n)\/2 where n is the argument and\r\n # store it in a variable say reslt.\r\n reslt = (3*numb*numb - numb)\/2\r\n # Return the reslt.\r\n return int(reslt)\r\n\r\n\r\n# Give the number N as user Input using the int(input()) function\r\n# and store it in a variable.\r\ngvnnumb = int(input('Enter some random Number = '))\r\n# Pass the given Number as an argument to nthPentagonalNumb()\u00a0\r\n# function and store the result returned from the function in a variable say nthNumb.\r\nnthNumb = nthPentagonalNumb(gvnnumb)\r\n# Print the nthNumb\u00a0value.\r\nprint('The {', gvnnumb, '} pentagonal number = ', nthNumb)\r\n<\/pre>\n
Enter some random Number = 6\r\nThe { 6 } pentagonal number = 51<\/pre>\n
\n