10. februar 2010

$gcd and $lcm ; greatest common divisor and least common multiple

/*

$gcd(n,m) the greatest common divisor of m and n
$lcm(n,m) the least common multiple of m and n

*/


alias gcd {
if ($2 > $1) tokenize 32 $2 $1
var %a $1,%b $2,%c
while (%b) {
%c = %b
%b = $calc(%a % %b)
%a = %c
}
return %a
}
alias lcm return $calc($1 * $2 / $gcd($1,$2))

Ingen kommentarer:

Send en kommentar