find the nth nonagonal number in php

main.php
function findNonagonal($n) {
    //Formula for finding the nth nonagonal number: n(7n-5)/2
    return ($n*(7*$n-5))/2;
}

//Example Usage
echo findNonagonal(5); //Output: 70
174 chars
8 lines

The above code defines a function findNonagonal that accepts an argument n as input and returns the nth nonagonal number using the formula n(7n-5)/2.

To find the nth nonagonal number, we simply need to substitute the value of n in the formula provided and return the result.

The example usage demonstrates how to call the function and we see that the 5th nonagonal number is 70.

gistlibby LogSnag