This has done a zillion times before, so I guess you posted it for critique.

Some comments, in random order:

To prove the last point, here's your code, with only stylistic changes made and the addition of strict compliancy. (Note: untested code!)

#!/usr/bin/perl -w use strict; system 'clear'; print "I will calculate the primes from 1 to the number you enter. Wha +t is the maximum number to check?\n"; my $max = <STDIN>; $max > 0 or die "Negatives or 0 not allowed!\n"; my $getal = $max + 1; for (; $getal--; $getal >= 0) { my $deeltal = 2; # A/B would give B in this case my $teller = 0; # A/B would give A in this case while ($getal % $deeltal != 0 and $deeltal + 2 <= $getal) { $teller++; $deeltal++; } if ($teller + 3 == $getal) { push @priemlijst, $getal; } } system 'clear'; push @priemlijst, 2; # Don't forget number 2! @priemlijst = reverse @priemlijst; print "The primes from 1 to $max are: @priemlijst\n";
Counting backwards means you have to reverse later. Why make it so hard for yourself? Here is how I would probably code a brute force prime number generator:
#!/usr/bin/perl -w use strict; $| = 1; chomp(my $max = <STDIN>); print "\n2 "; N: for my $i (3 .. $max - 1) { for my $k (2 .. $i - 1) { next N if $i % $k == 0; } print $i, " "; } print "\n";
Or, with the two optimizations implemented:
#!/usr/bin/perl -w use strict; $| = 1; chomp(my $max = <STDIN>); print "\n2 "; N: for (my $i = 3; $i < $max; $i += 2) { next unless $i % 6 == 5 or $i % 6 == 1; for (my $k = 3; $k < sqrt $i; $k += 2) { next N if $i % $k == 0; } print $i, " "; } print "\n";
The best way to learn from this is to study the differences between these four versions and the many other prime number generators.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: Calculate primes! by Juerd
in thread Calculate primes! by azerton

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.