Created
January 23, 2013 19:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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