Gp / Pari

时间:2018-08-19 02:20:11

标签: function pari

我正在尝试学习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)。任何有帮助的评论都将很棒!预先感谢!

1 个答案:

答案 0 :(得分: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