I wanted to try and learn multi-threading, so I thought it would be good to make a small program normally first and make it multi-threaded, so I made this prime finder.
from time import sleep
def prime_finder(num: int) -> bool:
"""Checks to see if a number is prime"""
if num == 2:
return True
divider = 2
while True:
if (num % divider) == 0:
return False
elif (divider + 1) >= (
num / divider): # I don't know how to explain this, but it prevents checking multiples twice
return True
divider += 1
num = 1
pause = 0 # A delay that makes each prime number more readable
print(f"The prime numbers starting from {num} are:")
while True:
if prime_finder(num):
print(num, end=", ")
sleep(pause)
num += 1
Before I added the multi-threading I wanted to make sure that there was nothing fundamentally wrong with it. As well I wanted it to be pretty quick from the start, and so any optimization (other than multi-threading obviously) would be appreciated. I also wanted it to be easy to build on, and so if there are any ways to make it easier to add stuff, specifically multi-threading, please let me know.
num
, you can just look at the smaller primes. You also appear to be building up a new list of lower primes for every number investigated, which is quite the overhead compared to just keeping track of the previous ones. [1]: en.wikipedia.org/wiki/Sieve_of_Eratosthenes \$\endgroup\$