First One Million Prime Numbers

Recently, I’ve been learning python.

I’ve made a program to find the first 1,000,000 Prime Numbers.

Here’s the zip file: FirstOneMillionPrimeNumbers

And here’s the code I created:


import math
text_file = open("Output.txt", "w")

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\n1. 2\n")
number = 1
rowNumber = 2

while rowNumber < 10000000:
    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
        text_file.write(str(rowNumber) + ". " + str(number) + "\n")
        primes.append(number)
        print(str(rowNumber) + ". " + str(number))

text_file.close()

 

This takes roughly 30-40 minutes to complete on my surface 3, but only 5 minutes on my PC.

And in case you’re wondering, the one millionth prime number is 15,485,857.

 

 

Leave a Comment