Hi

I am just casually solving some Project Euler problems using Perl.

For problem 3 (https://projecteuler.net/problem=3) I solved it using the
code below. The problem is stated in the code comments.

The code is single threaded and takes about 5-10 minutes on a standard
Intel i5 CPU on a 3 year old Macbook Air.

Can PDL make this faster ? No bignums are being used.

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use POSIX qw(floor);
    # Problem 3:
    # The prime factors of 13195 are 5, 7, 13, 29.
    # What is the largest prime factor of 600851475143 ?
    my $num = 600851475143;

    my $m = floor(sqrt($num));
    print "max factor limit: $m\n";
    # solve using sieve of erastothenes
    #
    my @primes = ();
    my @possible = (2 .. $m);
    while (@possible) {
        my $p = shift @possible;
        @possible = grep { $_ % $p != 0 } @possible;
        push @primes, $p;
        print "Remaining: ", scalar(@possible), "\n";
        print "Found possible prime factor: $p\n";
    }
    my @factors = grep { $num % $_ == 0 } @primes;
    if (@factors) {
        print "Prime Factors of $num: ", join(",", @factors), "\n";
    } else {
        print "$num is prime\n";
    }


_______________________________________________
Perldl mailing list
Perldl@jach.hawaii.edu
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to