Gp/Pari 的用户定义函数错误
Error in user defined functions with Gp/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)。任何有用的评论都会很棒!提前致谢!
您需要用大括号(而不是圆括号)将代码括起来才能使用多行。 (您也可以使用行尾反斜杠,正如 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
我正在努力学习 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)。任何有用的评论都会很棒!提前致谢!
您需要用大括号(而不是圆括号)将代码括起来才能使用多行。 (您也可以使用行尾反斜杠,正如 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