find the nth heptagonal number in javascript

To find the nth heptagonal number, you can use the following formula:

index.tsx
n(5n - 3) / 2
14 chars
2 lines

where n is the ordinal number of the heptagonal number you want to find.

Here's a function in JavaScript that takes an input n and returns the corresponding heptagonal number:

index.tsx
function heptagonalNumber(n) {
  return n * (5 * n - 3) / 2;
}
63 chars
4 lines

You can then call this function with any value of n to get the corresponding heptagonal number:

index.tsx
console.log(heptagonalNumber(1)); // 1
console.log(heptagonalNumber(2)); // 7
console.log(heptagonalNumber(3)); // 18
console.log(heptagonalNumber(4)); // 34
// and so on...
174 chars
6 lines

Note that the heptagonal numbers are a sequence of figurate numbers, with each polygonal number having seven sides.

gistlibby LogSnag