Superlman has asked for the wisdom of the Perl Monks concerning the following question:
But, of course, that's not good enough
Below is a simple prime number program I wrote not long ago. What I'd like is for you illustrious monks to go over it and criticize (mock?) EVERYTHING. No matter how small or trivial. Is there a more efficient way to do $THING? Can my code be made easier to read / terse / faster / better smelling? Add features? Remove bloat?
You get the idea. Nitpick! Educate! Correct! (And a free ++ from me for each helpful comment, no matter how vicious)
#!/usr/local/bin/perl -w use strict; #These variables are user-configurable. my $countermax = 10000; #The max number of primes per file. ( +To regulate file size) my $primefile = "Primes.seed"; #The location of the seed file. This + is needed. my $path = "./"; #The first part of the path to dump the files in. + This must be pre-existing. my $last = ".txt"; #The extension to append to the file +names. Your choice. my $beginloc = "00000"; #The number to start counting at whe +n making filenames. #End users should not edit below this line my $lowrange = 0; my $hirange = 0; my $current = 0; my $i = 0; my $primey = 0; my $counter = 0; my @parray; my $outputloc = $beginloc; #First this looks for a pre-existing primes file #which has all the primes less than 10000 in it. #It needs this file. Assume it is present or #suggest a better way or something... open PRIMES, $primefile; while(<PRIMES>) { push (@parray, $_); } close PRIMES; open RESULTS, ">".$path.$outputloc.$last; print "Welcome to the prime number program.\nThis program will tell yo +u every prime number within a certain range.\nPlease enter the lower +range now: "; $lowrange = <STDIN>; chomp $lowrange; #Simple idiot-proofing. I didn't bother to check to make #sure it's actually an integer. What's a good way to #do this? if($lowrange < 0) { $lowrange = 0; print "\nYour number must be positi +ve. I have set it to 0 for you."; } print "\nHow high should the program search for primes? "; $hirange = <STDIN>; chomp $hirange; if($hirange <= $lowrange) { $hirange = $lowrange + 100; print "\nYour second number must be at least equal to the first. \nI h +ave arbitratily set it to be 100 greater.\n"; } print "Now computing the range of all primes between $lowrange and $hi +range. \nPlease wait.\n"; #No need to check even numbers, right? if($lowrange % 2 == 0) { $lowrange++; } #Of course, since 1 isn't a prime, we have to account #for that. Mathmaticians, LART me if I'm wrong. if($lowrange == 1) { print RESULTS "2\n"; $lowrange = 3; $counter++; } for($current = $lowrange; $current <= $hirange; $current += 2) { $primey = 1; for($i = 0; $parray[$i] <= sqrt($current); $i++) { if($current % $parray[$i] == 0) { $primey = 0; $i = 10000; } } if($primey == 1) { $counter++; #print "PRIME FOUND: $current\n"; print RESULTS "$current\n"; if($counter%$countermax == 0) { close RESULTS; $outputloc++; print "\nFile full. Currently ".($current *100/$hirange)."\% done."; open RESULTS, ">".$path.$outputloc.$last; } } } print "\nTotal amount of primes: $counter \n"; print "Your results have been saved, starting with ".$path.$beginloc.$last."\n\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mock my code!
by Masem (Monsignor) on Sep 06, 2001 at 17:06 UTC | |
by Hofmator (Curate) on Sep 06, 2001 at 18:04 UTC | |
|
Re: Mock my code!
by Sifmole (Chaplain) on Sep 06, 2001 at 17:43 UTC | |
|
Re: Mock my code!
by dragonchild (Archbishop) on Sep 06, 2001 at 18:14 UTC | |
|
Re: Mock my code!
by Cine (Friar) on Sep 06, 2001 at 17:59 UTC | |
|
Re: Mock my code!
by blakem (Monsignor) on Sep 07, 2001 at 12:54 UTC | |
by tilly (Archbishop) on Sep 08, 2001 at 06:00 UTC | |
|
Re: Mock my code!
by gornox_zx (Priest) on Sep 06, 2001 at 22:43 UTC |