Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Created November 3, 2012 00:53
Show Gist options
  • Select an option

  • Save jehoshua02/4005312 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/4005312 to your computer and use it in GitHub Desktop.
This was one of the mensa puzzles in a box that I have.
<?php
function factorial($number)
{
$factorial = $number;
$i = $number - 1;
while ($i > 0) {
$factorial *= $i--;
}
return $factorial;
}
function possibleRoutes($rows, $columns)
{
$m = $rows - 1;
$n = $columns - 1;
$routes = factorial($m + $n) / (factorial($m) * factorial($n));
return $routes;
}
assert(possibleRoutes(3, 3) == 6);
assert(possibleRoutes(4, 3) == 10);
assert(possibleRoutes(6, 6) == 252);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment