注意:这是一次回收客串271314‘S排列问题(S)的尝试
有一种有趣的模式,当你发现10基数字的词汇排序排列与上升的唯一数字之间的差异时,就会形成一个有趣的模式。例如,123有排列:
123 132 213 231 312 321当你发现它们之间的差异时,你就得到了序列
9 81 18 81 9它们都可以除以9(因为基数为10的数字之和),以及回文。
特别要注意的是,如果我们使用下一个数字,1234,我们会得到序列
9 81 18 81 9 702 9 171 27 72 18 693 18 72 27 171 9 702 9 81 18 81 9它扩展了前面的序列,同时在693周围保持回文。即使当您开始使用更多的10数字时,这种模式仍然有效,尽管序列的长度是n!-1表示的n数字。请注意,要使用高于0 to 9的数字,我们不会更改为不同的基,只需将数字乘以10^x,例如[1,12,11]_{10} = 1*10^2 + 12*10^1 + 11*10^0 = 231。
您的目标是实现这个序列,方法是将每个元素返回为9的倍数。例如,这个序列的前23个元素是:
1 9 2 9 1 78 1 19 3 8 2 77 2 8 3 19 1 78 1 9 2 9 1其他一些测试用例(0索引):
23 => 657
119 => 5336
719 => 41015
5039 => 286694
40319 => 1632373
362879 => 3978052
100 => 1
1000 => 4
10000 => 3
100000 => 3发布于 2018-11-11 13:27:29
发布于 2018-11-11 11:15:12
≔⟦N⊕θ⟧ηW⌈η≧÷L⊞OυEη﹪κ⊕Lυη≔⁰δF²«≔Eυλζ≔⟦⟧ηF⮌Eυ§κι«⊞η§ζκ≔Φζ⁻μκζ»≦⁻↨ηχδ»I÷δ⁹在网上试试!链接是详细的代码版本。解释:
≔⟦N⊕θ⟧η获取一个包含输入和多个输入的列表。
W⌈η重复,直到两个值都为零为止。
≧÷L⊞OυEη﹪κ⊕Lυη对这两个值执行阶乘基转换。这是我第一次在列表中使用≧!
≔⁰δ清除结果。
F²«循环每个阶乘基数。
≔Eυλζ列出从0到长度- 1的数字列表。
≔⟦⟧η将结果初始化为空列表。
F⮌Eυ§κι«循环执行阶乘基数的数字。
⊞η§ζκ将下一个置换数字添加到结果中。
≔Φζ⁻μκζ»从列表中删除该数字。
≦⁻↨ηχδ»将置换转换为基数10的数字,并从中减去结果。
I÷δ⁹将最终结果除以9,然后转换为string。
发布于 2018-11-12 03:19:45
n=>(F=_=>f>n?((G=(n,z=0,x=f,y=l,b=[...a])=>y?G(n%(x/=y),+b.splice(n/x,1)+z*10,x,y-1,b):z)(n--)-G(n))/9:F(a.push(++l),f*=l))(a=[l=f=1])1-索引。
@guest271314's的观点是正确的。直接置换计算更短..。
n=>( // Function -
F=_=> // Helper func to calculate length needed
f>n? // If f > n (meaning the length is enough) -
(
(
G=( // Helper func to calculate permutation value -
n,
z=0, // Initial values
x=f, // Made as copies because we need to alter
y=l, // these values and the function will be
b=[...a] // called twice
)=>
y? // If still elements remaining -
G(
n%(x/=y), // Get next element
+b.splice(n/x,1)+z*10, // And add to the temporary result
x,
y-1, // Reduce length
b // Remaining elements
)
:z // Otherwise return the permutation value
)(n--)-G(n) // Calculate G(n) - G(n - 1)
)/9 // ... the whole divided by 9
:F(
a.push(++l), // Otherwise l = l + 1, push l into the array
f*=l // ... and calculate l!
)
)(
a=[l=f=1] // Initial values
)n=>(x=l=t=0n,P=(a,b=[])=>n?""+a?a.map(z=>P(a.filter(y=>y-z),[...b,z])):(v=b.reduce((u,y)=>u=u*10n+y),x?--n?0:t=v-x:0,x=v):0)([...Array(n+1))].map(_=>++l))&&t/9n链接到为性能而制作的更长版本。为了使演示工作,Array(n+1)变成了Array(Math.min(n+1,15))。理论上达到无穷大(在实践中达到了叠加极限)。
我的意思是有太多的事情要解释。
n=>( // Function
x=l=t=0n, // Initialization
P=( // Function to determine the permutation -
a, // remaining items
b=[] // storage
)=>
n? // if we haven't reached the required permutation yet -
""+a? // if we haven't the last layer of loop -
a.map( // loop over the entries -
z=>
P( // recurse -
a.filter(y=>y-z), // pick out the selected number
[...b,z] // append to next
)
)
:( // if we are at the last layer -
v=b.reduce((u,y)=>u=u*10n+y), // calculate the value of the permutation
x? // if not the first number -
--n? // if not the last -
0 // do nothing
:t=v-x // else calculate difference
:0, // else do nothing
x=v // record ot anyway
)
:0 // else do nothing
)
(
[...Array(n+1)].map(_=>++l) // the list of numbers to permute
)&&t/9n // last difference divided by 9https://codegolf.stackexchange.com/questions/175693
复制相似问题