1

I’m looking for ways I can speed up my short python code as it is meant to take the least amount of time to find the primes under a certain number.

import time
import sympy
import math
def RefinedPrimeFinder(n):
    Primes.append(2)
    for i in range(3,n,2):
        Prime = True
        sqrt = round(i**0.5)
        for b in Primes:
            if b > sqrt:
                break
            elif i % b == 0:
                Prime = False
                break
        if Prime:
            Primes.append(i)
range = 5000000
Primes = []
t_end = time.time()
RefinedPrimeFinder(range)
time = time.time() -t_end
print("It checked", max(Primes),"Last")
print("The length is",len(Primes))
print("The length is meant to be around", sympy.primepi(max(Primes)))
print("It took",time,"seconds")
print("\n")

0 Answers0