Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- while (1) {
- echo "INPUT ZERO-BASED COORDINATES\n";
- $handle = fopen("php://stdin", "r");
- $line = fgets($handle);
- $coords = explode(', ', trim($line));
- if (!preg_match('/^\d+$/', $coords[0]) && !preg_match('/^\d+$/', $coords[1])) {
- die("INVALID INPUT\n");
- }
- echo "RESULT: " . calculate($coords[1], $coords[0]) . "\n";
- }
- function calculate($x, $y) {
- static $triangle;
- if (($x == 0) || ($x == $y)) {
- return 1;
- }
- if ($x > $y) {
- return 0;
- }
- if (!isset($triangle[$y - 1][$x])) {
- $triangle[$y - 1][$x] = calculate($x, $y - 1);
- }
- if (!isset($triangle[$y - 1][$x - 1])) {
- $triangle[$y - 1][$x - 1] = calculate($x - 1, $y - 1);
- }
- return ($triangle[$y - 1][$x] + $triangle[$y - 1][$x - 1]);
- }
- ?>
Add Comment
Please, Sign In to add comment