find the nth heptagonal number in typescript

To find the nth heptagonal number, we can use the formula: Hn = n(5n-3)/2.

We can create a function in TypeScript that takes in a number n and returns the nth heptagonal number using this formula:

index.ts
function findNthHeptagonalNumber(n: number): number {
  return n * (5 * n - 3) / 2;
}
86 chars
4 lines

Here's an example usage of this function:

index.ts
const nthHeptagonalNumber = findNthHeptagonalNumber(6); // returns 135
console.log(nthHeptagonalNumber);
105 chars
3 lines

In this example, findNthHeptagonalNumber is called with the argument 6, which corresponds to the sixth heptagonal number. The function returns the value 135, which is the sixth heptagonal number.

gistlibby LogSnag