A PHP interview question, I feel that my writing is not very elegant. I would like to see how you write it.

A lamb can live to be 5 years old. It gives birth to a lamb at the age of 2 and 4, and dies at the age of 5.
Q: now there is a newborn lamb (how many sheep will there be after the age of 0), n?

Dec.23,2021

the first type:

function fn($x){
    $arr=[0=>0,1=>1,2=>2,3=>2,4=>4];
    if($x<=4)return $arr[$x];
    return fn($x-2)+fn($x-4);
}
echo fn(10);
Menu