I'm quite new to this. I have a nested list that represent solutions to an equation. I would like to create a new list that only contains one 'representative' of each element. As an example, imagine I had a random list of integers less than 20, and I wanted to make a new list such if an element in the original list has a factor in the list, that element would be excluded. Would look something like:

Original list: {1, 2, 5, 10, 11, 12, 15, 16, 19}

New list: {1, 2, 5, 11, 19}

I'd like to stress that this is not exactly what I'm trying to do, so suggestions that involve looking for prime numbers will not be helpful. What I need is to run a test over elements of a list, and the result of this test depends on all elements of the list.

I have this right now:

 JJ = {};
 Clear[a, b, c];

 a = sol[[i, 1]];
 b = sol[[i, 2]];
 c = sol[[i, 3]];
 m = {{a, b}, {b, c}};

  Clear[alpha, beta, gamma, n, k, kt, x, y, u, v];

  alpha = sol[[j, 1]];
  beta = sol[[j, 2]];
  gamma = sol[[j, 3]];

  n = {{alpha, beta}, {beta, gamma}};
  k = {{x, y}, {u, v}};
  kt = {{x, u}, {y, v}};
  If[Solve[{m == k.n.kt, x*v - y*u == 1}, {x, y, u, v}, 
     Integers] == {}, AppendTo[JJ, {a, b, c}]];

    , {j, 1, Length[sol]}]

 , {i, 1, Length[sol]}]

Above, the list sol is my originl list, where JJ is the new list I am trying to make such that JJ only contains 'unique representatives' from sol. It is not working how I thought it would. Any help/advice/info would be very much appreciated. Thanks!

  • 2
    $\begingroup$ DeleteDuplicates[Flatten[(FactorInteger /@ list), 1][[All, 1]]] or more generally Select[list,test]. $\endgroup$ – David G. Stork Mar 29 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.