6
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ You'll want to Flatten[] sets first before trying Select[], for starters. But for your actual problem, FindInstance[] might be more useful. $\endgroup$ – J. M. is away Dec 13 '15 at 17:11
  • $\begingroup$ Cases[sets, _?(isPytagorean), -1] $\endgroup$ – Algohi Dec 13 '15 at 18:26
5
$\begingroup$
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}}}

$\endgroup$
1
$\begingroup$
{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}
  ]
$\endgroup$
1
$\begingroup$
tup = Tuples[Range[10], 3];
can = Pick[tup, Sort[#].DiagonalMatrix[{1, 1, -1}].Sort[#] & /@ tup, 0]
Union[Sort /@ can]

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

$\endgroup$

Your Answer

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

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