*Compose a fraction from the two given integersâ€”numerator and denominatorâ€”and reduce it to lowest terms.*

5/15 and 16/280 are examples of fractions that can be reduced. The final results of this task are 1/3 and 2/35. Generally, the algorithm of reducing a fraction requires searching for the greatest common divisor, and then dividing both numerator and denominator by that number.

In Perl 6, there is a built-in operatorÂ `gcd`

that returns the greatest common divisor, so you can use it to solve the task (notice thatÂ `gcd`

is used as an operator, not a function):

my $a = 16;

my $b = 280;

my $gcd = $a gcd $b;

say $a/$gcd;# 2

say $b/$gcd;# 35

That is a classical solution, but Perl 6 offers something much more magicalâ€”the Rational data typeÂ `Rat`

.

my Rat $r = $a/$b;

say $r.numerator;# 2

say $r.denominator;# 35

TheÂ `Rat`

value keeps its parts as two integers, which are accessible via theÂ `numerator`

andÂ `denominator`

methods. TheÂ `perl`

method formats theÂ `Rat`

number in the following way:

say $r.perl;Â Â Â Â Â Â Â Â# <2/35>