In my opinion, there is nothing wrong with your code. The output from the code in the OP is exactly the same as the code output in the accepted answer . You can run this (where getcount() is a function from OP, and getcount2() is a function from Balaz Varga 's answer ):
for ($i=0; $i<10000; $i++) { $a=mt_rand(1,50); $b=mt_rand(1,50); $r1 = getcount($a, $b); $r2 = getcount2($b, $b); if ($r1 != $r2) { echo "D'oh!"; } }
and he will not return anything.
The only drawback is that your code gives a warning when getcount(0, 0) run. Also, the second line in your code ( $min=min($length,$breadth); ) is a bit redundant. You can write the same:
function getcount($length,$breadth,$count=0){ if($length>$breadth){ $length=$length-$breadth; $count++; return getcount($length,$breadth,$count); } else if($breadth>$length){ $breadth=$breadth-$length; $count++; return getcount($length,$breadth,$count); } else if ($breadth!=0){ $count++;
source share