in reply to Euclidean algorithm Golf in Perl 6
You can get rid of the temporary $t by using list assignment:
while $b != 0 { ($b, $a) = ($a % $b, $b); }
Or you can resort to recursion, ignoring your boundaries to the golf court:
sub gcd ($a, $b) { return $a if !$b; gcd($b, $a % $b); } say gcd(24, 42);
For none of these I can see something where Perl 6 really plays its strengths.
So maybe something completely else? This might work, but since nobody implements infix:<...> yet, I couldn't test it:
sub gcd ($a, $b) { ($a, $b ... { $^x % $^y || () })[*-1] }
For example for gcd(42, 24) it would produce the list 42, 24, 18, 6. During the next call to the block it the $^x % $^y returns 0, and the block the empty list, ending the creation of the list. The last element is the gcd.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Euclidean algorithm Golf in Perl 6
by John M. Dlugosz (Monsignor) on Jun 19, 2009 at 14:30 UTC |