-1
$\begingroup$

Yes, just define the function. i.e. Use the Fibonacci numbers. What are good practices to perform it? (Vague, let's get specific.)

  1. Create list of Fib.s 0, to 1,000,000.
  2. Make function _Integer parameter, name fibonacciQ.
  3. Error trap with message if < 0, or > million.

Is that how, to do it? My real GOAL is any {x,y,z} of Pythagorean Triples (not {3,4,5}, or {3,10}, or {3,4,4,5} eg.), only one value tested per call, create a predicate function pythagoreanQ[ ] for them. Likely, load table from disk, when needed in a notebook. Is that my only practical alternative, unless computing pythags 3 .. 1,000,000 directly every time?

Thanks for answering.

$\endgroup$
4
  • 4
    $\begingroup$ fibQ = TrueQ[# == Fibonacci@Round[Log[GoldenRatio, Sqrt[5] #]]] &? pythagQ = TrueQ[#1^2 + #2^2 == #3^2] & or pythagQ = TrueQ[{1, 1, 1} . #^2 == 0] &? $\endgroup$
    – Michael E2
    Commented Dec 8, 2021 at 4:04
  • 1
    $\begingroup$ "not {3,4,5}, or {3,10}, or {3,4,4,5}" -- does that mean that pythagoreanQ[{3,4,5}] should return False? "Select[ {3,13, 100, 17} returns {True, True, True, True}" seems to make no sense since {3,13,100,17} is not a triple at all. Also Select[] is a built-in function that does not return a list of True unless the input contained True. Do you mean pQ[n] should return True if n is a member of any Pythagorean triple? $\endgroup$
    – Michael E2
    Commented Dec 8, 2021 at 14:29
  • 1
    $\begingroup$ Last part yes, "pQ[n] should return True if n is a member of any Pythagorean triple? pQ[x,y,z] is an invalid form, just as pQ[x,z] would be. ** A simple Select[ data, pQ[ #] &] is how it will be called. $\endgroup$
    – prog9910
    Commented Dec 8, 2021 at 16:46
  • 1
    $\begingroup$ You should note that if $n$ is an integer greater than $2$, there always exist positive integers $x,y,z$ such that $x^2+y^2=z^2$ and $x=n$. $\endgroup$
    – Somos
    Commented Dec 8, 2021 at 18:08

1 Answer 1

4
$\begingroup$
PythagoreanQ[{x_Integer, y_Integer, z_Integer}] := 
  With[{forbiddenQ = (# < 0) &},
   If[AnyTrue[{x, y, z}, forbiddenQ], 
    Return@Failure[
      "PythagoreanQ::negativeInput", {"MessageTemplate" -> 
        "One or more supplied integers is negative.", 
       "MessageParameters" -> {}}]];
   2*Max[{x, y, z}]^2 == Plus @@ (#^2 &) /@ {x, y, z}
  ];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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