find the nth decagonal number in php

To find the nth decagonal number, we can use the formula:

n(7n - 5) / 2

We can create a PHP function to calculate the nth decagonal number as follows:

main.php
function decagonal_number($n) {
  return $n * (7 * $n - 5) / 2;
}
66 chars
4 lines

Here, $n represents the nth decagonal number we want to find. The function returns the calculated decagonal number.

We can call this function with the input value of n as follows:

main.php
echo decagonal_number(6); // Outputs: 141
42 chars
2 lines

Here, we are finding the 6th decagonal number, which is 141.

gistlibby LogSnag