Created
April 1, 2024 18:57
-
-
Save orjanv/cb7f4a375a9d30942626cb2d937c99b8 to your computer and use it in GitHub Desktop.
Find perfect numbers using multiprocessing
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
import multiprocessing | |
def get_divisors_sum(n): | |
divisors_sum = 0 | |
for i in range(1, int(n / 2) + 1): | |
if n % i == 0: | |
divisors_sum += i | |
return divisors_sum | |
def parallel_perfect_numbers(numbers): | |
with multiprocessing.Pool() as pool: | |
results = pool.map(get_divisors_sum, numbers) | |
return results | |
if __name__ == "__main__": | |
number_list = [] | |
numbers = list(range(1, 1000)) | |
results = parallel_perfect_numbers(numbers) | |
for i, j in zip(numbers, results): | |
if i == j: | |
number_list.append(i) | |
print(number_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment