Nth Ngonal Numbers

8

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!

Gryphon

Posted 2017-10-11T15:21:41.243

Reputation: 6 697

Related – Digital Trauma – 2017-10-11T17:55:03.067

It would not hurt to add a direct precise definition. – Wlod AA – 2017-10-12T19:11:20.883

Answers

11

Neim, 1 byte

¯\_(ツ)_/¯

Try it online!

Okx

Posted 2017-10-11T15:21:41.243

Reputation: 15 025

1A built-in for this? Really? Obviously less obscure than I'd thought. – Gryphon – 2017-10-11T15:29:16.240

1@Gryphon It's existed for ages. – Okx – 2017-10-11T15:29:30.637

You mean the built-in? – Gryphon – 2017-10-11T15:29:56.540

2@Gryphon Yeah. It's existed since May. – Okx – 2017-10-11T15:30:45.020

Weird. I'd have thought there were a lot of more common sequences to give built-ins to first. – Gryphon – 2017-10-11T15:31:54.313

1@Gryphon At that point, there were a lot of challenges for polygonal numbers, so I just added a bunch to Neim :P – Okx – 2017-10-11T15:35:03.667

Got it. That was before I joined the site, so, ya know, before my time. – Gryphon – 2017-10-11T15:36:32.123

3This 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 – 2017-10-11T18:08:06.433

6

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

Emigna

Posted 2017-10-11T15:21:41.243

Reputation: 50 798

<Dn*+>; also works for 7 bytes. – Neil – 2017-10-11T15:57:22.500

@Neil: Thanks! A modified version of that saved me a byte :) – Emigna – 2017-10-11T16:05:23.327

Can't you do <3m instead of <ÐP? – Erik the Outgolfer – 2017-10-11T16:34:29.953

@EriktheOutgolfer: Sure, but it's the same amount of bytes. – Emigna – 2017-10-11T17:24:33.897

@Emigna Just asked to be sure. – Erik the Outgolfer – 2017-10-11T17:25:12.040

4

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

Shaggy

Posted 2017-10-11T15:21:41.243

Reputation: 24 623

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 – 2017-10-11T16:35:35.580

Nice one, @ETHproductions; got distracted by work while trying to crunch it down. – Shaggy – 2017-10-11T16:45:04.083

@ETHproductions Nice one, that's tricky...another one would be ´U+³+2 z. – Erik the Outgolfer – 2017-10-11T16:46:49.503

@EriktheOutgolfer: or ÄÄ instead of +2. – Shaggy – 2017-10-11T16:48:42.117

17 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 – 2017-10-11T18:42:51.873

Nice one, @Mr.Xcoder. You should post that yourself, it's different enough to mine. – Shaggy – 2017-10-11T22:26:36.180

4

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.

Mr. Xcoder

Posted 2017-10-11T15:21:41.243

Reputation: 39 774

4

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!

Lynn

Posted 2017-10-11T15:21:41.243

Reputation: 55 648

Umm, why do you have a f=\ in a Jelly submission header? :P – Mr. Xcoder – 2017-10-11T18:41:54.907

3

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.

AdmBorkBork

Posted 2017-10-11T15:21:41.243

Reputation: 41 581

3

Python 2, 23 bytes

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

Try it online!

Mr. Xcoder

Posted 2017-10-11T15:21:41.243

Reputation: 39 774

3

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)

Giuseppe

Posted 2017-10-11T15:21:41.243

Reputation: 21 077

Perhaps a little simpler: t3Xn3*+ – Luis Mendo – 2017-10-11T18:54:10.263

huh, I swear I looked for Xn and didn't see it. I'm not convinced I know how to read documentation... – Giuseppe – 2017-10-11T19:12:57.083

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 – 2017-10-11T19:27:59.137

2

Recursiva, 11 bytes

*Ha+-Sa*3a4

Try it online!

officialaimm

Posted 2017-10-11T15:21:41.243

Reputation: 2 739

2

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

ETHproductions

Posted 2017-10-11T15:21:41.243

Reputation: 47 880

Or you could port the closed form solution for 17 bytes... – Neil – 2017-10-11T18:29:20.777

2

Mathematica, 14 bytes

shorter than built-in!!!

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

Try it online!

and 3 bytes shorter with Martin Ender's help

J42161217

Posted 2017-10-11T15:21:41.243

Reputation: 15 931

2

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.

Giuseppe

Posted 2017-10-11T15:21:41.243

Reputation: 21 077

1

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!

Jonathan Frech

Posted 2017-10-11T15:21:41.243

Reputation: 6 681

Can you not use /2 instead of >>1? – Neil – 2017-10-11T15:40:15.393

@Neil True, true. – Jonathan Frech – 2017-10-11T15:40:35.747

@Neil It was an artifact from back when there had been a sum that needed division. – Jonathan Frech – 2017-10-11T15:44:49.667

1

Ohm v2, 3 bytes

DƤ

Try it online!

Cinaski

Posted 2017-10-11T15:21:41.243

Reputation: 1 588

1

Pyth, 7 bytes

+*3.cQ3

Try it here!

Uses Lynn's algorithm.

Mr. Xcoder

Posted 2017-10-11T15:21:41.243

Reputation: 39 774

1

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

Joe

Posted 2017-10-11T15:21:41.243

Reputation: 895

1

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.

Mr. Xcoder

Posted 2017-10-11T15:21:41.243

Reputation: 39 774

Only noticing this now - +1. – Shaggy – 2018-01-19T17:08:16.523

1

05AB1E, 2 bytes

ÅU

Try it online!

How?

¯\_(ツ)_/¯

Mr. Xcoder

Posted 2017-10-11T15:21:41.243

Reputation: 39 774

0

Mathematica, 20 bytes

#~PolygonalNumber~#&

Jonathan Frech

Posted 2017-10-11T15:21:41.243

Reputation: 6 681

0

Jelly, 6 bytes

’*3+‘H

Try it online!

Uses Emigna's algorithm inspired by Neil.

Erik the Outgolfer

Posted 2017-10-11T15:21:41.243

Reputation: 38 134

I think you'll find that the <Dn*+>; comment was mine... – Neil – 2017-10-11T18:23:30.107

I'm using his modification, but I'll add you as well. – Erik the Outgolfer – 2017-10-11T18:24:31.577

0

cQuents 0, 16 bytes

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

Try it online!

Stephen

Posted 2017-10-11T15:21:41.243

Reputation: 12 293

0

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.

Kevin Cruijssen

Posted 2017-10-11T15:21:41.243

Reputation: 67 575