I was writing a program to solve the #3 exercise on project Euler. I needed to
find the prime factors of a given number.
I have solved it already but a couple of the ways I tried to solve the problem
caused a problem.
The below worked for smaller numbers but when I used the 12 digit number given
by project Euler I got "Out of memory!"
chomp( my $factor = <>);
my @primefactor = grep { &isprime( $_ ) } ( grep { $factor % $_ == 0 }
1..$factor );
sub isprime {
my $numb = shift;
my @quot = grep {
if ( $numb % $_ == 0 ) {1;
} else { 0;}
} 2..$numb-1;
unless ( @quot ) {
1;
# say "prime";
} else {
0;
# say "not prime";
}
}
This one worked but caused my computer to crash.
my $xxx = 1;
while ( $xxx < $factor ) {
if ( $factor % $xxx == 0 and &isprime($xxx) ) {
say $xxx;
}
$xxx++
}
what is causing perl to do this? Would using a module save memory?Thanks for
any help.