andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
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.
use strict; use warnings; my $tri = 1; my $i = 1; while ($i++) { $tri += $i; my $nof = 2 * scalar(grep { $tri % $_ == 0 } 1 .. sqrt($tri)); if ($nof >= 500) { print "The " . $i . "th triangle number $tri has $nof divisors"; last; } } __END__ $ time perl -wl 12.pl The 12375th triangle number 76576500 has 576 divisors real 0m26.947s user 0m25.013s sys 0m0.027s
|
|---|
| 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 | |
by rhesa (Vicar) on Sep 06, 2007 at 15:42 UTC | |
by swampyankee (Parson) on Sep 07, 2007 at 16:47 UTC | |
by blokhead (Monsignor) on Sep 07, 2007 at 16:57 UTC | |
by andreas1234567 (Vicar) on Sep 06, 2007 at 18:49 UTC | |
|
Re: Learning math and efficient algorithms using perl
by blazar (Canon) on Sep 09, 2007 at 10:45 UTC | |
|
Re: Learning math and efficient algorithms using perl
by artist (Parson) on Sep 06, 2007 at 20:36 UTC |