How to resolve the algorithm Parallel calculations step by step in the Racket programming language
How to resolve the algorithm Parallel calculations step by step in the Racket programming language
Table of Contents
Problem Statement
Many programming languages allow you to specify computations to be run in parallel. While Concurrent computing is focused on concurrency, the purpose of this task is to distribute time-consuming calculations on as many CPUs as possible. Assume we have a collection of numbers, and want to find the one with the largest minimal prime factor (that is, the one that contains relatively large factors). To speed up the search, the factorization should be done in parallel using separate threads or processes, to take advantage of multi-core CPUs. Show how this can be formulated in your language. Parallelize the factorization of those numbers, then search the returned list of numbers and factors for the largest minimal factor, and return that number and its prime factors. For the prime number decomposition you may use the solution of the Prime decomposition task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Parallel calculations step by step in the Racket programming language
Source code in the racket programming language
#lang racket
(require math)
(provide main)
(define (smallest-factor n)
(list (first (first (factorize n))) n))
(define numbers
'(112272537195293 112582718962171 112272537095293
115280098190773 115797840077099 1099726829285419))
(define (main)
; create as many instances of Racket as
; there are numbers:
(define ps
(for/list ([_ numbers])
(place ch
(place-channel-put
ch
(smallest-factor
(place-channel-get ch))))))
; send the numbers to the instances:
(map place-channel-put ps numbers)
; get the results and find the maximum:
(argmax first (map place-channel-get ps)))
> (main)
'(544651 115797840077099)
You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Openscad programming language
You may also check:How to resolve the algorithm Quoting constructs step by step in the Java programming language
You may also check:How to resolve the algorithm Number names step by step in the J programming language
You may also check:How to resolve the algorithm String append step by step in the Delphi programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Insitux programming language