Sylvester的序列OEIS A000058是一个整数序列,定义如下:
每个成员是所有先前成员加一的乘积。序列的第一个成员是2。
任务
创建可能的最小程序,该程序需要n并计算Sylvester序列的第n个项。标准输入,输出和漏洞适用。由于结果增长很快,因此您不希望接受任何会导致选择语言溢出的术语。
测试用例
您可以使用零个或一个索引。(这里我使用零索引)
>>0
2
>>1
3
>>2
7
>>3
43
>>4
1807
n返回nth接受的序列号?
Sylvester的序列OEIS A000058是一个整数序列,定义如下:
每个成员是所有先前成员加一的乘积。序列的第一个成员是2。
创建可能的最小程序,该程序需要n并计算Sylvester序列的第n个项。标准输入,输出和漏洞适用。由于结果增长很快,因此您不希望接受任何会导致选择语言溢出的术语。
您可以使用零个或一个索引。(这里我使用零索引)
>>0
2
>>1
3
>>2
7
>>3
43
>>4
1807
n返回nth接受的序列号?
Answers:
<>(()())<>{({}[()])<>({({}[()])({})}{}())<>}<>
改用以下关系:

它是根据从序列中提供的关系修改后的这种关系得出的:
a(n+1) = a(n) * (a(n) - 1) + 1。
有关每个命令的功能的文档,请访问GitHub页面。
Brain-Flak中有两个堆栈,我将分别命名为堆栈1和堆栈2。
输入存储在堆栈1中。
<>(()())<> Store 2 in Stack 2.
{ while(Stack_1 != 0){
({}[()]) Stack_1 <- Stack_1 - 1;
<> Switch stack.
({({}[()])({})}{}()) Generate the next number in Stack 2.
<> Switch back to Stack 1.
}
<> Switch to Stack 2, implicitly print.
对于生成算法:
({({}[()])({})}{}()) Top <- (Top + Top + (Top-1) + (Top-1) + ... + 0) + 1
( ) Push the sum of the numbers evaluated in the process:
{ } while(Top != 0){
({}[()]) Pop Top, push Top-1 (added to sum)
({}) Pop again, push again (added to sum)
}
{} Top of stack is now zero, pop it.
() Evaluates to 1 (added to sum).
这仅使用一个堆栈。
({}<(()())>){({}<({({}[()])({})}{}())>[()])}{}
1{?)=}&~".>")!@(</=+={"/>}*
展开:
1 { ? )
= } & ~ "
. > " ) ! @
( < / = + = {
" / > } * .
. . . . .
. . . .
让我们考虑一下顺序b(a) = a(n) - 1并进行一些重新排列:
b(a) = a(n) - 1
= a(n-1)*(a(n-1)-1) + 1 - 1
= (b(n-1) + 1)*(b(n-1) + 1 - 1)
= (b(n-1) + 1)*b(n-1)
= b(n-1)^2 + b(n-1)
这个序列非常相似,但是我们可以将增量推迟到最后,这恰好在该程序中节省了一个字节。
因此,这是带注释的源代码:

用Timwi的HexagonyColorer创建。
这是一个内存图(红色三角形显示内存指针的初始位置和方向):

用Timwi的EsotericIDE创建。
代码从环绕左角的灰色路径开始,因此初始线性位如下:
1{?)(
1 Set edge b(1) to 1.
{ Move MP to edge N.
? Read input into edge N.
)( Increment, decrement (no-op).
然后,代码到达<分支的,并指示主循环的开始(和结束)。只要N边具有正值,就会执行绿色路径。该路径在网格周围缠绕了几次,但实际上是完全线性的:
""~&}=.*}=+={....(
的.是空操作,所以实际的代码是:
""~&}=*}=+={(
"" Move the MP to edge "copy".
~ Negate. This is to ensure that the value is negative so that &...
& ...copies the left-hand neighbour, i.e. b(i).
}= Move the MP to edge b(i)^2 and turn it around.
* Multiply the two copies of b(i) to compute b(i)^2.
}= Move the MP back to edge b(i) and turn it around.
+ Add the values in edges "copy" and b(i)^2 to compute
b(i) + b(i)^2 = b(i+1).
={ Turn the memory pointer around and move to edge N.
( Decrement.
一旦此减量减少N到0,就会执行红色路径:
")!@
" Move MP back to edge b(i) (which now holds b(N)).
) Increment to get a(N).
! Print as integer.
@ Terminate the program.
这个版本感谢randomra。稍后,我将尝试写详细的说明。
0&(]*:-<:)2:
这个版本感谢哩。使用大副词^:代替议程,如下所示。会有更多解释。
2(]*:-<:)^:[~]
2:`(1+*/@$:@i.)@.*
0索引。
e =: 2:`(1+*/@$:@i.)@.*
e 1
3
e 2
7
e 3
43
e 4
1807
x: e i. 10
2 3 7 43 1807 3263443 10650056950807 113423713055421862298779648 12864938683278674737956996400574416174101565840293888 1655066473245199944217466828172807675196959605278049661438916426914992848 91480678309535880456026315554816
|: ,: x: e i. 10
2
3
7
43
1807
3263443
10650056950807
113423713055421862298779648
12864938683278674737956996400574416174101565840293888
165506647324519994421746682817280767519695960527804966143891642691499284891480678309535880456026315554816
这是一个看起来像这样的议程:
┌─ 2:
│ ┌─ 1
┌───┤ ├─ +
│ └────┤ ┌─ / ─── *
── @. ─┤ │ ┌─ @ ─┴─ $:
│ └─ @ ─┴─ i.
└─ *
((9!:7)'┌┬┐├┼┤└┴┘│─'然后使用生成5!:4<'e')
分解:
┌─ ...
│
── @. ─┤
│
└─ *
使用顶部的分支作为gerund G,底部的作为选择器F,这是:
e n <=> ((F n) { G) n
这在2:时0 = * n,即在符号为零(因此n为零)时使用常数函数。否则,我们使用此fork:
┌─ 1
├─ +
──┤ ┌─ / ─── *
│ ┌─ @ ─┴─ $:
└─ @ ─┴─ i.
这是其中之一,再加上以下顶级系列:
┌─ / ─── *
┌─ @ ─┴─ $:
── @ ─┴─ i.
进一步分解,这是范围(*/)上自参考($:)的乘积(i.)。
2(]*:-<:)^:[~]公式a(0) = 2和将power副词获取14个字节a(n+1) = a(n)^2 - (a(n) - 1)。要计算更大的值,2开始时必须将标记为扩展整数。
v`$:@.u递归格式。我总是使用一种^:v通常更复杂的格式。@miles我也从未使用过这个(]v)技巧。我花了5分钟时间才理解它。
2(]*:-<:)~&0~]或2:0&(]*:-<:)~])。并将它们组合成13个字节 ]0&(]*:-<:)2:。
0&(]*:-<:)2:。(对不起,我不应该在评论中打高尔夫。)
{(2,{1+[*] @_}...*)[$_]}
{(2,{1+.²-$_}...*)[$_]}
# bare block with implicit parameter 「$_」
{
(
# You can replace 2 with 1 here
# so that it uses 1 based indexing
# rather than 0 based
2,
# bare block with implicit parameter 「@_」
{
1 +
# reduce the input of this inner block with 「&infix:<*>」
# ( the input is all of them generated when using a slurpy @ var )
[*] @_
# that is the same as:
# 「@_.reduce: &infix:<*>」
}
# keep calling that to generate more values until:
...
# forever
*
# get the value as indexed by the input
)[ $_ ]
}
my &code = {(2,{1+[*] @_}...*)[$_]}
say code 0; # 2
say code 1; # 3
say code 2; # 7
say code 3; # 43
say code 4; # 1807
# you can even give it a range
say code 4..7;
# (1807 3263443 10650056950807 113423713055421844361000443)
say code 8;
# 12864938683278671740537145998360961546653259485195807
say code 9;
# 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
say code 10;
# 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920807
my $start = now;
# how many digits are there in the 20th value
say chars code 20;
# 213441
my $finish = now;
# how long did it take to generate the values up to 20
say $finish - $start, ' seconds';
# 49.7069076 seconds
$_?的数组切片 这是什么巫术?
int f(int n){return--n<0?2:f(n)*~-f(n)+1;}
使用带有常规公式的0索引。不过我换n*n-n了n*(n-1),因为Java没有方便的运算符,而且f()通话时间越来越长。
f(n)*~-f(n)应该管用。
return--n<0多保存一个字节。
漏尼姑让我在这里打败
({}<(()())>){({}[()]<<>(())<>([]){{}<>({}<<>(({}<>))><>)<>({<({}[()])><>({})<>}{})<>{}([])}{}<>({}())([]){{}({}<>)<>([])}{}<>>)}{}([][()]){{}{}([][()])}{}
在输入a(0)下放置两个
({}<(()())>)
当输入大于零时,从输入中减去1并...
{
({}[()]
默默...
<
将一个放在另一个堆栈上以充当乘法<>(())<>
当堆栈为非空时
([])
{
{}
将列表顶部移至上方并复制
<>({}<<>(({}<>))><>)
将催化剂乘以副本
<>({<({}[()])><>({})<>}{})<>{}
([])
}
{}
加一
<>({}())
将序列移回正确的堆栈
([])
{
{}
({}<>)<>
([])
}
{}
<>
>)
}{}
除去底部的所有内容(即最后创建的数字)
([][()])
{
{}
{}
([][()])
}
{}
多亏了JDL,节省了2个字节
多亏了user5957401,节省了1个字节
f=function(n)ifelse(n,(a=f(n-1))^2-a+1,2)
n保证不为负,就可以将条件从降低n>0为n。
f(n-1)是6个字节。我认为您可以通过将字节分配给某些内容来节省字节。即ifelse(n,(a=f(n-1))^2-a+1,2)
可能是我打高尔夫球的最后一门语言!不竞争,因为该语言将挑战推迟了。
码:
²->2
感谢Zwei的替代解决方案:
<*>2
扩展版本:
a(n) = ²->
a(0) = 2
说明:
² # Stack is empty, so calculate a(n - 1) ** 2.
- # Subtract, arity 2, so use a(n - 1).
> # Increment by 1.
使用CP-1252编码。在线尝试!
b<*>2使用过a(n-1)*(a(n-1)+1)-1
b因为它将被自动填充(而不是输入):)。
readIO
def : lbl
set 128 2
set 129 3
j = i
if j z
print 2
GOTO e
:z
j - 1
if j Z
print 3
GOTO e
:Z
i - 1
:a
a = 127
b = 1
c = 1
:b
b * c
a + 1
c = get a
if c b
b + 1
set a b
i - 1
if i a
printInt b
:e
随时在线尝试!
1索引:
F(n){return--n?n=F(n),n*n-n+1:2;}
测试主体:
int main() {
printf("%d\n", F(1));
printf("%d\n", F(2));
printf("%d\n", F(3));
printf("%d\n", F(4));
printf("%d\n", F(5));
}
Nest[#^2-#+1&,2,#]&
或21个字节:
Array[#0,#,0,1+1##&]&
Array解决方案是不可思议的。太糟糕了,##0不是一回事。;)