10
\$\begingroup\$

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.

Your Task:

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!

\$\endgroup\$
2
  • \$\begingroup\$ Related \$\endgroup\$ Oct 11, 2017 at 17:55
  • \$\begingroup\$ It would not hurt to add a direct precise definition. \$\endgroup\$
    – Wlod AA
    Oct 12, 2017 at 19:11

24 Answers 24

13
\$\begingroup\$

Neim, 1 byte

¯\_(ツ)_/¯

Try it online!

\$\endgroup\$
8
  • 1
    \$\begingroup\$ A built-in for this? Really? Obviously less obscure than I'd thought. \$\endgroup\$
    – Gryphon
    Oct 11, 2017 at 15:29
  • 1
    \$\begingroup\$ @Gryphon It's existed for ages. \$\endgroup\$
    – Okx
    Oct 11, 2017 at 15:29
  • 2
    \$\begingroup\$ @Gryphon Yeah. It's existed since May. \$\endgroup\$
    – Okx
    Oct 11, 2017 at 15:30
  • 1
    \$\begingroup\$ @Gryphon At that point, there were a lot of challenges for polygonal numbers, so I just added a bunch to Neim :P \$\endgroup\$
    – Okx
    Oct 11, 2017 at 15:35
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Lynn
    Oct 11, 2017 at 18:08
7
\$\begingroup\$

05AB1E, 7 6 bytes

Saved 1 byte thanks to Neil

<ÐP+>;

Try it online!

Explanation

<        # push input-1
 Ð       # triplicate
  P      # product of stack
   +     # add input
    >    # increment
     ;   # divide by 2
\$\endgroup\$
5
  • \$\begingroup\$ <Dn*+>; also works for 7 bytes. \$\endgroup\$
    – Neil
    Oct 11, 2017 at 15:57
  • \$\begingroup\$ @Neil: Thanks! A modified version of that saved me a byte :) \$\endgroup\$
    – Emigna
    Oct 11, 2017 at 16:05
  • \$\begingroup\$ Can't you do <3m instead of <ÐP? \$\endgroup\$ Oct 11, 2017 at 16:34
  • \$\begingroup\$ @EriktheOutgolfer: Sure, but it's the same amount of bytes. \$\endgroup\$
    – Emigna
    Oct 11, 2017 at 17:24
  • \$\begingroup\$ @Emigna Just asked to be sure. \$\endgroup\$ Oct 11, 2017 at 17:25
5
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Umm, why do you have a f=\ in a Jelly submission header? :P \$\endgroup\$
    – Mr. Xcoder
    Oct 11, 2017 at 18:41
4
\$\begingroup\$

Pyke, 6 bytes

t3^+he

Try it here!

t      - Decrement.
 3^    - Raise to the power of 3.
   +   - Add the input.
    h  - Increment.
     e - Floor Halve.
\$\endgroup\$
4
\$\begingroup\$

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 (Ä).

\$\endgroup\$
6
  • \$\begingroup\$ 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 Ä \$\endgroup\$ Oct 11, 2017 at 16:35
  • \$\begingroup\$ Nice one, @ETHproductions; got distracted by work while trying to crunch it down. \$\endgroup\$
    – Shaggy
    Oct 11, 2017 at 16:45
  • \$\begingroup\$ @ETHproductions Nice one, that's tricky...another one would be ´U+³+2 z. \$\endgroup\$ Oct 11, 2017 at 16:46
  • \$\begingroup\$ @EriktheOutgolfer: or ÄÄ instead of +2. \$\endgroup\$
    – Shaggy
    Oct 11, 2017 at 16:48
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Mr. Xcoder
    Oct 11, 2017 at 18:42
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 23 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
      +                   add
(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) )
      +                   add
(implicit output)
\$\endgroup\$
3
  • \$\begingroup\$ Perhaps a little simpler: t3Xn3*+ \$\endgroup\$
    – Luis Mendo
    Oct 11, 2017 at 18:54
  • \$\begingroup\$ huh, I swear I looked for Xn and didn't see it. I'm not convinced I know how to read documentation... \$\endgroup\$
    – Giuseppe
    Oct 11, 2017 at 19:12
  • \$\begingroup\$ 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 \$\endgroup\$
    – Luis Mendo
    Oct 11, 2017 at 19:27
2
\$\begingroup\$

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!

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

Recursiva, 11 bytes

*Ha+-Sa*3a4

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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...)

\$\endgroup\$
1
  • \$\begingroup\$ Or you could port the closed form solution for 17 bytes... \$\endgroup\$
    – Neil
    Oct 11, 2017 at 18:29
2
\$\begingroup\$

Mathematica, 14 bytes

shorter than built-in!!!

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

Try it online!

and 3 bytes shorter with Martin Ender's help

\$\endgroup\$
0
2
\$\begingroup\$

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 ) . . .
    . .
    . .

original answer:

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.

\$\endgroup\$
1
\$\begingroup\$

Ohm v2, 3 bytes

DƤ

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 20 bytes

#~PolygonalNumber~#&
\$\endgroup\$
1
\$\begingroup\$

Pyth, 7 bytes

+*3.cQ3

Try it here!

Uses Lynn's algorithm.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Only noticing this now - +1. \$\endgroup\$
    – Shaggy
    Jan 19, 2018 at 17:08
1
\$\begingroup\$

05AB1E, 2 bytes

ÅU

Try it online!

How?

¯\_(ツ)_/¯

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 23 bytes

{x*(((x*x)-(3*x))+4)%2}

Try it online!

Having to add parenthesis because K parses from right-to-left.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "Having to add parenthesis because K parses from right-to-left." - you don't have to add all of them \$\endgroup\$
    – ngn
    Jul 21 at 16:41
  • 1
    \$\begingroup\$ for instance the ( ) around 3*x are unnecessary, and you can avoid more pairs by rewriting things in equivalent ways \$\endgroup\$
    – ngn
    Jul 21 at 17:17
1
\$\begingroup\$

HOPS, 17 bytes

{n*(n*n-3*n+4)/2}

Attempt This Online!

Using the generating function given on OEIS by R. J. Mathar:

HOPS, 23 bytes

x*(1-2*x+4*x^2)/(1-x)^4

Attempt This Online!

Or using the exponential generating function given on OEIS by Paul Barry:

HOPS, 24 bytes

(exp(x)*(x+x^3/2)).*{n!}

Attempt This Online!

-3 bytes thanks to alephalpha

\$\endgroup\$
1
  • \$\begingroup\$ -3 bytes for the last one: (exp(x)*(x+x^3/2)).*{n!}. \$\endgroup\$
    – alephalpha
    Jul 21 at 7:01
0
\$\begingroup\$

cQuents 0, 16 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 6 bytes

’*3+‘H

Try it online!

Uses Emigna's algorithm inspired by Neil.

\$\endgroup\$
2
  • \$\begingroup\$ I think you'll find that the <Dn*+>; comment was mine... \$\endgroup\$
    – Neil
    Oct 11, 2017 at 18:23
  • \$\begingroup\$ I'm using his modification, but I'll add you as well. \$\endgroup\$ Oct 11, 2017 at 18:24
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.