Last active
December 17, 2023 16:39
-
-
Save abidkhan484/21292da8de0ef0217f3d0ac330e0e3a0 to your computer and use it in GitHub Desktop.
Prime check using multi thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import concurrent.futures | |
from math import floor, sqrt | |
import time | |
def is_prime(number: int) -> bool: | |
if not (number & 1): return 0 | |
iteration = floor(sqrt(number)) + 1 | |
for i in range(3, iteration, 2): | |
if not (number % i): return 0 | |
return 1 | |
def main() -> int: | |
h_range = 100_000 | |
start_time = time.time() | |
max_workers = 10 | |
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: | |
futures = [executor.submit(is_prime, number) for number in range(h_range + 1)] | |
total_prime = 0 | |
for prime_count in concurrent.futures.as_completed(futures): | |
try: | |
total_prime += prime_count.result() | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
end_time = time.time() | |
print(f"Total execution time is {end_time - start_time} \ | |
where count of primes is {total_prime}") | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment