Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,18 +1,22 @@
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.
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.
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.
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.
For the prime number decomposition
you may use the solution of the [[Prime decomposition]] task.
{{omit from|J}}

View file

@ -0,0 +1,18 @@
USING: io kernel fry locals sequences arrays math.primes.factors math.parser channels threads prettyprint ;
IN: <filename>
:: map-parallel ( seq quot -- newseq )
<channel> :> ch
seq [ '[ _ quot call ch to ] "factors" spawn ] { } map-as
dup length [ ch from ] replicate nip ;
{ 576460752303423487 576460752303423487
576460752303423487 112272537195293
115284584522153 115280098190773
115797840077099 112582718962171
112272537095293 1099726829285419 }
dup [ factors ] map-parallel
dup [ infimum ] map dup supremum
swap index swap dupd nth -rot swap nth
"Number with largest min. factor is " swap number>string append
", with factors: " append write .

View file

@ -0,0 +1,7 @@
USING: kernel io prettyprint sequences arrays math.primes.factors math.parser concurrency.combinators ;
{ 576460752303423487 576460752303423487 576460752303423487 112272537195293
115284584522153 115280098190773 115797840077099 112582718962171 }
dup [ factors ] parallel-map dup [ infimum ] map dup supremum
swap index swap dupd nth -rot swap nth
"Number with largest min. factor is " swap number>string append
", with factors: " append write .

View file

@ -1,4 +1,4 @@
mport Control.Parallel.Strategies
import Control.Parallel.Strategies
import Control.DeepSeq
import Data.List
import Data.Function

View file

@ -0,0 +1,40 @@
import static java.lang.System.out;
import static java.util.Arrays.stream;
import static java.util.Comparator.comparing;
public interface ParallelCalculations {
public static final long[] NUMBERS = {
12757923,
12878611,
12878893,
12757923,
15808973,
15780709,
197622519
};
public static void main(String... arguments) {
stream(NUMBERS)
.unordered()
.parallel()
.mapToObj(ParallelCalculations::minimalPrimeFactor)
.max(comparing(a -> a[0]))
.ifPresent(res -> out.printf(
"%d has the largest minimum prime factor: %d%n",
res[1],
res[0]
))
;
}
public static long[] minimalPrimeFactor(long n) {
return iterate(2, i -> i + 1)
.filter(i -> n >= i * i)
.filter(i -> n % i == 0)
.mapToObj(i -> new long[]{i, n})
.findFirst()
.orElseGet(() -> new long[]{n, n})
;
}
}

View file

@ -35,11 +35,11 @@ def prime_factors_of_number_with_lowest_prime_factor(NUMBERS):
def main():
print( 'For these numbers:\n ' + '\n '.join(str(p) for p in NUMBERS) )
print('For these numbers:')
print('\n '.join(str(p) for p in NUMBERS))
number, all_factors = prime_factors_of_number_with_lowest_prime_factor(NUMBERS)
print(' The one with the largest minimum prime factor is %i:' % number)
print(' All its prime factors in order are: %s' % all_factors)
print(' The one with the largest minimum prime factor is {}:'.format(number))
print(' All its prime factors in order are: {}'.format(all_factors))
if __name__ == '__main__':
main()

View file

@ -0,0 +1,46 @@
import multiprocessing
# ========== #Python3 - concurrent
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
# ========== #Python3 - concurrent
def prime_factors_of_number_with_lowest_prime_factor(numbers):
pool = multiprocessing.Pool(processes=5)
factors = pool.map(lowest_factor,numbers)
low_factor,number = max((l,f) for l,f in zip(factors,numbers))
all_factors = prime_factors(number,low_factor)
return number,all_factors
if __name__ == '__main__':
print('For these numbers:')
print('\n '.join(str(p) for p in numbers))
number, all_factors = prime_factors_of_number_with_lowest_prime_factor(numbers)
print(' The one with the largest minimum prime factor is {}:'.format(number))
print(' All its prime factors in order are: {}'.format(all_factors))