Skip to content

Instantly share code, notes, and snippets.

@tiagodavi
Created November 18, 2011 22:42
Show Gist options
  • Save tiagodavi/1378004 to your computer and use it in GitHub Desktop.
Save tiagodavi/1378004 to your computer and use it in GitHub Desktop.
Simplificando e juntando a recursividade com closures.
<?php
//mdc (PHP 5.3)
$mdc = function($x,$y) use (&$mdc){
$a = max($x,$y);
$b = min($x,$y);
return ($a%$b == 0) ? ($b) : ($mdc($b,($a%$b)));
};
//mmc (PHP 5.3)
$mmc = function($x,$y) use(&$mdc){
return (($x*$y)/($mdc($x,$y)));
};
echo $mdc(48,30); //6
echo $mmc(4,6); //12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment