[NBLUG/talk] Euclid's algorithm in Perl

Steve Zimmerman stevetux at sonic.net
Thu May 29 09:30:04 PDT 2003


On Thursday 29 May 2003 08:05 am, Eric Eisenhart wrote:
> 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;
> }

Thank you.  That's why I like C.
*grin*
-- 
Steve Zimmerman



More information about the talk mailing list