tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
33
Task/Catalan-numbers/PHP/catalan-numbers.php
Normal file
33
Task/Catalan-numbers/PHP/catalan-numbers.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
class CatalanNumbersSerie
|
||||
{
|
||||
private static $cache = array(0 => 1);
|
||||
|
||||
private function fill_cache($i)
|
||||
{
|
||||
$accum = 0;
|
||||
$n = $i-1;
|
||||
for($k = 0; $k <= $n; $k++)
|
||||
{
|
||||
$accum += $this->item($k)*$this->item($n-$k);
|
||||
}
|
||||
self::$cache[$i] = $accum;
|
||||
}
|
||||
function item($i)
|
||||
{
|
||||
if (!isset(self::$cache[$i]))
|
||||
{
|
||||
$this->fill_cache($i);
|
||||
}
|
||||
return self::$cache[$i];
|
||||
}
|
||||
}
|
||||
|
||||
$cn = new CatalanNumbersSerie();
|
||||
for($i = 0; $i <= 15;$i++)
|
||||
{
|
||||
$r = $cn->item($i);
|
||||
echo "$i = $r\r\n";
|
||||
}
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue