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 = 72

Comments

  1. gcd is a built-in infix operator, so $a gcd $b gives the greatest common divisor directly.

  2. The least common multiple is the product of the two numbers divided by their gcd — computed inside the interpolation { ... } with integer division div.

Course navigation

Greatest common divisor and least common multiple   |   Digital root