0
$\begingroup$
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.

$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – lericr
    Commented Jan 28, 2023 at 21:43

1 Answer 1

1
$\begingroup$
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 *)
$\endgroup$
1
  • $\begingroup$ oh, heh, the OP intended the whole thing to be the definition. Nice catch! $\endgroup$
    – lericr
    Commented Jan 28, 2023 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.