Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

(jeffa) Re: Continued Fractions

by jeffa (Bishop)
on Nov 16, 2000 at 22:54 UTC ( #42017=note: print w/replies, xml ) Need Help??


in reply to Continued Fractions

You rule Tilly!

I had to point out the mod operator in this function:

sub gcd {
    my ($n, $m) = @_;
    while ($m) {
      my $k = $n % $m;
      ($n, $m) = ($m, $k);
    }
    return $n;
}
. . . particularly the use of the mod operator. I remember reading Robert Sedgewick's Mastering Algorithms in C++ and really getting a kick out of the first example - Euclide's greatest common denominator formula.

At first, Sedgewick implemented it with the division operator:

my $k = $n / $m;
Then he explained that replacing the / operator with the % operator results in about 75% less iterations through the while loop. Incredible.

Jeff

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://42017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2018-09-21 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (185 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!