# Using Select on a 2d table with a criteria that accepts 3 parameters

I have a table

sets := Table[{i, j, k}, {i, 1, 10}, {j, 1, 10}, {k, 1, 10}]


and I want to use a function that accepts three parameters and returns a boolean, as a criteria in Select. The function looks like this:

isPytagorean[{a_, b_, c_}] := a^2 == b^2 + c^2


I tried a couple of things but none of them worked. This one returns an empty list:

Select[sets, isPytagorean]


How should I use Select to get a new table of tables for which the criteria isPytagorean is met?

• You'll want to Flatten[] sets first before trying Select[], for starters. But for your actual problem, FindInstance[] might be more useful. Dec 13 '15 at 17:11
• Cases[sets, _?(isPytagorean), -1] Dec 13 '15 at 18:26

sol = Position[Map[isPytagorean, sets, {3}], True]


{{5, 3, 4}, {5, 4, 3}, {10, 6, 8}, {10, 8, 6}}

rev = Reverse /@ Union[Sort /@ sol]


{{5, 4, 3}, {10, 8, 6}}

isPytagorean /@ rev


{True, True}

Update

The following is much faster:

Cases[PowersRepresentations[#, 2, 2] & /@ Range@100, {{0, _}, _}]


{{{0, 5}, {3, 4}}, {{0, 10}, {6, 8}}}

{a, b, c} /. Solve[
a^2 == b^2 + c^2 && 1 <= a <= 10 && 1 <= b <= 10 &&  1 <= c <= 10 && {a, b, c} ∈ Integers,
{a, b, c}
]

tup = Tuples[Range, 3];
can = Pick[tup, Sort[#].DiagonalMatrix[{1, 1, -1}].Sort[#] & /@ tup, 0]
Union[Sort /@ can]


yields: {{3, 4, 5}, {6, 8, 10}}