0

我正在努力学习 Gp Pari 编程语言,我正在解决项目 Euler 问题,但我似乎无法正确编译它:( 它应该计算所有尺寸较小的斐波那契数的列表比一些输入 n。

这是代码,

Euler_2(n) = 
(
x  = 0;
y = 0;
fib = listcreate(n);
listput(fib,1);
listput(fib,1);
a = True;
while(a, 
{if( x > n,
a = False;
);
x = fib[#fib] + fib[#fib-1];
listput(fib,x);
}); \\ end the while loop
)\\ end the function

我对这种语言完全陌生(我知道相当多的python)。任何有用的评论都会很棒!提前致谢!

4

1 回答 1

1

您需要用大括号而不是圆括号将代码括起来才能使用多行。(您也可以使用行尾反斜杠,正如 Shawn 在评论中建议的那样,但这很快就会过时。)快速代码审查:

Euler_2(n) = 
{
  \\ always declare lexical variables with my()
  my(x = 0, y = 0, fib = List([1, 1]), a = 1);
  while(1, \\ loop forever 
    x = fib[#fib] + fib[#fib-1];
    listput(fib,x);
    if(x > n, break);
  ); \\ end the while loop
  Vec(fib); \\ not sure what you wanted to return -- this returns the list, converted to a vector
} \\ end the function
于 2018-08-19T08:08:14.890 回答