[NBLUG/talk] Euclid's algorithm in Perl

Eric Eisenhart eric at nblug.org
Thu May 29 08:04:00 PDT 2003


On Thu, May 29, 2003 at 07:56:22AM -0700, Steve Zimmerman wrote:
> From TAoCP, vol. 1, p. 2 (Euclid's algorithm).  This is an
> implementation in C.  How would it be done in Perl?

Well, the obvious would be to do it the exact same way:

#!/usr/bin/perl -w
use strict;

# This'd be better with the readline library and separate prompts...
print ("Gimme two ints: ");
($int1, $int2) = split(m/\s+/, <>);

print &euc_alg($int1, $int2), "\n";

sub euc_alg {
    my ($m, $n) = @_;
    my $r;

    while ($m % $n != 0) {
        $r = $m % $n;
        $m = $n;
        $n = $r;
    }
    return $n;
}

-- 
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/b854352d/attachment.pgp


More information about the talk mailing list