{"id":2302,"date":"2012-07-05T19:43:24","date_gmt":"2012-07-05T19:43:24","guid":{"rendered":"http:\/\/craftydba.com\/?p=2302"},"modified":"2024-02-17T14:28:43","modified_gmt":"2024-02-17T14:28:43","slug":"calculating-primes-2","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=2302","title":{"rendered":"Calculating Primes &#8211; Part 1"},"content":{"rendered":"<p>Today I am going to talk about calculating primes.<\/p>\n<p>Not the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Transformers:_Dark_of_the_Moon\">Transformer movie<\/a> in which Optimus Prime has to help the humans save the world.<br \/>\nI am talking about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Prime_numbers\">prime numbers<\/a> in which the number is only divisable by one and itself.<\/p>\n<p>The Algorithm that I am going to introduce is a brute force method for calculating prime numbers.  It is great for comparing the computing power of two machines by looking at overall execution times.<\/p>\n<p>This routine consists in dividing n by each integer m which is greater than 1 and less than or equal to the square root of n. If the result of any of these divisions is an integer, then n is not a prime; otherwise, it is a prime.  <\/p>\n<p>Most of the Perl coding used standard, built-in functions.  <\/p>\n<p>One exception was the date calculation package (<a href=\"http:\/\/search.cpan.org\/~stbey\/Date-Calc-6.3\/lib\/Date\/Calc.pod\">Date::Calc<\/a>) that I down loaded from CPAN to calculate the total elapsed time of the program. There is a bug in the perl package <a href=\"http:\/\/www.perlmonks.org\/?node_id=434813\">installer <\/a>from Active State. Afer commenting out the offending line in web.pm, I used the command line \u2018ppm install Date::Calc\u2019 to add the package.<\/p>\n<p>I always like to create a high level program algorithm before starting any coding. I usually write it in psuedo code so that is program like in nature.<\/p>\n<p><span style=\"text-decoration: underline;\">Calculating Primes Algorithm:<\/span><\/p>\n<p><span style=\"text-decoration: underline;\">Calculating Primes Algorithm:<\/span><\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"pseudo code\">\r\nGet script arguement (N = max prospect)\r\nRecord start time\r\nWrite arguement N to log file\r\nWrite 2 &amp; 3 as first two primes to CSV result file\r\nARRAY A(1, 2) = (2, 3)\r\nSet found counter, CNT = 2\r\nFOR M = 4 to N\r\n  FOR EACH ARRAY A (P)\r\n      If P > SQRT(M), then prime, save to ARRAY A(MAX+1).\r\n      If M modulus P = zero, then non prime, exit for\r\n  NEXT P\r\n  Otherwise, save prime number to ARRAY A (MAX+1)\r\n  Write ARRAY A(MAX) to CSV result file as next prime number\r\n  Increment found counter, CNT++\r\nNEXT M\r\nWrite number of primes found, CNT, to log file\r\nRecord end time\r\nCalculate &amp; write time elapsed to Log file<\/pre>\n<p>The table below has the PERL script used in the solution and various supporting \/ output files. I ran the program to calculate all the prime numbers less than or equal to 2.5 Million. The program executed in 87 seconds on a Dell i5 Laptop running Windows 7 &#8211; 32 bit operating system and found 183,072 prime numbers.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"650\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/07\/prg-calc-primes-v1.pl_.txt'>prg-calc-primes-v1.pl<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Calculate Primes Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/07\/primes.cmd_.txt'>primes.cmd<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Primes Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/07\/primes.log_.txt'>primes.log<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Primes Log File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/07\/primes.csv'>primes.csv<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Primes Result File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/07\/dos-output-perl-primes-prgm.jpg\">image<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">DOS Output Screen<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I am going to talk about calculating primes. Not the Transformer movie in which Optimus Prime has to help the humans save the world. I am talking about prime numbers in which the number is only divisable by one and itself. The Algorithm that I am going to introduce is a brute force method for calculating prime numbers. It is great for comparing the computing power of two machines by looking at overall execution times. This routine consists in dividing n by each integer m which is greater than&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[441,479,12,15,443,18,478],"class_list":["post-2302","post","type-post","status-publish","format-standard","hentry","category-other","tag-cpan-perl-modules","tag-datecalc","tag-free-code","tag-john-f-miner-iii","tag-perl-script-2","tag-prime-numbers","tag-trail-division"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2302"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2302\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}