in reply to Finding the greatest common divisor
Great heavens, don't do all that. You don't have to find all the divisors just to find the greatest common divisor. Just use this:
This algorithm was invented by Euclid about 2200 years ago.sub gcd { my ($a, $b) = @_; ($a,$b) = ($b,$a) if $a > $b; while ($a) { ($a, $b) = ($b % $a, $a); } return $b; }
--
Mark Dominus
Perl Paraphernalia
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Finding the greatest common divisor
by tilly (Archbishop) on Sep 04, 2001 at 00:09 UTC |