in reply to Mock my code!
#!/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!
|
|---|