# ****************************************************** # * # * Name: prg-calc-primes-v2.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 # Array A(1, 2) = (2, 3) # 4 - Set found primes, C = 2 # 5 - For M = 4 to N # 6 - For Each Array A(P) # If P > SQRT(M), then prime # If M modulus P = 0, then non-prime # 7 - Otherwise, write prime number to result file # 8 - Array A(Max) = M if prime # 9 - Increment found primes, C++ # 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 = ','; # Define a global array to hold primes my @primes = (2, 3); # # IsPrime() - determine if a number > 0 is prime # sub IsPrime() { # get the input parameter my ($var) = @_; # division by primes foreach (@primes) { # get current prime my $cur = $_; # no need to check further if ($cur > (sqrt($var) + 1)) { push (@primes, $var); return 1; }; # number is not prime if (($var % $cur) == 0) { return 0; }; }; # number is prime push (@primes, $var); 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\\primes2\\primes2.csv'); # header info - write out data $line = 'line no' . $del . 'prim no' . $nl; print TXT $line; # 1st prime - write out data $line = '1' . $del . $primes[0] . $nl; print TXT $line; # 2nd prime - write out data $line = '2' . $del . $primes[1] . $nl; print TXT $line; # find prime numbers $found = 2; for (my $cnt = 4; $cnt <= $var; $cnt++) { # found a prime by factorization if (&IsPrime($cnt) == 1) { # increment found $found++; # prime info - write out data - last array element $line = $found . $del . $primes[-1] . $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\\primes2\\primes2.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;