in reply to Finding Primes

In addition to the more general problems pointed out by Zaxo, there are some run of the mill coding improvements that could be made.
&gen_primes(eval(1 . '0' x 10));

The number 1-with-ten-zeros-after-it can be written as 10 ** 10.

foreach (@primes) { ... foreach (@primes) { ... } }

You can make a brute force search run faster by cutting off areas of the search space that you know can be skipped.

In this case, there are a number of ways you can shrink the search space; for example, next if ( $a > $b ); allows you to avoid checking twice for $a * $b and $b * $a, and next PRIMESCAN if ( length($a * $b) > 400; would let you stop testing a sequence when the numbers got too large.

Even with these tricks, brute-force isn't going to be an efficient approach, for the reasons discussed elsewhere.

last PRIMESCAN and print <<__FOUND__

This invokes "last" before "print" -- meaning that the print never gets invoked. You probably want print <<__FOUND__ and last PRIMESCAN.

Replies are listed 'Best First'.
Re: Re: Finding Primes
by Tommy (Chaplain) on Aug 14, 2003 at 01:53 UTC

    Thanks for the tips :-)

    The number 1-with-ten-zeros-after-it can be written as 10 ** 10.

    Thanks! That's what I was looking for there. Yup, the eval(1 . '0' x 10) was a hack.

    This invokes "last" before "print" -- meaning that the print never gets invoked. You probably want print <<__FOUND__ and last PRIMESCAN.

    Yes, quite. Thanks.

    -- Tommy Butler, a.k.a. TOMMY