RosettaCodeData/Task/Parallel-calculations/Python/parallel-calculations-1.py

46 lines
1.3 KiB
Python
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
from concurrent import futures
from math import floor, sqrt
NUMBERS = [
112272537195293,
112582718962171,
112272537095293,
115280098190773,
115797840077099,
1099726829285419]
# NUMBERS = [33, 44, 55, 275]
def lowest_factor(n, _start=3):
if n % 2 == 0:
return 2
search_max = int(floor(sqrt(n))) + 1
for i in range(_start, search_max, 2):
if n % i == 0:
return i
return n
def prime_factors(n, lowest):
pf = []
while n > 1:
pf.append(lowest)
n //= lowest
lowest = lowest_factor(n, max(lowest, 3))
return pf
def prime_factors_of_number_with_lowest_prime_factor(NUMBERS):
with futures.ProcessPoolExecutor() as executor:
low_factor, number = max( (l, f) for l, f in zip(executor.map(lowest_factor, NUMBERS), NUMBERS) )
all_factors = prime_factors(number, low_factor)
return number, all_factors
def main():
2015-02-20 00:35:01 -05:00
print('For these numbers:')
print('\n '.join(str(p) for p in NUMBERS))
2013-04-10 23:57:08 -07:00
number, all_factors = prime_factors_of_number_with_lowest_prime_factor(NUMBERS)
2015-02-20 00:35:01 -05:00
print(' The one with the largest minimum prime factor is {}:'.format(number))
print(' All its prime factors in order are: {}'.format(all_factors))
2013-04-10 23:57:08 -07:00
if __name__ == '__main__':
main()