find the nth pentagonal number in php

To find the nth pentagonal number in PHP, you can use the formula:

P(n) = n(3n-1)/2

where P(n) is the nth pentagonal number.

Here is an example function that takes an integer n as input and returns the nth pentagonal number:

main.php
function nthPentagonalNumber($n) {
  return $n*(3*$n-1)/2;
}
61 chars
4 lines

You can then call this function with any value of n to get the corresponding pentagonal number:

main.php
echo nthPentagonalNumber(5); // Output: 35
echo nthPentagonalNumber(10); // Output: 145
88 chars
3 lines

Note that the input value of n must be a positive integer.

gistlibby LogSnag