{"id":617,"date":"2017-01-29T20:37:36","date_gmt":"2017-01-30T04:37:36","guid":{"rendered":"http:\/\/www.cornerspots.com\/sirknightj\/?p=617"},"modified":"2017-06-26T08:15:34","modified_gmt":"2017-06-26T16:15:34","slug":"first30millionprimenumbers","status":"publish","type":"post","link":"https:\/\/www.sirknightj.com\/blog\/2017\/01\/29\/first30millionprimenumbers\/","title":{"rendered":"First 30 Million Prime Numbers"},"content":{"rendered":"<p>After creating some code, I&#8217;ve finally got my computer to calculate the first 30 Million Prime Numbers. It only took 8.3 hours, according to the timer I set up.<\/p>\n<p><!--more--><\/p>\n<p>Here&#8217;s my code:<\/p>\n<pre><code>\r\nfrom timeit import default_timer\r\nstart = default_timer()\r\nimport math\r\ntext_file = open(\"10,000,000 to 20,000,000.txt\", \"w\")\r\ntext_file1 = open(\"10,000,000 to 20,000,000 with rowNumbers.txt\", \"w\")\r\ntext_file2 = open(\"20,000,000 to 30,000,000.txt\", \"w\")\r\ntext_file3 = open(\"20,000,000 to 30,000,000 with rowNumbers.txt\", \"w\")\r\ntext_output = open(\"Output.txt\", \"w\")\r\ntext_output.write(\"Started: \"+ str(default_timer() - start) + \" seconds\\n\")\r\n\r\nprimes = [2, 3]\r\nprint(\"This file was made for cornerspots.com\\n1. 2\\n2. 3\")\r\ntext_file.write(\"This file was made for cornerspots.com\\n10,000,000th prime to 20,000,000th prime\\n\")\r\ntext_file1.write(\"This file was made for cornerspots.com\\n10,000,000th prime to 20,000,000th prime\\n\")\r\ntext_file2.write(\"This file was made for cornerspots.com\\n10,000,000th prime to 20,000,000th prime\\n\")\r\ntext_file3.write(\"This file was made for cornerspots.com\\n10,000,000th prime to 20,000,000th prime\\n\")\r\nnumber = 1\r\nrowNumber = 2\r\n\r\nwhile rowNumber < 30000000: #30,000,000\r\n    isPrime = True\r\n    number += 2\r\n\r\n    for prime in primes:\r\n        # if number is bigger than the sqrt of num we're checking\r\n        if prime > math.sqrt(number):\r\n            break\r\n        # is not a prime\r\n        if number % prime == 0:\r\n            isPrime = False\r\n            break\r\n\r\n    if isPrime:\r\n        rowNumber += 1\r\n        primes.append(number)\r\n        print(str(rowNumber) + \". \" + str(number))\r\n        if (rowNumber >= 10000000 and rowNumber <= 20000000):\r\n            text_file.write(str(number) + \"\\n\")\r\n            text_file1.write(str(rowNumber) + \". \" + str(number) + \"\\n\")\r\n\r\n        if (rowNumber >= 20000000 and rowNumber <= 30000000):\r\n            text_file2.write(str(number) + \"\\n\")\r\n            text_file3.write(str(rowNumber) + \". \" + str(number) + \"\\n\")\r\n            \r\ntext_file.close()\r\ntext_file1.close()\r\ntext_file2.close()\r\ntext_file3.close()\r\ntext_output.write(\"Ended: \" + str(default_timer() - start) + \"seconds\")<\/pre>\n<p><\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Now I know that there was probably an easier way to do this, but I decided to split up the files into 10 Million each because I noticed that notepad was starting to get a little laggy when I<br \/>\nIf you're looking for the first 10 Million, look at the post right before this one. Also, if you use notepad++ to open the files, then there is almost no lag.<br \/>\nIf you try to open this with normal notepad, you have to wait around 1 minute for the whole thing to load. If notepad says it's not responding, just wait. It will open eventually.<\/p>\n<p><a href=\"http:\/\/www.sirknightj.com\/blog\/wp-content\/uploads\/2017\/01\/10MillionthPrime_To_20MillionthPrime_withoutrowNumbers.zip\">10MillionthPrime_To_20MillionthPrime_withoutrowNumbers<\/a><\/p>\n<p><a href=\"http:\/\/www.sirknightj.com\/blog\/wp-content\/uploads\/2017\/01\/10MillionthPrime_To_20MillionthPrime_withRowNumbers.zip\">10MillionthPrime_To_20MillionthPrime_withRowNumbers<\/a><\/p>\n<p><a href=\"http:\/\/www.sirknightj.com\/blog\/wp-content\/uploads\/2017\/01\/20MillionthPrime_To_30MillionthPrime_withoutRowNumbers.zip\">20MillionthPrime_To_30MillionthPrime_withoutRowNumbers<\/a><\/p>\n<p><a href=\"http:\/\/www.sirknightj.com\/blog\/wp-content\/uploads\/2017\/01\/20MillionthPrime_To_30MillionthPrime_withRowNumbers.zip\">20MillionthPrime_To_30MillionthPrime_withRowNumbers<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After creating some code, I&#8217;ve finally got my computer to calculate the first 30 Million Prime Numbers. It only took 8.3 hours, according to the timer I set up.<\/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\/617"}],"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=617"}],"version-history":[{"count":6,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":717,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions\/717"}],"wp:attachment":[{"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sirknightj.com\/blog\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}