Parallel calculations: Difference between revisions

Content added Content deleted
m (Changed one indentation.)
Line 1,228: Line 1,228:


=={{header|Nim}}==
=={{header|Nim}}==
===Using threads===
We use one thread per number to process. To find the lowest prime factor, we use a simple algorithm as performance is not important here.
We use one thread per number to process. To find the lowest prime factor, we use a simple algorithm as performance is not important here.


Line 1,300: Line 1,301:
The first number with the largest minimum prime factor is: 115797840077099
The first number with the largest minimum prime factor is: 115797840077099
Its factors are: 544651, 212609249</pre>
Its factors are: 544651, 212609249</pre>

===Using parallel statement===
The parallel statement is an experimental feature. It uses threads but may simplify slightly the code in comparison to manual management.
Note that program must be compiled with option <code>--threads:on</code>.

<lang Nim>import sequtils, strutils, threadpool

{.experimental: "parallel".}

const Numbers = [576460752303423487,
576460752303423487,
576460752303423487,
112272537195293,
115284584522153,
115280098190773,
115797840077099,
112582718962171,
299866111963290359]


proc lowestFactor(n: int64): int64 =
if n mod 2 == 0: return 2
if n mod 3 == 0: return 3
var p = 5
var delta = 2
while p * p < n:
if n mod p == 0: return p
inc p, delta
delta = 6 - delta
result = n


proc factors(n, lowest: int64): seq[int64] =
var n = n
var lowest = lowest
while true:
result.add lowest
n = n div lowest
if n == 1: break
lowest = lowestFactor(n)


# Launch the threads.
var results: array[Numbers.len, int64] # To store the results.
parallel:
for i, n in Numbers:
results[i] = spawn lowestFactor(n)

# Find the minimum prime factor and the first number with this minimum factor.
let maxIdx = results.maxIndex()
let maxMinfact = results[maxIdx]
let result = Numbers[maxIdx]

echo ""
echo "The first number with the largest minimum prime factor is: ", result
echo "Its factors are: ", result.factors(maxMinfact).join(", ")</lang>

{{out}}
Output is the same as with manual management of threads.


=={{header|Oforth}}==
=={{header|Oforth}}==