find the nth nonagonal number in typescript

To find the nth nonagonal number in Typescript, we first need to understand what a nonagonal number is. A nonagonal number is a figurate number that represents a regular polygon with nine sides. The nth nonagonal number can be calculated using the following formula:

Nonagonal number = n * (7n - 5) / 2

Here is a Typescript function that uses this formula to find the nth nonagonal number:

index.ts
function getNonagonalNumber(n: number): number {
  return (n * (7 * n - 5)) / 2
}
82 chars
4 lines

We can then call this function with any value of n to get the corresponding nonagonal number. For example, to find the 5th nonagonal number, we can call the function like this:

index.ts
const fifthNonagonalNumber = getNonagonalNumber(5)
console.log(fifthNonagonalNumber) // Output: 145
100 chars
3 lines

This would output 145, which is the 5th nonagonal number.

gistlibby LogSnag