Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
I've got an infinite supply of \$n\$-dimensional chocolate for some positive integer \$n\$. The shape of the chocolate is not important. You may assume that they are just \$n\$-dimensional hypercubes with side length \$1\$.
To celebrate the upcoming Christmas, I want to assemble them into a giant chocolate pyramid with \$x\$ layers. The base of the pyramid is an \$(n-1)\$-dimensional hypercube with side length \$x\$, which contains \$x^{n-1}\$ chocolates. The next layer is an \$(n-1)\$-dimensional hypercube with side length \$x-1\$, which contains \$(x-1)^{n-1}\$ chocolates. And so on. The top layer is a single chocolate.
For example, when \$n=3\$, the pyramid would contain \$1^2 + 2^2 + \dots + x^2 = \frac{1}{6}x(x+1)(2x+1)\$ chocolates.
Interestingly, for any dimension \$n\$, the number of chocolates in the pyramid is always a polynomial in \$x\$.
- When \$n=1\$, this is \$\sum_{k=1}^x k^0 = x\$.
- When \$n=2\$, this is \$\sum_{k=1}^x k^1 = \frac{1}{2}x^2+\frac{1}{2}x\$.
- When \$n=3\$, this is \$\sum_{k=1}^x k^2 = \frac{1}{3}x^3+\frac{1}{2}x^2+\frac{1}{6}x\$.
- When \$n=4\$, this is \$\sum_{k=1}^x k^3 = \frac{1}{4}x^4+\frac{1}{2}x^3+\frac{1}{4}x^2\$.
- When \$n=5\$, this is \$\sum_{k=1}^x k^4 = \frac{1}{5}x^5+\frac{1}{2}x^4+\frac{1}{3}x^3-\frac{1}{30}x\$.
The general formula for these polynomials is called the Faulhaber's Formula.
Your task is to find the coefficients of these polynomials.
Rules
The usual sequence rules apply. So you may:
- Output all the polynomials.
- Take an input \$n\$ and output the \$n\$-th polynomial.
- ake an input \$n\$ and output the first \$n\$ polynomial.
You may use \$0\$-indexing or \$1\$-indexing.
You may output the polynomials in any reasonable format. Here are some example formats:
- a list of coefficients, in descending order, e.g. \$\frac{1}{5}x^5+\frac{1}{2}x^4+\frac{1}{3}x^3-\frac{1}{30}x\$ is represented as
[1/5,1/2,1/3,0,-1/30,0]
; - a list of coefficients, in ascending order, e.g. \$\frac{1}{5}x^5+\frac{1}{2}x^4+\frac{1}{3}x^3-\frac{1}{30}x\$ is represented as
[0,-1/30,0,1/3,1/2,1/5]
; - a function that takes an input \$k\$ and gives the coefficient of \$x^k\$;
- a built-in polynomial object.
Since the coefficients are not integers, you may output them as rational numbers, floating-point numbers, or any other reasonable format.
You may also take two integers \$n, k\$, and output the coefficient of \$x^k\$ in \$n\$-th polynomial. You may assume that \$k\le n\$.
This is code-golf, so the shortest code in bytes wins.
Testcases
Here I output lists of coefficients in descending order.
1 -> [1, 0]
2 -> [1/2, 1/2, 0]
3 -> [1/3, 1/2, 1/6, 0]
4 -> [1/4, 1/2, 1/4, 0, 0]
5 -> [1/5, 1/2, 1/3, 0, -1/30, 0]
6 -> [1/6, 1/2, 5/12, 0, -1/12, 0, 0]
7 -> [1/7, 1/2, 1/2, 0, -1/6, 0, 1/42, 0]
8 -> [1/8, 1/2, 7/12, 0, -7/24, 0, 1/12, 0, 0]
9 -> [1/9, 1/2, 2/3, 0, -7/15, 0, 2/9, 0, -1/30, 0]
10 -> [1/10, 1/2, 3/4, 0, -7/10, 0, 1/2, 0, -3/20, 0, 0]
11 -> [1/11, 1/2, 5/6, 0, -1, 0, 1, 0, -1/2, 0, 5/66, 0]
12 -> [1/12, 1/2, 11/12, 0, -11/8, 0, 11/6, 0, -11/8, 0, 5/12, 0, 0]
13 -> [1/13, 1/2, 1, 0, -11/6, 0, 22/7, 0, -33/10, 0, 5/3, 0, -691/2730, 0]
14 -> [1/14, 1/2, 13/12, 0, -143/60, 0, 143/28, 0, -143/20, 0, 65/12, 0, -691/420, 0, 0]
15 -> [1/15, 1/2, 7/6, 0, -91/30, 0, 143/18, 0, -143/10, 0, 91/6, 0, -691/90, 0, 7/6, 0]
16 -> [1/16, 1/2, 5/4, 0, -91/24, 0, 143/12, 0, -429/16, 0, 455/12, 0, -691/24, 0, 35/4, 0, 0]
n=16
, not sure if that's fine) \$\endgroup\$[1, 0]
is \$x\$.[1]
is \$1\$. They are different polynomials. So no. \$\endgroup\$