Project Euler 3: Find the Largest Prime Factor
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 317584931803?
To calculate the prime factors, I loop through the prime numbers, dividing the number with each if it is divisible. I used a modified version of my prime calculator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | my $m = 3175849318035832905; my @primes = (); my $i = 1, $b = 0; while ( $i < $m ) { # looped until the final prime factor has been found $i++; foreach (@primes) { if( ! ( $i % $_ ) ) { $b = 1; next; } } if ( $b ) { $b = 0; } else { # if it is a prime push @primes, $i; while ( ! ( $m % $i ) ) { # as long as $m is divisbible by the prime print "$i \n"; # print the prime (since it's a prime factor) $m /= $i; # divide $m by the prime } } } |
The last number printed is the highest prime factor. Explanations for the modifications are in the code.

This did not help me!!!!! :(
Comment by Carrie Confused — May 9, 2007 @ 3:27 pm
Carrie, I’m sorry. I don’t support Liberal Arts majors.
Comment by Tim — May 9, 2007 @ 10:57 pm