fork download
  1. <?php
  2.  
  3. class BCN implements Iterator {
  4. private static $table = [];
  5. private $n, $k;
  6.  
  7. public function __construct ($n) { $this->n=$n; }
  8.  
  9. public function rewind() { $this->k=0; }
  10.  
  11. public function current() { return $this->get($this->k); }
  12.  
  13. public function key() { return $this->k; }
  14.  
  15. public function next() { ++$this->k; }
  16.  
  17. public function valid() { return $this->k <= $this->n; }
  18.  
  19. public function get ($k) {
  20. if ($this->n==0 || $k==0 || $this->n==$k) return 1;
  21.  
  22. if ($k > $this->n / 2)
  23. $k = $this->n-$k;
  24.  
  25. if (!isset (self::$table[$this->n][$k])) {
  26. $bcn = new BCN ($this->n-1);
  27. self::$table[$this->n][$k] = $bcn->get($k-1)+$bcn->get($k);
  28. }
  29.  
  30. return self::$table[$this->n][$k];
  31. }
  32. }
  33.  
  34.  
  35. function check_binom ($a, $b, $n) {
  36.  
  37. echo "($a+$b)^$n=", pow($a+$b, $n), '=';
  38.  
  39. $sum=0;
  40. $x = $a+$b-1;
  41. $xk = 1;
  42. foreach (new BCN($n) as $k=>$bc) {
  43. if ($k) echo '+';
  44. echo $bc,'*',$xk;
  45. $sum += $bc*$xk;
  46. $xk *= $x;
  47. }
  48.  
  49. echo '=', $sum;
  50. }
  51.  
  52.  
  53. $a=4;
  54. $b=5;
  55. $n=6;
  56. check_binom ($a, $b, $n);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
(4+5)^6=531441=1*1+6*8+15*64+20*512+15*4096+6*32768+1*262144=531441