{"id":642,"date":"2017-03-01T21:29:36","date_gmt":"2017-03-02T05:29:36","guid":{"rendered":"http:\/\/www.cornerspots.com\/sirknightj\/?p=642"},"modified":"2017-07-06T11:39:21","modified_gmt":"2017-07-06T19:39:21","slug":"very-fast-prime-number-finder-code-python","status":"publish","type":"post","link":"https:\/\/www.sirknightj.com\/blog\/2017\/03\/01\/very-fast-prime-number-finder-code-python\/","title":{"rendered":"Very FAST Prime Number Finder Code (python)"},"content":{"rendered":"<p>I noticed that my previous code was slow, since the print command is VERY slow.<br \/>\nIt slows python down quite a bit, so I made it print less frequently.<br \/>\nIt does the same thing, though.<\/p>\n<p><!--more--><\/p>\n<pre>from timeit import default_timer as timer\r\nimport timeit\r\nprint(\"Starting Calculation...\")\r\nstart = timer()\r\nprimeList = [2]\r\ntext_file = open(\"Primes.txt\", \"w\")\r\ntext_other_file = open(\"PrimesLists.txt\", \"w\")\r\nmax = 100000000 # checks the integers between two and max\r\ninterval = 50000 # gives timer notification at every interval in seconds\r\n\r\nprevStart = 0 \r\n\r\nfor x in range(3, max + 1, 2):\r\n    isPrime = True\r\n    if (x % interval) - 1 == 0:\r\n        time = timer() - start\r\n        print(\"{:,d}\".format(x - 1) + \" : \" + str(\"{0:.2f}\".format(time)) + \" seconds elapsed, or \" + str(\"{0:.2f}\".format(time \/ 60)) + \" minutes elapsed, \" + str(\"{0:.2f}\".format(timer() - prevStart)) + \" second gap\")\r\n        prevStart = timer()\r\n    for y in primeList:\r\n        if y &gt; (x**0.5):\r\n            break\r\n        if x % y == 0:\r\n            isPrime = False\r\n            break\r\n    if isPrime:\r\n        primeList.append(x)\r\n\r\nend = timer()\r\nprint(\"Done Calculating All The Primes From 2 Through \" + str(max))\r\nprint(\"Completed in \" + str(end - start) + \" seconds\")\r\n\r\nfor z in range(0, len(primeList) - 1):\r\n    text_file.write(str(primeList[z]) + \"\\n\")\r\n    text_other_file.write(str(z + 1) + \". \" + str(primeList[z]) + \"\\n\")\r\nprint(\"Successfully written to text file\")\r\nprint(\"Completed in \" + str(timer() - end) + \" seconds\")\r\ntext_file.close()\r\ntext_other_file.close()<\/pre>\n<p>Hope you like it!<br \/>\nHere&#8217;s the prime numbers between 2 and 100,000,000.<br \/>\n<a href=\"http:\/\/www.sirknightj.com\/blog\/wp-content\/uploads\/2017\/03\/PrimesFromTwoThrough100MillionCornerspots.zip\">PrimesFromTwoThrough100MillionCornerspots<\/a><br \/>\nNote: If notepad will open the file, it just takes around 10 seconds to open. It will say not responding, but just wait and it will open.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I noticed that my previous code was slow, since the print command is VERY slow. It slows python down quite a bit, so I made it print less frequently. It does the same thing, though.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/642"}],"collection":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":9,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/642\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}