Guest User

http://www.reddit.com/r/dailyprogrammer/comments/psf4n/21620

a guest
Feb 16th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1.  
  2. <?php
  3. while (1) {
  4.     echo "INPUT ZERO-BASED COORDINATES\n";
  5.     $handle = fopen("php://stdin", "r");
  6.     $line = fgets($handle);
  7.     $coords = explode(', ', trim($line));
  8.     if (!preg_match('/^\d+$/', $coords[0]) && !preg_match('/^\d+$/', $coords[1])) {
  9.         die("INVALID INPUT\n");
  10.     }
  11.  
  12.     echo "RESULT: " . calculate($coords[1], $coords[0]) . "\n";
  13. }
  14.  
  15. function calculate($x, $y) {
  16.     static $triangle;
  17.     if (($x == 0) || ($x == $y)) {
  18.         return 1;
  19.     }
  20.     if ($x > $y) {
  21.         return 0;
  22.     }
  23.     if (!isset($triangle[$y - 1][$x])) {
  24.         $triangle[$y - 1][$x] = calculate($x, $y - 1);
  25.     }
  26.     if (!isset($triangle[$y - 1][$x - 1])) {
  27.         $triangle[$y - 1][$x - 1] = calculate($x - 1, $y - 1);
  28.     }
  29.     return ($triangle[$y - 1][$x] + $triangle[$y - 1][$x - 1]);
  30. }
  31.  
  32. ?>
Add Comment
Please, Sign In to add comment