pascal triangle in php (anything wrong with this solution?) [migrated]
- by zhenka
I saw that one of the interview questions could be building a pascal triangle.
Is there anything wrong with this particular solution I came up with?
function pascal_r($r){
$local = array();
if($r == 1){
return array(array(1));
} else {
$previous = pascal_r($r - 1);
array_push($local, 1);
for($i = 0; $i < $r - 2 && $r > 2; $i++){
array_push($local, $previous[$r-2][$i] + $previous[$r-2][$i + 1]);
}
array_push($local, 1);
}
array_push($previous, $local);
return $previous;
}
print_r(pascal_r(100));