I made a number of changes.
  1. Got rid of the primes file. Unless you're planning on working with substantially more than 10,000 primes, this way is just as fast, or faster. (You're not loading primes you won't need...)
  2. Unless you have a reason for only putting 10,000 primes in a file, I'd put everything in the same file. It makes working with them later a little easier.
Of course, the question arises of why you're even doing it this way. If you already have the list of primes, why not just start with lowrange and go to the hirange of the primes. If you're trying to determine further primes, you're going to find non-primes because you're not increasing your list of primes. (What if your list of primes ends at the 10,000th prime, but your range includes the 10,001st prime squared ...) This way, you guarantee to find only primes.
#!/usr/local/bin/perl -w use strict; ###################################################################### +########## my $resultFile = "results.txt"; my $lowrange = 0; my $hirange = 0; ###################################################################### +########## print "Welcome to the prime number program.\n"; print "This program will tell you every prime number within a certain +range.\n"; print "Please enter the lower range now: "; chomp($lowrange = <STDIN>); die "\nThe low must be a positive integer\n" if ($lowrange && $lowrange =~ /\D/); print "\nHow high should the program search for primes? "; chomp($hirange = <STDIN>); die "\nThe high must be a positive integer\n" if ($hirange && $hirange =~ /\D/); die "\nThe high must be greater than or equal to the low\n" if ($hirange < $lowrange); print "Now listing the range of all primes between $lowrange and $hira +nge. \n"; print "Please wait.\n"; $lowrange++ if ($lowrange % 2 == 0); open RESULTS, ">$resultFile" || die "Cannot open $resultFile for writing\n"; my $counter = 0; if ($lowrange <= 3) { if ($lowrange <= 2) { print RESULTS "2\n"; $counter++; } print RESULTS "3\n"; $lowrange = 5; $counter++; } CANDIDATE: for (my $candidate = $lowrange; $candidate <= $hirange; $candidate += +2) { for my $divisor (3 .. int(sqrt($candidate))) { next CANDIDATE unless $candidate % $divisor; } $counter++; print RESULTS "$candidate\n"; } close RESULTS; print "\nTotal amount of primes: $counter \n"; print "Your results have been saved to $resultFile\n"; __END__

------
We are the carpenters and bricklayers of the Information Age.

Vote paco for President!


In reply to Re: Mock my code! by dragonchild
in thread Mock my code! by Superlman

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.