find the nth heptagonal number in php

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

main.php
Hn = n(5n-3)/2
15 chars
2 lines

where Hn is the nth heptagonal number.

We can implement this formula in PHP as follows:

main.php
function heptagonal($n) {
  return $n * (5 * $n - 3) / 2;
}

// Example usage
echo heptagonal(5);  // outputs 95
113 chars
7 lines

In the above code, we define a function heptagonal that takes an integer argument n and returns the nth heptagonal number using the formula. We then demonstrate the usage of this function by calling it with the argument 5 and outputting the result, which should be 95.

gistlibby LogSnag