Ahlahn

05-29-2011, 04:10 PM

Hey guys,

I'm trying to write a recursive code that prints EACH value in the sequence leading up to the specified place. For example, if I enter 3, I will get

0 1 1

Here's what I have so far. It prints the nth number in the sequence- I don't know where to go from there.

function fibRec($n){

if($n==1){

return 0;

}

if($n==2){

return 1;

}

else{

$sum = fibRec($n-1)+fibRec($n-2);

return $sum;

}

}

$fib = fibRec(29);

echo $fib;

I'm trying to write a recursive code that prints EACH value in the sequence leading up to the specified place. For example, if I enter 3, I will get

0 1 1

Here's what I have so far. It prints the nth number in the sequence- I don't know where to go from there.

function fibRec($n){

if($n==1){

return 0;

}

if($n==2){

return 1;

}

else{

$sum = fibRec($n-1)+fibRec($n-2);

return $sum;

}

}

$fib = fibRec(29);

echo $fib;