<?xml version="1.0" encoding="windows-1252"?>
<node id="772778" title="Re: Euclidean algorithm Golf in Perl 6" created="2009-06-18 12:28:54" updated="2009-06-18 12:28:54">
<type id="11">
note</type>
<author id="616540">
moritz</author>
<data>
<field name="doctext">
Two simple things you can do (also in Perl 5): 

&lt;p&gt;You can get rid of the temporary &lt;c&gt;$t&lt;/c&gt; by using list assignment:

&lt;code&gt;
    while $b != 0 {
        ($b, $a) = ($a % $b, $b);
    }
&lt;/code&gt;

&lt;p&gt;Or you can resort to recursion, ignoring your boundaries to the golf court:

&lt;code&gt;
sub gcd ($a, $b) {
    return $a if !$b;
    gcd($b, $a % $b);
}

say gcd(24, 42);
&lt;/code&gt;

&lt;p&gt;For none of these I can see something where Perl 6 really plays its strengths.

&lt;p&gt;So maybe something completely else? This might work, but since nobody implements &lt;c&gt;infix:&lt;...&gt;&lt;/c&gt; yet, I couldn't test it:

&lt;code&gt;
sub gcd ($a, $b) {
    ($a, $b ... { $^x % $^y || () })[*-1]
}
&lt;/code&gt;

&lt;p&gt;For example for &lt;c&gt;gcd(42, 24)&lt;/c&gt; it would produce the list &lt;c&gt;42, 24, 18, 6&lt;/c&gt;. During the next call to the block it the &lt;c&gt;$^x % $^y&lt;/c&gt; returns 0, and the block the empty list, ending the creation of the list. The last element is the gcd.</field>
<field name="root_node">
772766</field>
<field name="parent_node">
772766</field>
</data>
</node>
