Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk
Created January 24, 2013 09:22
Show Gist options
  • Save jbrooksuk/4619056 to your computer and use it in GitHub Desktop.
Save jbrooksuk/4619056 to your computer and use it in GitHub Desktop.
Pascals triangle in PHP.
<?php
function pascal_r($n) {
$aLines = [[1]];
for ($i=1; $i <= $n; $i++) {
$lastItem = $aLines[$i-1];
$aLines[$i] = [];
for ($j=0; $j <= $i; $j++) {
$aLines[$i][$j] = (isset($lastItem[$j - 1]) ? $lastItem[$j - 1] : 0) + (isset($lastItem[$j]) ? $lastItem[$j] : 0);
}
}
return $aLines[$n - 1];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment