最大质因数

作者:Mark A. Hershberger

https://projecteuler.net/problem=3

13195 的质因数是 5、7、13 和 29。

600851475143 的最大质因数是多少?

源代码:prob003-hexmode.pl

use v6;

class Prime {
    has @!primes;
    has Int $!number = 1;

    method next() {
        my $not_prime = 1;

        while $not_prime && $!number++ {
            $not_prime = @!primes.grep({$!number % $^a == 0});
        }
        @!primes.push($!number);

        my $copy = $!number;
        return $copy;
    }
}

my $number = 600851475143;

my $prime = Prime.new;
my $current-prime;
my @primes = gather repeat {
    $current-prime = $prime.next;
    if $number %% $current-prime {
        take $current-prime;
        $number /= $current-prime;
    }
} while ($current-prime < $number);

say @primes.max;