With some basic algebra you can eliminate the need for any loops:
function findJumps($jump_height, $slip, $wall_height, $walls) {
return $walls * ($jump_height > $wall_height) ? 1 : ceil(($wall_height - $slip) / ($jump_height - $slip));
}
Explanation:
For every jump the prisoner makes he gains $jump_height
but loses $slip
in elevation, so his change in elevation can be represented as $jump_height - $slip
. To find how many jumps per wall simply divide the wall height by the change in elevation $wall_height / ($jump_height - $slip)
. Since it's possible that the result of this isn't even, we have to round up with ceil()
(Ex. if you jumped 2 feet and the wall was 3 feet high, you'd need to take two jumps to completely clear the wall). The simply multiply this by the number of walls he has to scale.