# How can I remove and keep the elements of this set?

I have a list

list = {{1, 3, 7, 1, 2, 1, 4, 7}, {6, 2, 7, 9, 4, 3, 6, 7}, {2, 8, 8, 2, 3, 2, 4, 6}, {5, 7, 7, 5, 2, 1, 2, 4}, {8, 10, 10, 8, 2, 1, 2, 4}}


Consider an element of list, with two item i and i+1. If all the pair (i, i+1) (i stand at odd position from left to right) so that GCD[i, i+1]==1, we keef this element. If there is a pair (i, i+1) so that GCD[i, i+1] different 1, then we remove that element.

With this rule, the list only keep the element {1, 3, 7, 1, 2, 1, 4, 7} How can I tell Mathematica to do that? I do not how to start.

Edit

According to the comment, we should divide the 8 element to 4 pairs.

Select[list, And @@ (GCD @@@ Partition[#, 2] == 1 // Thread) &]


Original

Select[list, And @@ (GCD @@@ Partition[#, 2, 1] == 1 // Thread) &]


{1, 3, 7, 1, 2, 1, 4, 7}.

• list = {{1, 3, 7, 1, 2, 1, 4, 7}, {6, 2, 7, 9, 4, 3, 6, 7}, {2, 8, 8, 2, 3, 2, 4, 6}, {5, 7, 7, 5, 2, 1, 2, 4}, {8, 10, 10, 8, 2, 1, 2, 4}, {4, 1, 7, 10, 5, 4, 6, 7} with that list, the element {4, 1, 7, 10, 5, 4, 6, 7} does not remove. Commented Jun 17, 2023 at 7:31
• I think Select[list, And @@ (GCD @@@ Partition[#, 2] == 1 // Thread) &] is correct. Commented Jun 17, 2023 at 7:37
f1 = BlockMap[If[CoprimeQ @@ #, Splice @ #, Nothing] &, #, 2] &;

f1 /@ list

 {{1, 3, 7, 1, 2, 1, 4, 7},
{7, 9, 4, 3, 6, 7},
{3, 2},
{5, 7, 7, 5, 2, 1},
{2, 1}}


Also

f2 = Flatten @ Select[Apply @ CoprimeQ] @ Partition[#, 2] &;

f2 /@ list


same result

Update: If you need to find the members of list that does not lose any elements under your selection rule, you can use f1 and f2

Select[ f1 @ # == # &] @ list

 {{1, 3, 7, 1, 2, 1, 4, 7}}

Select[ f2 @ # == # &] @ list

 {{1, 3, 7, 1, 2, 1, 4, 7}}


Alternatively, more directly

s1 = Select[And @@ BlockMap[Apply[CoprimeQ], #, 2] &];

s2 = Select[Apply[And]@MapApply[CoprimeQ]@Partition[#, 2] &];


An using (still-undocumented) 6-argument form of Partition as follows:

s3 = Select[And @@ Partition[#, 2, 2, 1, {}, CoprimeQ] &];


All three methods give the same result

# @ list & /@ {s1, s2, s3}

 {{{1, 3, 7, 1, 2, 1, 4, 7}},
{{1, 3, 7, 1, 2, 1, 4, 7}},
{{1, 3, 7, 1, 2, 1, 4, 7}}}


Using //. is also interesting.

If all the pair (i, i+1) (I stand at the odd position from left to right) so that GCD[i, i+1]==1, we keep this element.

keepQ = (# //. {x_,y_,z___} /; GCD[x,y] === 1 :> {z} // EqualTo[{}]) &


so

Select[keepQ][list]


{{1, 3, 7, 1, 2, 1, 4, 7}}

• Thank you very much. Commented Jun 21, 2023 at 4:53

Another way using Pick, Partition and AllTrue:

list1 = {{1, 3, 7, 1, 2, 1, 4, 7}, {6, 2, 7, 9, 4, 3, 6, 7},
{2, 8, 8,2, 3, 2, 4, 6}, {5, 7, 7, 5, 2, 1, 2, 4},
{8, 10, 10, 8, 2, 1, 2, 4}};

list2 = {{1, 3, 7, 1, 2, 1, 4, 7}, {6, 2, 7, 9, 4, 3, 6, 7},
{2, 8, 8,2, 3, 2, 4, 6}, {5, 7, 7, 5, 2, 1, 2, 4},
{8, 10, 10, 8, 2, 1, 2, 4}, {4, 1, 7, 10, 5, 4, 6, 7}};

Pick[#, AllTrue[Partition[#, {2}], GCD[#[[1]], #[[2]]] == 1 &] & /@ #] &@list1

(*{{1, 3, 7, 1, 2, 1, 4, 7}}*)

Pick[#, AllTrue[Partition[#, {2}], GCD[#[[1]], #[[2]]] == 1 &] & /@ #] &@list2

(*{{1, 3, 7, 1, 2, 1, 4, 7}, {4, 1, 7, 10, 5, 4, 6, 7}}*)


Using the lists of E. Chan-López

f[x_] /; AllTrue[# == 1 &] @ MapApply[GCD] @ Partition[x, 2] := x
f[_] := Nothing

f /@ list1


{{1, 3, 7, 1, 2, 1, 4, 7}}

f /@ list2


{{1, 3, 7, 1, 2, 1, 4, 7}, {4, 1, 7, 10, 5, 4, 6, 7}}