in reply to Mock my code!
!/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 = 0; #The number to start counting at when maki +ng filenames. #End users should not edit below this line 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 || die $!; my @parray = <PRIMES>; close PRIMES; open RESULTS, ">".$path.$outputloc.$last || die $!; print <<TXT; Welcome to the prime number program. This program will tell you every prime number within a certain range. Please enter the lower range now: TXT #get from arg or stdin my $lowrange = int($ARGV[0]||<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 positive. I have set it to 0 for you." +; } print "\nHow high should the program search for primes? "; #get from arg or stdin my $hirange = int($ARGV[1]||<STDIN>); chomp $hirange; if($hirange <= $lowrange) { $hirange = $lowrange + 100; print <<TXT; Your second number must be at least equal to the first. I have arbitratily set it to be 100 greater. TXT } print "Now computing the range of all primes between $lowrange and $hi +range. \nPlease wait.\n"; #No need to check even numbers, right? $lowrange++ unless $lowrange % 2; my $counter=0; #Of course, since 1 isn't a prime, we have to account #for that. if($lowrange == 1) { print RESULTS "2\n"; $lowrange = 3; $counter++; } for(my $current = $lowrange; $current <= $hirange; $current += 2) { for(my $i = 0; $parray[$i] <= sqrt($current); $i++) { if($current % $parray[$i] == 0) { $counter++; print RESULTS "$current\n"; if($counter%$countermax == 0) { close RESULTS; $outputloc++; print "\nFile full. Currently ".($current *100/$hirange)."\% d +one."; open RESULTS, ">".$path.$outputloc.$last || die $!; } last; } } } print <<TXT; Total amount of primes: $counter Your results have been saved, starting with $path$beginloc$last TXT
|
|---|