190k views
5 votes
(Math: pentagonal numbers) A pentagonal number is defined as for and so on. So, the first few numbers are 1, 5, 12, 22, .... Write a function with the following header that returns a pentagonal number: def getPentagonalNumber(n): Write a test program that uses this function to display the first 100 pentagonal numbers with 10 numbers on each line

User Jnrg
by
4.6k points

1 Answer

2 votes

Answer:

from math import sqrt

def getPentagonalNumber(n):

return int((3 * n * n - n) / 2)

for p in range(1, 100, 10):

for x in range(p, p+10):

print(getPentagonalNumber(x), end=" ")

print()

Step-by-step explanation:

The nth pentagonal number can be found by
(3n^(2) -n )/(2)

Create a function called getPentagonalNumber that takes one parameter, n and calculates the nth pentagon number using the formula

Create a nested for loop that iterates 100 times. Call the getPentagonalNumber function inside the loop to calculate the first 100 pentagonal number with 10 numbers on each line

User MeghP
by
4.7k points