find the nth hexagonal number in php

main.php
function nth_hexagonal_number($n) {
    return $n * (2 * $n - 1);
}

// Example usage
echo nth_hexagonal_number(5); // Output: 61
130 chars
7 lines

The function nth_hexagonal_number($n) takes an integer $n as input and returns the $n$th hexagonal number. The formula to calculate the $n$th hexagonal number is $n(2n-1)$.

In the example usage, nth_hexagonal_numbser(5) returns the 5th hexagonal number, which is 61.

related categories

gistlibby LogSnag