8
\$\begingroup\$

Given a non-negative integer, \$n\,ドル yield the \$(n^2)^\text{th } n\$-gonal number.

Further Detail:

The \$x\$-gonal numbers, or polygonal numbers, are also known as the two-dimensional figurate numbers.

Many people will be familiar with the triangular numbers, these are the \3ドル\$-gonal numbers:

$$F(3,n)=\sum_{i=1}^{n}(i)=\frac{n(n+1)}{2}$$

The triangular numbers are OEIS A000217:

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, ...

Probably even more will be familiar with the square numbers, these are the \4ドル\$-gonal numbers:

$$F(4,n)=\sum_{i=1}^{n}(2i-1)=n^{2}$$

The square numbers are OEIS A000290:

0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, ...

In general the \$k^\text{th }x\$-gonal number is the number of pebbles required to iteratively build up an \$x\$-sided polygon by adding pebbles to \$x-2\$ of the sides, here are a few formulae:

\begin{align} F(x,k)&=\sum_{i=1}^{k}\left(1+\sum_{j=2}^{i}(x-2)\right) \\ &=\frac{k^2(x-2)-k(x-4)}{2} \\ &=\frac{k(k-1)}{2}(x-2)+k\\ &=(x-3)\sum_{i=1}^{k-1}(i)+\sum_{i=1}^{k}(i)\\ &=(x-2)(k-1)+1+F(x,k-1) \end{align}

I'm sure there are plenty more.
Note that the last one is recursive but that \$F(x,0)=0\$.

The challenge

...is to golf code for \$G(n)=F(n,n^2)\$ for non-negative \$n\$.
i.e. Given a non-negative integer, \$n\,ドル yield the \$(n^2)^\text{th } n\$-gonal number.
This is not (currently) in the OEIS:

0, 1, 4, 45, 256, 925, 2556, 5929, 12160, 22761, 39700, 65461, 103104, 156325, 229516, 327825, 457216, 624529, 837540, 1105021, ...

Notes

You may yield a list of these numbers up to and including the required one if preferred.
For example, given an input of 5 you may yield either:

  • the integer 925, or
  • the list [0, 1, 4, 45, 256, 925]
  • ...but not [0, 1, 4, 45, 256] or [1, 4, 45, 256, 925]

Results may also be results of floating point calculation and may deviate as such, so long as infinite precision floating point arithmetic would yield correct results.


Win by creating the shortest code in bytes in a language. The overall winner will be the shortest across all languages, but please don't let golfing languages dissuade you from entering in your favourite language - the primary goals are to challenge yourself and have fun!

