最大质因数
作者:Eric Hodges
https://projecteuler.net/problem=3
13195 的质因数是 5、7、13 和 29。
600851475143 的最大质因数是多少?
use v6;
sub is_prime ($num) {
for (2..^$num) {
return 0 unless $num % $_;
}
return 1;
};
class Primes {
has $.current = 0;
method next {
$!current++;
$!current++ until is_prime($.current);
return $.current;
}
}
my $prime = Primes.new();
my $number = 600851475143;
my @primes = gather while ($number > 1) {
if !($number % $prime.next) {
$number /= $prime.current;
take $prime.current;
}
}
say @primes.max;
Perl 6 示例