# Cases to identify the sublists, within the list that contain an integer and its square somewhere in the list

Use Cases to identify the sublists, within the list below, that contain an integer and its square somewhere in the list. (Hint: Use the BlankNullSequence and the named BlankSequence patterns with a Condition).

list={{6,3,2,4},{3,9,5},{1,7,2},{16,5,4},{49,7},{1,3,1},{3,2,7}};

While the earlier question (Cases to identify integers within a list) did help identify the list that contain an integer, but failed to figure out how BlankNullSequence and named BlankSequence patterns can identify the lists that contain an integer and its square somewhere in the list.

• Thanks for the accept. But you might want to wait more, as you might get better answers. Sep 11, 2021 at 10:02
• @Nasser It helps a lot despite someone putting other ways. Sep 11, 2021 at 10:03

You might find OrderlessPatternSequence useful for specifying your pattern:

Cases[{OrderlessPatternSequence[x_, y_, ___] /; y == x^2}] @ lis

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


May be

lis = {{6,3,2,4}, {3,9,5}, {1,7,2}, {16,5,4}, {49,7}, {1,3,1}, {3, 2, 7}};
Cases[lis, {___, x_, ___, y_, ___} /; (x^2 == y || y^2 == x) :> {x, y}]


gives

{{2, 4}, {3, 9}, {16, 4}, {49, 7}, {1, 1}}


The {1,1} is from {1, 3, 1} because 1 is the square of 1

However, the above does not capture all possible n and n^2 inside the list. For example given {6, 3, 2, 4, 16}, it finds {2,4} but not also {4,16} from same list.

I assumed your list has only one pair of n and n^2 in it.

Edit

Per comment, output the whole list which contains any $$n,n^2$$ in it

lis = {{6,3,2,4}, {3,9,5}, {1,7,2}, {16,5,4}, {49,7}, {1,3,1}, {3, 2, 7}};
Cases[lis, {a___, x_, c___, y_, b___} /; (x^2 == y || y^2 == x) :> {a,x,c,y,b}]


gives

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

• Any suggestion on how to have the output in terms of the content of the list. For instance, {6,3,2,4} as one of the output because 2,4 included. My best guess is instead of {__, x, __, y, ___}, there will be some wildcard character for ___. Sep 11, 2021 at 10:07
• @SplendidDigitalSolutions updated, You can use any letter to capture the pattern. I am sure there are many other ways to do this. Sep 11, 2021 at 10:14
list =
{{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4}, {49, 7}, {1, 3, 1}, {3, 2, 7}};


Using SubsetCases (new in 12.1)

sc = SubsetCases[#, {x_, y_} /; y == x^2] & /@ list


{{{2, 4}}, {{3, 9}}, {}, {{4, 16}}, {{7, 49}}, {{1, 1}}, {}}

1. pairs

Join @@ sc


{{2, 4}, {3, 9}, {4, 16}, {7, 49}, {1, 1}}

2. sublists

p = Cases[MapIndexed[List, sc], {{__}, a_} :> a]


{{1}, {2}, {4}, {5}, {6}}

Extract[list, p]


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

A variant using MemberQ and TakeList:

list = {{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4}, {49, 7}, {1,
3, 1}, {3, 2, 7}};

f[k_List] :=
TakeList[k, {{#}, All}] & /@ Range@Length@k //
Map[MemberQ[Last@#, (First@First@#)^2] &] // ContainsAny[{True}]

Pick[list, f /@ list]

Select[list, f]


Result:

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

list = {{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4},
{49, 7}, {1, 3, 1}, {3, 2, 7}};


Using Pick:

test = ! FreeQ[#2 == #1^2 & @@@ Union[Sort /@ Permutations[#, {2}]], True] &;

Pick[#, test /@ #] &@list

(*{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}*)