# function with parallel iterations

coprimesParallelIter[n_] := DistributeDefinitions[GCD];
ClearAll;
ParallelTable[If[GCD[i, n] == 1, i, Nothing], {i, 1, n - 1}] //
Flatten // DeleteCases[Nothing]


That function generates the list of coprimes less than n, but how can this list of coprimes be stored? When I call the function there is no return value.

• Your code doesn't align with your question, so this is confusing. You haven't defined n, so maybe that's why it's failing. Also, you don't actually use coprimesParallelIter anywhere, so it's not clear why that is included in your sample code. Are you sure you posted what you intended to post? Commented Jan 28, 2023 at 21:43

Clear["Global*"]


Use a CompoundExpression and store the results in a symbol (note that Nothing evaporates on its own)

coprimesParallelIter[n_] := (
DistributeDefinitions[GCD];
ParallelTable[
If[GCD[i, n] == 1, i, Nothing],
{i, 1, n - 1}])

list = coprimesParallelIter[20]

(* {1, 3, 7, 9, 11, 13, 17, 19} *)

coprimesParallelIter2[n_] := (
DistributeDefinitions[GCD];
ParallelTable[
If[CoprimeQ[n, i], i, Nothing],
{i, 1, n - 1}])

list === coprimesParallelIter2[20]

(* True *)
`
• oh, heh, the OP intended the whole thing to be the definition. Nice catch! Commented Jan 28, 2023 at 21:47