asked Aug 26, 2018 at 20:30
\$\endgroup\$
2
  • \$\begingroup\$ Results may also be results of floating point calculation and may deviate as such -- are possible integer overflows also acceptable? \$\endgroup\$ Commented Aug 27, 2018 at 0:07
  • \$\begingroup\$ @JonathanFrech indeed, that would certainly be a default (the floating point stuff probably is too, but thought it best to say since it's an integer based challenge) \$\endgroup\$ Commented Aug 27, 2018 at 0:17

20 Answers 20

6
\$\begingroup\$

Wolfram Language (Mathematica), 19 bytes

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

Try it online!

Just a golfy version of the second formula.

With PolygonalNumber built-in, 23 bytes

PolygonalNumber[#,#^2]&

Try it online!

As expected, the Mathematica built-in is longer than the golfed version.

answered Aug 26, 2018 at 20:57
\$\endgroup\$
5
\$\begingroup\$

Neim, 2 bytes

ᛦP

Try it online!

̄\_(ツ)_/ ̄ First Neim answer, right tool for the job. By the way, the advice I give on my About me page still holds for my own posts: Don't upvote trivial solutions, please.

answered Aug 26, 2018 at 21:07
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Finding the right language is a +1 from me :p \$\endgroup\$ Commented Aug 26, 2018 at 21:11
  • \$\begingroup\$ Upvoted because when I saw the question I immediately thought "oh, Neim can do this in 2 bytes". But someone beat me to it! \$\endgroup\$ Commented Aug 27, 2018 at 13:54
  • \$\begingroup\$ Upvoted because I am oppositional by nature. \$\endgroup\$ Commented Aug 27, 2018 at 14:53
3
\$\begingroup\$

Jelly, 8 bytes

×ばつ_2$‘S

Try it online!

In short, it generates the range \$[0,1,2,\dots,(n-1)^2]\,ドル multiplies every integer in that range with \$n-2\,ドル increments each of them (to avoid adding \$n^2\$ at the end, saving 1 byte), then sums the resulting list.

Jonathan Allan
115k8 gold badges68 silver badges293 bronze badges
answered Aug 26, 2018 at 23:57
\$\endgroup\$
2
  • \$\begingroup\$ Ah ha you found the 1-byte save I knew must have been there (had _2ײṖ$S+² , ²ṖS×’’$+² and many other 9 variants) \$\endgroup\$ Commented Aug 27, 2018 at 0:05
  • \$\begingroup\$ @JonathanAllan Thank you for the edits. \$\endgroup\$ Commented Aug 27, 2018 at 6:43
2
\$\begingroup\$

05AB1E, 4 bytes

nsÅU

Try it online!

answered Aug 26, 2018 at 21:01
\$\endgroup\$
2
  • \$\begingroup\$ I thought you'd been really clever for a second there. \$\endgroup\$ Commented Aug 26, 2018 at 21:07
  • \$\begingroup\$ @JonathanAllan ¯\_(ツ)_/¯... Right tool for the job. Actually, give me a second to try Neim :P \$\endgroup\$ Commented Aug 26, 2018 at 21:07
2
\$\begingroup\$

Pyth, (削除) 14 (削除ここまで) 9 bytes

smh*d-Q2*

Try it online!

answered Aug 26, 2018 at 23:57
\$\endgroup\$
2
  • 1
    \$\begingroup\$ shM*R-Q2* saves 5 whole bytes. \$\endgroup\$ Commented Aug 27, 2018 at 0:02
  • \$\begingroup\$ @Mr.Xcoder yeah, found a similar 9 byte solution right before you posted that... \$\endgroup\$ Commented Aug 27, 2018 at 0:04
2
\$\begingroup\$

Python 2, (削除) 31 (削除ここまで) 30 bytes

lambda k:(k*k*(k-2)-k+4)*k*k/2

Try it online!


Saved:

  • -1 byte, thanks to joH1
answered Aug 27, 2018 at 14:44
\$\endgroup\$
1
1
\$\begingroup\$

JavaScript (ES7), 26 bytes

n=>n**4*(n-2)-n*n*(n-4)>>1

Try it online!


Recursive (ES6), 33 bytes

A quick attempt from mobile. Not really optimized for \$(n,n^2)\$.

x=>(g=k=>k&&(x-2)*--k-~g(k))(x*x)

Try it online!

answered Aug 26, 2018 at 20:45
\$\endgroup\$
2
  • \$\begingroup\$ Golfing from mobile and still FGITW :o (helps having no weird code-page I guess) \$\endgroup\$ Commented Aug 26, 2018 at 20:49
  • \$\begingroup\$ It definitely helps. :) (Or I'd need another phone, I guess...) \$\endgroup\$ Commented Aug 26, 2018 at 20:51
1
\$\begingroup\$

Charcoal, 17 bytes

I⊘↨⟦1±2±1¦4¦0¦0⟧N

Try it online! Link is to verbose version of code. Explanation: Uses the second formula expanded into the polynomial \$ \frac{1}{2} ( x^5 - 2x^4 - x^3 + 4x^2 ) \$ which is calculated by treating it as an arbitrary base conversion before halving and casting to string for implicit output.

answered Aug 26, 2018 at 21:12
\$\endgroup\$
1
\$\begingroup\$

cQuents, 20 bytes

0ドル:$$+$$/2($$-1)($-2

Try it online!

Explanation

0ドル Zero indexing
 : Mode: sequence (output nth term)
 Each term equals:
 $$+ index * index +
 $$/2 index * index / 2 * 
 ($$-1)($-2 (index * index - 1) * (index - 2
 ) ) implicit
answered Aug 26, 2018 at 21:07
\$\endgroup\$
0
1
\$\begingroup\$

Physica, 25 bytes

->n:(n^4-n^2)/2*(n-2)+n^2

Try it online!


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

Try it online!


->n:n^5/2-n^4-n^3/2+2*n*n

Try it online! | Try all of them at once as a test suite!

answered Aug 26, 2018 at 21:35
\$\endgroup\$
1
\$\begingroup\$

Clean, 38 bytes

import StdEnv
$x=x^2*(x^3/2-x^2-x/2+2)

Try it online!

answered Aug 26, 2018 at 22:33
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 17 bytes

×ばつ*∘3-⊢

Try it online!

answered Aug 26, 2018 at 22:53
\$\endgroup\$
2
  • \$\begingroup\$ You may want to include 0 in your test-suite \$\endgroup\$ Commented Aug 27, 2018 at 0:07
  • \$\begingroup\$ 2+×⍨-⍨2-×⍨- \$\endgroup\$ Commented Aug 27, 2018 at 12:50
1
\$\begingroup\$

Jelly, (削除) 11 (削除ここまで) 10 bytes

2μ1⁄2_×ばつ+

Try it online!

Uses the formula \$F(x,k)=\frac{k(k-1)}{2}(x-2)+k\$ as given in the challenge.

answered Aug 26, 2018 at 22:09
\$\endgroup\$
1
  • \$\begingroup\$ I have less but don't think I have optimal yet. \$\endgroup\$ Commented Aug 26, 2018 at 22:44
1
\$\begingroup\$

MATL, 9 bytes

U:qG2-*Qs

Try it online!

This uses Mr. Xcoder's Jelly approach.

answered Aug 27, 2018 at 0:15
\$\endgroup\$
2
  • \$\begingroup\$ FYI the approach is just a small step from the 4th formula (with the two sums), or, indeed, the 3rd (replace the triangle-number-esque formula with a sum) :) \$\endgroup\$ Commented Aug 27, 2018 at 0:20
  • \$\begingroup\$ @JonathanAllan Yes, I had noticed about the third, but not about the fourth. Thanks! \$\endgroup\$ Commented Aug 27, 2018 at 0:40
1
\$\begingroup\$

K (oK), 23 bytes

{((x-2)*k%2%k-1)+k:x*x}

Try it online!

Prefix function; implementation of the second formula: \$\frac{k(k-1)}{2}(x-2)+k\$

How:

{((x-2)*k%2%k-1)+k:x*x} # Main function, argument x.
 k:x*x # def k = n2
 ((x-2)*k%2%k-1)+ # calculate k + (((k/2)/(k-1))*(x-2))
answered Aug 27, 2018 at 15:45
\$\endgroup\$
1
\$\begingroup\$

Haskell (31 bytes)

g k=(k*k*k*k*(k-2)-k*k*(k-4))/2
answered Aug 28, 2018 at 13:35
\$\endgroup\$
2
  • \$\begingroup\$ k**4 instead of k*k*k*k. \$\endgroup\$ Commented Aug 28, 2018 at 15:50
  • 2
    \$\begingroup\$ or k^4. But the equation may be rearranged a little: (k^4*(k-2)-k*k*(k-4))/2 = ((k*k*(k-2)-k+4)*k*k)/2 = ((k^3-2*k-k+4)*k*k)/2 - so this gives the 25 byte version: g k=(k^3-2*k*k-k+4)*k*k/2 Here is an online IDE link - Try It Online! \$\endgroup\$ Commented Aug 28, 2018 at 17:39
1
\$\begingroup\$

Haskell, 27 bytes

f k=(k^4*(k-2)-k^2*(k-4))/2

Try it online!

My first attempt at golfing in Haskell, basically same as almost any answer :

Nothing worth explaining here.

answered Aug 28, 2018 at 16:37
\$\endgroup\$
4
  • \$\begingroup\$ Nice. I gave a golfed version to Davide Spataro, since they posted earlier, (my first attempt at golfing Haskell too :)) \$\endgroup\$ Commented Aug 28, 2018 at 17:43
  • \$\begingroup\$ @JonathanAllan you did better than me, (your formula was better). Seeing there is already an existing Haskell answer, do you want me to delete mine ? \$\endgroup\$ Commented Aug 29, 2018 at 6:59
  • \$\begingroup\$ Some people delete if they find the same (or effectively the same) solution, but I think if one finds it independently there is nothing wrong with keeping it. \$\endgroup\$ Commented Aug 29, 2018 at 7:28
  • 1
    \$\begingroup\$ @JonathanAllan then for now, I'll let it stay :)\ \$\endgroup\$ Commented Aug 29, 2018 at 8:23
1
\$\begingroup\$

C (gcc), (削除) 60 (削除ここまで) (削除) 36 (削除ここまで) 34 bytes

-24 bytes thanks to Jonathan Allan

-2 bytes by factorizing again an n*n expression.

g(n){return(n*n*(n-2)-n+4)*n*n/2;}

Try it online!

Factored version of the second formula, uses a single function.

Entry point is function g(n), value is returned as integer.

answered Aug 30, 2018 at 9:32
\$\endgroup\$
2
  • 1
    \$\begingroup\$ g(n){return(n*n*n-2*n*n-n+4)*n*n/2;} is 36 bytes (is it golfable?) \$\endgroup\$ Commented Aug 30, 2018 at 9:40
  • \$\begingroup\$ @JonathanAllan thanks! I factorized as much as I could, I don't think it can't be golfed down again, at least not using this approach. \$\endgroup\$ Commented Aug 30, 2018 at 11:14
1
\$\begingroup\$

Ruby, 25 bytes

->n{(4-n+(n-2)*n*=n)*n/2}

Try it online!

answered Aug 30, 2018 at 11:31
\$\endgroup\$
0
\$\begingroup\$

Pyt, 4 bytes

Đ2⇹ᑭ

Try it online!

Explanation:

 Implicit input
Đ Duplicate
 2 Square
 ⇹ Swap top two elements on the stack
 ᑭ Get the (n^2)th n-gonal number
 Implicit output
answered Aug 27, 2018 at 15:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.