Your method of deleting from your @prime list is a bit suspect. First, a minor cleanup. I'm removing the special case for "2" - partly because of premature optimisation, and partly because I hate special cases. And I'm introducing a debugging aid: a print statement to tell us where we are.

use strict; use warnings; my @prime = 2; for my $i(3..100){ print "$i...\n"; push @prime, $i; for(@prime){ if(int($i/$_)==($i/$_) and $i!=$_){ for(@prime){ $_=~s/$i//; } } } } for(@prime){ print "$_\n"; }
Running this tells me that the problem happens when $i is 5. That's because during the $i == 4 loop, you've gone and set $prime[3] from 4 to "" (removing the "4"). Instead, what you really want to do is not bother to push $i onto @prime unless you know it already to be prime:
use strict; use warnings; my @prime = 2; for my $i(3..100){ print "$i...\n"; my $is_prime = 1; for(@prime){ if(int($i/$_)==($i/$_) and $i!=$_){ $is_prime = 0; last; } } push @prime, $i if $is_prime; } for(@prime){ print "$_\n"; }
IMO, this is a perfect excuse to use List::MoreUtils' "any" function:
use strict; use warnings; use List::MoreUtils qw/any/; my @prime = 2; for my $i(3..100){ push @prime, $i unless any { int($i/$_)==($i/$_) and $i!=$_ } @pri +me; } for(@prime){ print "$_\n"; }
(Thanks again to borisz for pointing this out to me earlier.)

I'm sure this can be optimised further (without drastic changes to your original algorithm), but I'll let you investigate this further.


In reply to Re: Primes. Again. by Tanktalus
in thread Primes. Again. by Andrew_Levenson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.