sub fibonacci {
$arg = $_[0];
if ($arg < 2) {
return $arg;
}
else {
return(fibonacci($arg - 1) + fibonacci($arg - 2));
}
}
$input = <STDIN>;
print fibonacci($input);
------------------------------------------------------------
My little recursion script runs without stop..!!
I don't know what is going wrong, who can kindly teach me?
But when I replace the fibonacci subroutine with:
sub fibonacci {$_ [0] < 2 ? $_ [0] : fibo ($_ [0] - 1) + fibo ($_ [0] - 2)}
it works just fine!!
What are the differences between them?!
Thanks!!
$arg = $_[0];
if ($arg < 2) {
return $arg;
}
else {
return(fibonacci($arg - 1) + fibonacci($arg - 2));
}
}
$input = <STDIN>;
print fibonacci($input);
------------------------------------------------------------
My little recursion script runs without stop..!!
I don't know what is going wrong, who can kindly teach me?
But when I replace the fibonacci subroutine with:
sub fibonacci {$_ [0] < 2 ? $_ [0] : fibo ($_ [0] - 1) + fibo ($_ [0] - 2)}
it works just fine!!
What are the differences between them?!
Thanks!!