Given an integer n, find the nth Pentagonal number. First three pentagonal numbers are 1, 5 and 12 (Please see below diagram).The n’th pentagonal number P_{n} is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots, when the pentagons are overlaid so that they share one vertex.
Pentagonal Number
Example 1:
Input:
n = 1 
Output:
1 
Explanation:
See the diagram given above.
Example 2:
Input:
n = 2 
Output:
5
Explanation:
See the diagram given above.
Example 3:
Input:
n = 3
Output:
12
Explanation:
See the diagram given above.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getNthPentagonalNum() which takes an Integer n as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= n <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:
    def getNthPentagonalNum(self, n):
        # code here

Solutions:
class Solution:

	def getNthPentagonalNum(self, n):
		return int(3 * n * (n - 1) / 2 + n)


class Solution:

	def getNthPentagonalNum(self, n):
		return (3 * n * n - n) // 2


class Solution:

	def getNthPentagonalNum(self, n):
		if n == 1:
			return 1
		elif n == 2:
			return 5
		return (3 * n * n - n) // 2


class Solution:

	def getNthPentagonalNum(self, n):
		return n * (n - 1) + n * (n + 1) // 2
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.getNthPentagonalNum(n))


class Solution:

	def getNthPentagonalNum(self, n):
		if n % 2 != 0:
			return n * (n + n // 2)
		g = n // 2
		mul = 3 * (g - 1) + 2
		return n // 2 + n * mul


class Solution:

	def getNthPentagonalNum(self, n):
		a = int((3 * n * n - n) / 2)
		return a


class Solution:

	def getNthPentagonalNum(self, n):
		s = (n - 1) * n + n * (n + 1) / 2
		return int(s)


class Solution:

	def getNthPentagonalNum(self, n):
		if n == 1:
			return 1
		else:
			return n * (3 * n - 1) // 2