in reply to Prime list

> been sitting with this broken code for hours now and cant fix it,

no wonder it's unreadable... indentation rules man!

get perltidy or at least a decent editor!

update

I was bored, here your homework:

your basic problem is that you have to skip to the next iteration in the outer loop, thats what next LABEL does!

( did some adjustments to readability).

my @primes=(); OUTER: for ( my $i=2; @primes < 100; $i++) { for my $p (@primes) { if ($i % $p == 0) { next OUTER; } } push @primes, $i; } print "@primes";

Cheers Rolf

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Prime list
by AnomalousMonk (Archbishop) on Feb 08, 2015 at 22:45 UTC

    And, of course, the classic regex approach:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @primes; ;; for (my $i = 2; @primes < 10; ++$i) { push @primes, $i if ('x' x $i) !~ m{ \A (..+) \1+ \z }xms; } ;; print qq{@primes}; " 2 3 5 7 11 13 17 19 23 29


    Give a man a fish:  <%-(-(-(-<

      Maybe a nice educational example to explain, how \1 backreference and backtracking in regex work... not so much for primes though.

      Homework wise I mean! ;-)

      Cheers Rolf

      PS: Je suis Charlie!

        But it's such a deliciously scandalous (ab)use of the RE that I had to throw it in! And in any event, a student's reach should exceed his or her grasp...


        Give a man a fish:  <%-(-(-(-<

      I love this one. Thanks for throwing it up again.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of