P36 - 确定给定正整数的质因数 (2)。

作者:Curtis Poe

提示:此问题与问题 P13 类似。

这最初是一篇博客文章:http://blogs.perl.org/users/ovid/2010/08/prime-factors-in-perl-6.html

规范

P36 (**) Determine the prime factors of a given positive integer (2).
    Construct a list containing the prime factors and their multiplicity.

示例

> prime_factors_mult(315).perl.say
(3 => 2, 5 => 1, 7 => 1)

源代码:P36-ovid.pl

use v6;

constant PRIMES = grep { .is-prime }, 2 .. *;

sub prime-factors(Int $number-to-factor where * > 1 --> Hash) {
    return { $number-to-factor => 1 } if $number-to-factor.is-prime;

    my %factors;
    my $number = $number-to-factor;
    for PRIMES.cache -> $prime {
        last if $prime ** 2 > $number;
        while $number %% $prime {
            %factors{$prime}++;
            $number div= $prime;
        }
    }
    %factors{$number}++ if $number != 1;  # we have a prime left over
    return %factors;
}

for 2, 17, 53, 90, 94, 200, 289, 62710561 -> $number {
    say "Prime factors of $number are: {prime-factors($number).perl}";
}