` 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. |
Comment onEuclidean algorithm Golf in Perl 6DownloadCode