import multiprocessing from factorial import factorial def factorial_wrapper(args): """Unpack the tuple to pass to factorial().""" top, bottom = args return factorial(top, bottom) def parallel_factorial(worker_count, n): """Calculate the nth factorial using the specified number of workers.""" step = n / worker_count pairs = [(x, x - step + 1) for x in xrange(n, 1, -step)] pool = multiprocessing.Pool(worker_count) result = pool.map(factorial_wrapper, pairs) return reduce(int.__mul__, result) if __name__ == '__main__': import sys print parallel_factorial(int(sys.argv[1]), int(sys.argv[2]))