您好我正在嘗試使用perl執行遞歸函數的斐波那契數列。這是代碼perl中的斐波那契數列執行到無限循環
#! /user/bin/perl
print "enter the number of elements for the series\n";
$value=<stdin>;
chomp($value);
print "\n\nThe value entered is $value\n\n";
for($i=0;$i<$value;$i++)
{
print "fib($i)=".&fib($i);
print "\n";
}
sub fib
{
[email protected]_[0];
print"In the subroutine rec is :".$rec."\n";
if($rec == '0')
{
$f='0';
}
if($rec == '1')
{
$f='1';
}
else
{
$f=fib($rec-1)+fib($rec-2);
}
return $f;
}
但是,這段代碼最終以無限循環結束。任何人都可以幫我解決我犯的錯誤。
對這個問題的回答指出你的代碼如何工作,但還有很多需要改進。如果您想將驗證碼批評,流行到[codereview.se。 – amon