Note: This is an attempt at recycling guest271314's permutation question(s)
There's an interesting pattern that forms when you find the differences between lexographically sorted permutations of base 10 numbers with ascending unique digits. For example, 123
has permutations:
123 132 213 231 312 321
When you find the differences between these, you get the sequence
9 81 18 81 9
Which are all divisible by nine (because of the digit sum of base 10 numbers), as well as being palindromic.
Notably, if we use the next number up, 1234
, we get the sequence
9 81 18 81 9 702 9 171 27 72 18 693 18 72 27 171 9 702 9 81 18 81 9
Which extends the previous sequence while remaining palindromic around \$693\$. This pattern always holds, even when you start using more that 10
numbers, though the length of the sequence is \$n!-1\$ for \$n\$ numbers. Note that to use numbers above 0 to 9
, we don't change to a different base, we just multiply the number by \$10^x\$, e.g. \$ [1,12,11]_{10} = 1*10^2 + 12*10^1 + 11*10^0 = 231\$.
Your goal is to implement this sequence, by returning each element as a multiple of nine. For example, the first 23 elements of this sequence are:
1 9 2 9 1 78 1 19 3 8 2 77 2 8 3 19 1 78 1 9 2 9 1
Some other test cases (0 indexed):
23 => 657
119 => 5336
719 => 41015
5039 => 286694
40319 => 1632373
362879 => 3978052
100 => 1
1000 => 4
10000 => 3
100000 => 3
Rules:
- The submission can be any of:
- A program/function that takes a number and returns the number at that index, either 0 or 1 indexed.
- A program/function that takes a number \$n\$ and returns up to the \$n\$th index, either 0 or 1 indexed.
- A program/function that outputs/returns the sequence infinitely.
- The program should be capable of theoretically handling up to the \$11!-1\$th element and beyond, though I understand if time/memory constraints make this fail. In particular, this means you cannot concatenate the digits and evaluate as base 10, since something like \$012345678910\$ would be wrong.
- This is code-golf, so the shortest implementation for each language wins!
Notes:
- This is OEIS A217626
- I am offering a bounty of 500 for a solution that works out the elements directly without calculating the actual permutations.
- The sequence works for any contiguous digits. For example, the differences between the permutations of \$ [1,2,3,4]_{10} \$ are the same as for \$[-4,-3,-2,-1]_{10}\$.
3628799 => -83676269
) which I think is the first negative term. \$\endgroup\$