# ****************************************************** # * # * Name: prg-calc-primes-v1.pl # * # * Design Phase: # * Author: John Miner # * Date: 05/10/2012 # * Purpose: Logging to file and event logs. # * # ****************************************************** # # Algorithm: # # 1 - Read in program arguement (N) # 2 - Write arguement N to log file # 3 - Write 2 & 3 as first two primes to result file # 4 - Set found prime count = 2 # 5 - For M = 4 to N # 6 - For O = 2 to SQRT(M) # 7 - If M modulus O = 0, then non-prime # 8 - Otherwise, write prime number to result file # 9 - Increment found prime counter # 10 - Write total number of primes & elapsed time to log file # 11 - Display total number of primes & elapsed time to crt # ~ # Setup # ~ # Declare all variables use strict; # Include defined module (CPAN) use Date::Calc qw(:all); # Constants my $nl = "\n"; my $del = ','; # # IsPrime() - determine if a number > 0 is prime # sub IsPrime() { # get the input parameter my ($var) = @_; # not a prime if ($var == 1) { return 0; }; # a prime number if ($var == 2) { return 1; }; # trial division for (my $cnt = 2; $cnt <= (sqrt($var) + 1); $cnt++) { if (($var % $cnt) == 0) { return 0; }; }; # number is prime return 1; }; # # StorePrimes() - calculate primes from 2 to n # sub StorePrimes() { # variables my $line = ''; my $found = 0; # get input parameter my ($var) = @_; # open the text file open(TXT, '>c:\\perl-depot\\primes\\primes.csv'); # header info - write out data $line = 'line no' . $del . 'prim no' . $nl; print TXT $line; # 1st prime - write out data $line = '1' . $del . '2' . $nl; print TXT $line; # 2nd prime - write out data $line = '2' . $del . '3' . $nl; print TXT $line; # find prime numbers $found = 2; for (my $cnt = 4; $cnt <= $var; $cnt++) { # found a prime by trial division? if (&IsPrime($cnt) == 1) { # increment found $found++; # prime info - write out data $line = $found . $del . $cnt . $nl; print TXT $line; }; }; # close text file close(TXT); # return count return $found; } # ~ # Step 1 # ~ # local variables my $max1 = 0; my $found1 = 0; my $total1 = 0; # Read in argv if ($#ARGV + 1 != 1 ) { print 'Usage: ' . $nl; print ' prg-calc-primes-v1 ' . $nl; print ' ' . $nl; exit; }; $max1 = $ARGV[0]; # get full time (y,m,d, h,m,s, dy,dw,ds) my @clock1 = System_Clock(); # Display message (crt) print 'Starting program' . $nl; print localtime() . $nl; print ' ' . $nl; # ~ # Step 2 # ~ # Open the log file open(LOG, '>c:\\perl-depot\\primes\\primes.log'); # What is the upper limit? print LOG 'Max Integer = ' . $max1 . $nl; # ~ # Step 3 to 9 # ~ $found1 = &StorePrimes($max1); # ~ # Step 10 # ~ # get full time (y,m,d, h,m,s, dy,dw,ds) my @clock2 = System_Clock(); # calc difference (day, hr, min, sec) my ($tday, $thr, $tmin, $tsec)= Delta_DHMS($clock1[0], $clock1[1], $clock1[2], $clock1[3], $clock1[4], $clock1[5], $clock2[0], $clock2[1], $clock2[2], $clock2[3], $clock2[4], $clock2[5]); # should be in seconds $total1 = ($tday * 86400) + ($thr * 3600) + ($tmin * 60) + $tsec; # How many found? print LOG 'Primes Found = ' . $found1 . $nl; print LOG 'Elapsed Time (s) = ' . $total1 . $nl; # Close the text file close(LOG); # ~ # Step 11 # ~ # Display message (crt) print 'Ending program' . $nl; print localtime() . $nl; print ' ' . $nl; print 'Elapsed Time (s) = ' . $total1 . $nl; print ' ' . $nl;