Course of Raku / Addendum 🆕 / Working with data / Numbers and mathematics / Exercises / Greatest common divisor and least common multiple
Solution: Greatest common divisor and least common multiple
Here is a possible solution to the task.
Code
my ($a, $b) = 24, 36;
my $g = $a gcd $b;
say "gcd = $g";
say "lcm = { $a * $b div $g }";🦋 You can find the source code in the file gcd-lcm.raku.
Output
gcd = 12
lcm = 72Comments
gcdis a built-in infix operator, so$a gcd $bgives the greatest common divisor directly.The least common multiple is the product of the two numbers divided by their gcd — computed inside the interpolation
{ ... }with integer divisiondiv.
Course navigation
← Greatest common divisor and least common multiple | Digital root →