{"id":233,"date":"2011-06-01T15:23:32","date_gmt":"2011-06-01T15:23:32","guid":{"rendered":"http:\/\/craftydba.com\/?p=233"},"modified":"2024-02-17T14:26:34","modified_gmt":"2024-02-17T14:26:34","slug":"calculating-primes","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=233","title":{"rendered":"Calculating Primes"},"content":{"rendered":"<p>Today I am going to talk about calculating primes.<\/p>\n<p>Not the new <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.<br \/>\nIt 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<br \/>\nto the square root of n. If the result of any of these divisions is an integer, then n is not a prime;<br \/>\notherwise, it is a prime.<\/p>\n<p>I am going to use both the &#8220;Application Logging&#8221; and &#8220;Write Text File&#8221; classes that were discussed earlier to solve this problem.<\/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<pre class=\"lang:TSQL theme:epicgeeks\" title=\"pseudo code\">\r\n\r\nGet script arguement (N-max prospect)\r\nRecord\u00a0start time\r\nWrite arguement N to log file\r\nWrite 2 & 3 as first two primes to CSV result file\r\nSet found counter = 2\r\n\r\nFor M = 4 to N\r\n\u00a0 For O = 2 to SQRT(M)\r\n\u00a0\u00a0\u00a0 If M modulus O = zero, then non prime, exit for\r\n\u00a0 NEXT O\r\n\u00a0 Otherwise, write prime number\u00a0to CSV result file\r\n\u00a0 Increment found counter\r\nNEXT M\r\n\r\nWrite number of primes found to log file\r\nRecord end time\r\nCalculate & write time elapsed to Log file\r\n<\/pre>\n<p>\u00a0<\/p>\n<p>The table below has the Visual Basic (VB) Scripts used in the solution and the output files. I ran the program to calculate all the prime numbers less than or equal to 2.5 Million. The program executed in 91 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=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/mod-write-text-file.vbs_.txt\">mod-write-text-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Text File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/mod-app-logging.vbs_1.txt\">mod-app-logging.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Application Logging Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/prg-calc-primes-v1.vbs_.txt\">prg-calc-primes-v1.vbs<\/a><\/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\/2011\/06\/primes.cmd_.txt\">primes.cmd<\/a><\/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\/2011\/06\/prg-calc-primes-v1.log_.txt\">prg-calc-primes-v1.log<\/a><\/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\/2011\/06\/primes.csv\">primes<\/a><\/td>\n<td style=\"border: thin solid gray;\">Primes Result File<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\nThis is a screen shot of the calculating primes VB Script excuting in a MS DOS command shell.<br \/>\n<\/P><\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/primes-screen-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-257\" title=\"primes-screen-output\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/primes-screen-output-300x173.jpg\" alt=\"\" width=\"300\" height=\"173\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/primes-screen-output-300x173.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/primes-screen-output.jpg 675w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\nAs you can see, the second time I ran the program it took 95 seconds. Just to show you that background processes like email, word, and etc. can have sligth performance impacts of heavy computational programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I am going to talk about calculating primes. Not the new 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&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":[12,15,18,13],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-other","tag-free-code","tag-john-f-miner-iii","tag-prime-numbers","tag-vb-script-2"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/233","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=233"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}