Course of Raku / Addendum 🆕 / Working with data / Numbers and mathematics / Exercises / Prime factorisation

Solution: Prime factorisation

Here is a possible solution to the task.

Code

my $n = 360;
my @factors;

for 2..$n -> $d {
    while $n %% $d {
        @factors.push($d);
        $n div= $d;
    }
}

say @factors;

🦋 You can find the source code in the file prime-factors.raku.

Output

[2 2 2 3 3 5]

Comments

  1. For each candidate divisor $d, the inner while divides it out of $n as many times as it fits, pushing $d each time. Because the smaller factors are removed first, every $d that still divides $n is guaranteed to be prime.

  2. $n div= $d is integer division back into $n; it shrinks the number until nothing but 1 is left.

Course navigation

Prime factorisation   |   Greatest common divisor and least common multiple