#!/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 filenames. Your choice. my $beginloc = "00000"; #The number to start counting at when 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() { push (@parray, $_); } close PRIMES; open RESULTS, ">".$path.$outputloc.$last; print "Welcome to the prime number program.\nThis program will tell you every prime number within a certain range.\nPlease enter the lower range now: "; $lowrange = ; 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? "; $hirange = ; chomp $hirange; if($hirange <= $lowrange) { $hirange = $lowrange + 100; print "\nYour second number must be at least equal to the first. \nI have arbitratily set it to be 100 greater.\n"; } print "Now computing the range of all primes between $lowrange and $hirange. \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";