[NBLUG/talk] Euclid's algorithm in Perl

Eric Eisenhart eric at nblug.org
Thu May 29 11:02:00 PDT 2003


On Thu, May 29, 2003 at 09:54:41AM -0700, mrp wrote:
> # slight adaptation.. requires one more loop through, but will usually be 
> # faster becuase comparison in each loop is simpler:  You only need to
> # compute $m%$n once.
> 
> sub euc_alg {
> 	my ($m,n) = @_;
> 
> 	($m,$n) = ($n,$m%$n) while $n;
> 
> 	return $m;
> }

Or, this variation, that can handle numbers bigger than 2^31:
use Math::BigInt;
sub euc_alg {
    my ($m, $n) = @_;
    my $mb = Math::BigInt->new($m);
    return $mb->bgcd($n);
}

Okay, now go do it in a regular expression.  ;)
-- 
Eric Eisenhart
NBLUG Co-Founder & Vice-President Pro Tempore
The North Bay Linux Users Group
http://nblug.org/
eric at nblug.org, IRC: Freiheit at freenode, AIM: falschfreiheit, ICQ: 48217244
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://nblug.org/pipermail/talk/attachments/20030529/f7a82851/attachment.pgp


More information about the talk mailing list