First 30 Million Prime Numbers

After creating some code, I’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.

Here’s my code:


from timeit import default_timer
start = default_timer()
import math
text_file = open("10,000,000 to 20,000,000.txt", "w")
text_file1 = open("10,000,000 to 20,000,000 with rowNumbers.txt", "w")
text_file2 = open("20,000,000 to 30,000,000.txt", "w")
text_file3 = open("20,000,000 to 30,000,000 with rowNumbers.txt", "w")
text_output = open("Output.txt", "w")
text_output.write("Started: "+ str(default_timer() - start) + " seconds\n")

primes = [2, 3]
print("This file was made for cornerspots.com\n1. 2\n2. 3")
text_file.write("This file was made for cornerspots.com\n10,000,000th prime to 20,000,000th prime\n")
text_file1.write("This file was made for cornerspots.com\n10,000,000th prime to 20,000,000th prime\n")
text_file2.write("This file was made for cornerspots.com\n10,000,000th prime to 20,000,000th prime\n")
text_file3.write("This file was made for cornerspots.com\n10,000,000th prime to 20,000,000th prime\n")
number = 1
rowNumber = 2

while rowNumber < 30000000: #30,000,000
    isPrime = True
    number += 2

    for prime in primes:
        # if number is bigger than the sqrt of num we're checking
        if prime > math.sqrt(number):
            break
        # is not a prime
        if number % prime == 0:
            isPrime = False
            break

    if isPrime:
        rowNumber += 1
        primes.append(number)
        print(str(rowNumber) + ". " + str(number))
        if (rowNumber >= 10000000 and rowNumber <= 20000000):
            text_file.write(str(number) + "\n")
            text_file1.write(str(rowNumber) + ". " + str(number) + "\n")

        if (rowNumber >= 20000000 and rowNumber <= 30000000):
            text_file2.write(str(number) + "\n")
            text_file3.write(str(rowNumber) + ". " + str(number) + "\n")
            
text_file.close()
text_file1.close()
text_file2.close()
text_file3.close()
text_output.write("Ended: " + str(default_timer() - start) + "seconds")

 

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
If 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.
If 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.

10MillionthPrime_To_20MillionthPrime_withoutrowNumbers

10MillionthPrime_To_20MillionthPrime_withRowNumbers

20MillionthPrime_To_30MillionthPrime_withoutRowNumbers

20MillionthPrime_To_30MillionthPrime_withRowNumbers

 

 

Leave a Comment