in reply to Re: CarTalk Puzzler
in thread CarTalk Puzzler

That's not a very good benchmark, as you will be pushing the answer onto the same array over and over again. After running your benchmark, @main::sol contains 28200 elements.

I don't understand why you are using our all over your program. What's wrong with my?

But you missed a much faster solution: just taking the squares of the numbers from 1 to the square root of 20_000.

Here's a revised benchmark:

use strict; use warnings; my (@sol1, @sol2, @sol3); my $lights = 20_000; sub sol1 { #init; my @lit = (1) x ($lights+1); #flip; for (2..($lights+1)) { my $cnt = $_; while ($cnt <= $lights) { $lit[$cnt] = !$lit[$cnt]; $cnt+=$_; } } #answer; @sol1 = grep {$lit[$_]} 1 .. ($lights+1); } sub sol2 { @sol2 = (); for (1..$lights) { my $sqr = sqrt($_); push @sol2, $_ if int($sqr) == $sqr; } } sub sol3 { @sol3 = map {$_**2} 1 .. sqrt($lights); } use Benchmark ':all'; cmpthese( -10, { sol1 => \&sol1, sol2 => \&sol2, sol3 => \&sol3, }); __END__ Rate sol1 sol2 sol3 sol1 3.92/s -- -90% -100% sol2 41.0/s 945% -- -99% sol3 3762/s 95923% 9085% --
Perl --((8:>*

Replies are listed 'Best First'.
Re^3: CarTalk Puzzler
by radiantmatrix (Parson) on Nov 17, 2005 at 17:54 UTC

    Yeah, I don't know why I used our. Not enough coffee today, I guess -- it's not even a general habit of mine. Odd. Thanks, though.

    Thanks also for your even-faster solution, which reinforces my core point: understanding the problem well enough to rephrase it leads to much faster results!

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law