find the nth pentagonal number in typescript

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

Pn = n(3n - 1) / 2

Where Pn is the nth pentagonal number.

We can implement this in Typescript as follows:

index.ts
function getPentagonalNumber(n: number): number {
  return n * (3 * n - 1) / 2;
}

// Example usage
const nth = 6;
const pentagonalNumber = getPentagonalNumber(nth);
console.log(`The ${nth}th pentagonal number is ${pentagonalNumber}`);
236 chars
9 lines

In the example usage, we pass in the value 6 for the nth parameter, which will output:

index.ts
The 6th pentagonal number is 35
32 chars
2 lines

This means that the 6th pentagonal number is 35.

gistlibby LogSnag