Skip to content

Instantly share code, notes, and snippets.

@greydnls
Created December 14, 2014 00:59
Show Gist options
  • Save greydnls/4c1d055fce30e734232b to your computer and use it in GitHub Desktop.
Save greydnls/4c1d055fce30e734232b to your computer and use it in GitHub Desktop.
TofZ
<?php
/**
* Created by PhpStorm.
* User: kayladnls
* Date: 12/13/14
* Time: 7:24 PM
*/
// Bt(z)^r == sigma(k) * (tk + r, k) * r / tk + r * z^k
// Problem: Compute the number of 4th order ternary trees;
// t = 3; k = 4;, r = 1
// Binomial coefficients (tk + r, k) is computed
// n!/(n-k)!k!
$t = 3;
$r = 1;
$k = 4;
$string_answer = "1 +";
$arr = array_fill(0, $k, 0);
foreach ($arr as $idx => $value)
{
$i = $idx + 1;
$n1 = ($t*$i) + $r;
$n2 = $i;
$bc = binomialCoefficient($n1, $n2);
$answer = $bc * ($r/(($t*$i) +$r));
$string_answer .= strval($answer)."z^".$i." + ";
}
echo trim($string_answer, '+');
function binomialCoefficient($num1, $num2)
{
return factorial($num1) / (factorial(($num1 - $num2)) * factorial($num2));
}
function factorial($n)
{
$answer = $n;
while ($n > 1)
{
$answer *= ($n-1);
$n--;
}
return $answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment