# Nth Ngonal Numbers

Most of us are probably familiar with the concept of triangular and square numbers. However, there are also pentagonal numbers, hexagonal numbers, septagonal numbers, octagonal numbers, etc. The Nth Nagonal number is defined as the Nth number of the sequence formed with a polygon of N sides. Obviously, N >= 3, as there are no 2 or 1 sided closed shapes. The first few Nth Ngonal numbers are 0, 1, 2, 6, 16, 35, 66, 112, 176, 261, 370, 506, 672, 871.... This is sequence A060354 in the OEIS.

Write a program or function that, when given an integer n as input, outputs/returns the Nth Nagonal number.

## Input:

An integer N between 3 and 10^6.

## Output:

The Nth Nagonal number where N is the input.

## Test Case:

25 -> 6925
35 -> 19670
40 -> 29680


## Scoring:

This is , lowest score in bytes wins!

• Related – Digital Trauma Oct 11 '17 at 17:55
• It would not hurt to add a direct precise definition. – Wlod AA Oct 12 '17 at 19:11

# Neim, 1 byte

ℙ


¯\_(ツ)_/¯

Try it online!

• A built-in for this? Really? Obviously less obscure than I'd thought. – Gryphon Oct 11 '17 at 15:29
• @Gryphon It's existed for ages. – Okx Oct 11 '17 at 15:29
• @Gryphon Yeah. It's existed since May. – Okx Oct 11 '17 at 15:30
• @Gryphon At that point, there were a lot of challenges for polygonal numbers, so I just added a bunch to Neim :P – Okx Oct 11 '17 at 15:35
• This seems to be a built-in for calculating the ath b-gonal number, which gets both its parameters autofilled with the only argument given. – Lynn Oct 11 '17 at 18:08

# 05AB1E, 7 6 bytes

Saved 1 byte thanks to Neil

<ÐP+>;


Try it online!

Explanation

<        # push input-1
Ð       # triplicate
P      # product of stack
>    # increment
;   # divide by 2

• <Dn*+>; also works for 7 bytes. – Neil Oct 11 '17 at 15:57
• @Neil: Thanks! A modified version of that saved me a byte :) – Emigna Oct 11 '17 at 16:05
• Can't you do <3m instead of <ÐP? – Erik the Outgolfer Oct 11 '17 at 16:34
• @EriktheOutgolfer: Sure, but it's the same amount of bytes. – Emigna Oct 11 '17 at 17:24
• @Emigna Just asked to be sure. – Erik the Outgolfer Oct 11 '17 at 17:25

# Pyke, 6 bytes

t3^+he


Try it here!

t      - Decrement.
3^    - Raise to the power of 3.
h  - Increment.
e - Floor Halve.


# Japt, 9 8 bytes

´U+³ z Ä


Try it

• 1 byte saved thanks to ETH

## Explanation

Decrement (´) the input (U), add the input cubed (³) to that, floor divide by 2 (z) and add 1 (Ä).

• There are about a bazillion ways to rewrite this in 9 bytes, but it's tough to find an 8-byte solution... oh, here's one I think should work: ´U+³ z Ä – ETHproductions Oct 11 '17 at 16:35
• Nice one, @ETHproductions; got distracted by work while trying to crunch it down. – Shaggy Oct 11 '17 at 16:45
• @ETHproductions Nice one, that's tricky...another one would be ´U+³+2 z. – Erik the Outgolfer Oct 11 '17 at 16:46
• @EriktheOutgolfer: or ÄÄ instead of +2. – Shaggy Oct 11 '17 at 16:48
• 7 bytes: à3 *3+U or U+3*Uà3 (credit goes partly to Lynn)... Quite sure it can be rearranged to save more bytes. – Mr. Xcoder Oct 11 '17 at 18:42

# Jelly, 5 bytes

c3×3+


Try it online!

Computes choose(n, 3) × 3 + n.

This translates quite readily to 05AB1E:

# 05AB1E, 5 bytes

3c3*+


Try it online!

• Umm, why do you have a f=\ in a Jelly submission header? :P – Mr. Xcoder Oct 11 '17 at 18:41

# Python 2, 23 bytes

lambda n:(~-n)**3-~n>>1


Try it online!

# PowerShell, 34 28 bytes

param($n)$n*($n*$n-3*\$n+4)/2


Try it online!

Closed-form solution golfed from the OEIS page. Used FOIL for another 6 byte savings.

# MATL, 7 bytes

t3Xn3*+


Luis Mendo's suggestion, which is a little clearer.

    (implicit input)
t                         duplicate
3Xn                      n choose 3
3*                    multiply by 3
(implicit output)


Try it online!

t:3XNn+


Try it online!

Both solutions port Lynn's algorithm

(implicit input)
t                         duplicate
:                        range (1...n)
3XN                     push 3, compute all 3-combinations of the range
n                    number ( equal to 3*choose(n,3) )
(implicit output)

• Perhaps a little simpler: t3Xn3*+ – Luis Mendo Oct 11 '17 at 18:54
• huh, I swear I looked for Xn and didn't see it. I'm not convinced I know how to read documentation... – Giuseppe Oct 11 '17 at 19:12
• Documentation could be clearer :-) It assumes you more or less know the equivalent Matlab functions. In this case, if you search for combinations of nchoosek in Suever's server it gives both functions as results – Luis Mendo Oct 11 '17 at 19:27

# Recursiva, 11 bytes

*Ha+-Sa*3a4


Try it online!

## JavaScript (ES6), 38 bytes

f=(n,k=n)=>k<2|n<3?k:f(n-1,k)+f(3,k-1)


Recursion FTW (or maybe only for seventh...)

• Or you could port the closed form solution for 17 bytes... – Neil Oct 11 '17 at 18:29

# Mathematica, 14 bytes

shorter than built-in!!!

(#^2-3#+4)#/2&


Try it online!

and 3 bytes shorter with Martin Ender's help

# Cubix, 20 17 bytes

Saved 3 bytes porting Emigna's answer.

Iu(:^\:**p+u@O,2)


Try it online!

    I u
( :
^ \ : * * p + u
@ O , 2 ) . . .
. .
. .


Iu-2^\:*p*qu@O,2+p*:


Try it online!

Expands to the cube

    I u
- 2
^ \ : * p * q u
@ O , 2 + p * :
. .
. .


which implements the (n*(n-2)^2+n^2)/2 approach.

# Ohm v2, 3 bytes

DÆ¤


Try it online!

# Python 2, 25 24 bytes

• Saved one byte thanks to Neil; golfed >>1 to /2.
lambda n:n*(n*n-3*n+4)/2


Try it online!

• Can you not use /2 instead of >>1? – Neil Oct 11 '17 at 15:40
• @Neil True, true. – Jonathan Frech Oct 11 '17 at 15:40
• @Neil It was an artifact from back when there had been a sum that needed division. – Jonathan Frech Oct 11 '17 at 15:44

# Pyth, 7 bytes

+*3.cQ3


Try it here!

Uses Lynn's algorithm.

# dc, 13 bytes

dd2-2^*r2^+2/


A fairly straightforward implementation of the first formula listed on the OEIS page.

# Commands           # Stack Tracker (tm)
# Begin with input   # n
d                    # n n
d                    # n n n
2-                   # n-2 n n
2^                   # (n-2)^2 n n
*                    # n*(n-2)^2 n
r                    # n n*(n-2)^2
2^                   # n^2 n*(n-2)^2
+                    # n*(n-2)^2+n^2
2/                   # (n*(n-2)^2+n^2)/2 # matches first formula
# End with output on stack


# Japt, 7 bytes

à3 *3+U


Try it here!

First, it was a comment on Shaggy's answer, but they told me I should post it myself.

• Only noticing this now - +1. – Shaggy Jan 19 '18 at 17:08

# 05AB1E, 2 bytes

ÅU


Try it online!

¯\_(ツ)_/¯

# Mathematica, 20 bytes

#~PolygonalNumber~#&


# cQuents 0, 16 bytes

#|1:A(AA-3A+4)/2


Try it online!

# Jelly, 6 bytes

’*3+‘H


Try it online!

Uses Emigna's algorithm inspired by Neil.

• I think you'll find that the <Dn*+>; comment was mine... – Neil Oct 11 '17 at 18:23
• I'm using his modification, but I'll add you as well. – Erik the Outgolfer Oct 11 '17 at 18:24

# Java 8, 18 bytes

n->n*(n*n-3*n+4)/2


Try it here.

The approach used by most other answers is the shortest in Java. For funsies I also ported two other answers:

Port of Mr. Xcoder's Python 2 answer (29 bytes):

n->(int)Math.pow(n-1,3)-~n>>1


Try it here.

Port of Lynn's Jelly answer (with manual calculation of a choose b) (76 bytes):

n->c(n,3)*3+nint c(int t,int c){return t<c?0:c==t|c==0?1:c(--t,c-1)+c(t,c);}


Try it here.