(n: number, exp: number)
| 47 | } |
| 48 | |
| 49 | // Return all the combinations of n non-negative integers that sum to exp. |
| 50 | function* powers(n: number, exp: number): Generator<number[]> { |
| 51 | if (n === 1) { |
| 52 | yield [exp]; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | for (let i = 0; i <= exp; i += 1) |
| 57 | for (const p of powers(n - 1, exp - i)) yield [i, ...p]; |
| 58 | } |
| 59 | |
| 60 | /** Use the multinomial theorem (https://en.wikipedia.org/wiki/Multinomial_theorem) to expand the expression. |