Skip to content

Instantly share code, notes, and snippets.

@iamjochem
Created January 23, 2013 19:24
Show Gist options
  • Save iamjochem/4611869 to your computer and use it in GitHub Desktop.
Save iamjochem/4611869 to your computer and use it in GitHub Desktop.
PHP implementation for the answer to the question posed here: http://news.ycombinator.com/item?id=5103163
<?php
function pascal($depth)
{
if (!is_int($depth) || $depth < 1)
return array();
if ($depth === 1)
return array(1);
$layer = array(1, 1);
while (($len = count($layer)) < $depth) {
$tmp = array(1);
for ($i = 1; $i < $len; $i += 1) {
$tmp[] = $layer[$i - 1] + $layer[$i];
}
$tmp[] = 1;
$layer = $tmp;
}
return $layer;
}
function display_pascal($depth)
{
echo 'level ', $depth, ' = ', join(',', pascal($depth)), "\n";
}
foreach (range(0, 10) as $depth)
display_pascal($depth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment