Skip to content

Commit

Permalink
Merge branch 'bpetering'
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Aug 1, 2009
2 parents 3d5e772 + 334d879 commit 53a8517
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions perl6/003/01.pl
@@ -0,0 +1,63 @@
use v6;

=begin pod
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
=end pod

class PrimeIter {
has $!cur = 2;
method next {
($!cur++ and return 1) if $!cur == 1;
($!cur++ and return 2) if $!cur == 2;
($!cur++ and return 3) if $!cur == 3;
$!cur++;
$!cur++ while ! self.cur_is_prime;
return $!cur;
}
method reset {
$!cur = 2;
}
method cur_is_prime {
# uncomment & rename method for easy conversion to normal sub :)
#my $n = @_.shift;
my $n = $!cur;

return 0 if $n % 2 == 0;
return 1 if $n < 4;

my $x = 3;
while ( $x <= sqrt $n ) {
return 0 unless $n % $x;
$x += 2;
}
return 1;
}
}

my $to_factor = 600851475143;
my $pi = PrimeIter.new;

my @prime_factors;
my $n = $to_factor;

while $n > 1 {
my $try_prime = $pi.next;
#say "tp = $try_prime";
if $n % $try_prime == 0 {
@prime_factors.unshift($try_prime);
$n /= $try_prime;
#say "removed $try_prime, n=$n";
$pi.reset;
}
}

say @prime_factors[0];

#say "{@prime_factors}"; # array interp broken in rakudo 2009-07-25



0 comments on commit 53a8517

Please sign in to comment.