Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Learning math and efficient algorithms using perl

by andreas1234567 (Vicar)
on Sep 06, 2007 at 13:24 UTC ( [id://637416]=perlquestion: print w/replies, xml ) Need Help??

andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I try my best at Project Euler. Both fun and highly challenging.

Problem 12 reads Which is the first triangle number to have over five-hundred divisors? Being a Brute Force Monk, I came up with this highly inefficient algorithm. I want to learn more on math and efficient algorithms using perl, and I humbly seek the Monks advice on how to I acquire such skills.

--
Andreas

Replies are listed 'Best First'.
Re: Learning math and efficient algorithms using perl
by rhesa (Vicar) on Sep 06, 2007 at 14:36 UTC
Re: Learning math and efficient algorithms using perl
by swampyankee (Parson) on Sep 06, 2007 at 15:15 UTC

    Another site that may be of interest is Dictionary of Algorithms and Data Structures.

    Also, when you say "500 divisors" do you mean 500 distinct divisors (2500 has one divisor, repeated 500 times)?


    emc

    Information about American English usage here and here.

    Any New York City or Connecticut area jobs? I'm currently unemployed.

      No, 2500 has 501 divisors. It's divisible by 1, 2, 4, ..., 2499, 2500.

        OK; 2 distinct divisors, one of which is repeated 500 times...


        emc

        Information about American English usage here and here.

        Any New York City or Connecticut area jobs? I'm currently unemployed.

      Also, when you say "500 divisors" do you mean 500 distinct divisors?
      I believe that's a correct observation.
      --
      Andreas
Re: Learning math and efficient algorithms using perl
by blazar (Canon) on Sep 09, 2007 at 10:45 UTC

    I try my best at Project Euler. Both fun and highly challenging.

    Problem 12 reads Which is the first triangle number to have over five-hundred divisors?

    I also play with PE and this is what I had used:

    #!/usr/bin/perl -l use strict; use warnings; my $n; my @div=(0); while (++$n) { my ($m,$p)=($n,0); $m /= 2 and $p++ until $m & 1; $div[$n] = $p ? $div[$m]*$p : grep !($n%$_), 1..$n; print $n*($n-1)/2 and last if $div[$n]*$div[$n-1] > 500; } __END__
Re: Learning math and efficient algorithms using perl
by artist (Parson) on Sep 06, 2007 at 20:36 UTC
    You can improve the speed of your code with:
    sub get_nof2 { return scalar @{ divisors(+shift) }; } { my $D; sub divisors { my $n = shift; foreach my $d ( 2 .. sqrt($n) ) { unless ( $n % $d ) { my $div = $n / $d; unless ( defined $D->{$div} ) { $D->{$div} = divisors($div); } return $D->{$n} = [ uniq( @{ $D->{$div} }, map { $d * $_ } @{ $D->{$div} } + ) ]; } } return $D->{$n} = [ 1, $n ]; }
    --Artist

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://637416]
Approved by Corion
Front-paged by blazar
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found