find the nth pentagonal number in javascript

One way to find the nth pentagonal number is to use the formula:

Pn = n(3n-1)/2

Here's a JavaScript function that implements this formula to find the nth pentagonal number:

index.tsx
function nthPentagonalNumber(n) {
  return n * (3 * n - 1) / 2;
}
66 chars
4 lines

To use this function, simply pass in the value of n as an argument:

index.tsx
console.log(nthPentagonalNumber(5)); // Output: 35
console.log(nthPentagonalNumber(10)); // Output: 145
104 chars
3 lines

This will output the nth pentagonal number for the given value of n.

gistlibby LogSnag