50
\$\begingroup\$

Given a, b, c the length of the three sides of a triangle, say if the triangle is right-angled (i.e. has one angle equal to 90 degrees) or not.

Input

Three positive integer values in any order

Output

Either a specific true output (true, 1, yes, ...) or a specific false output (false, 0, no, ...)

Example

5, 3, 4        --> yes
3, 5, 4        --> yes
12, 37, 35     --> yes
21, 38, 50     --> no
210, 308, 250  --> no

Rules

  • The input and output can be given in any convenient format.
  • In your submission, please state the true and the false values.
  • No need to handle negative values or invalid edge triple
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Must we handle negative values or invalid edge triple? \$\endgroup\$
    – DELETE_ME
    Oct 23, 2017 at 14:57
  • 2
    \$\begingroup\$ Very related. I'll leave it up to the rest of the community to decide if its a dup. \$\endgroup\$ Oct 23, 2017 at 17:18
  • 2
    \$\begingroup\$ I think that using coordinates instead of lengths changes the challenge significantly \$\endgroup\$
    – Luis Mendo
    Oct 23, 2017 at 18:24
  • 8
    \$\begingroup\$ There is no triangle with lengths 21, 38, 5, because 21 + 5 < 38. Is this an intentional pathological case that we have to handle? \$\endgroup\$
    – Kevin
    Oct 23, 2017 at 22:31
  • 1
    \$\begingroup\$ @Kevin no you have not to handle this case. User202729 has already asked this question :) \$\endgroup\$
    – mdahmoune
    Oct 24, 2017 at 6:44

81 Answers 81

1 2
3
2
\$\begingroup\$

Tcl, 70 bytes

proc R {a b c} {expr $a[set H ==hypot(]$b,$c)||$b$H$a,$c)||$c$H$a,$b)}

Try it online!

Still too long.

\$\endgroup\$
5
2
\$\begingroup\$

Pyt, 12 8 6 bytes

²ĐƩ₂⇹∈

Explanation:

                 implicit input (as a list, i.e., "[A,B,C]")
 ²               square each element in the list
  Đ              duplicate the list (on stack twice)
   Ʃ             sum elements in list on top of stack
    ₂            divide sum by 2
     ⇹           swap top two items on stack
      ∈          check if sum/2 is in the list of squares
                 implicit print

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$
    – mdahmoune
    Feb 2, 2018 at 11:02
  • \$\begingroup\$ Back when I posted this answer, it wasn't on TIO \$\endgroup\$
    – mudkip201
    Feb 2, 2018 at 13:13
2
\$\begingroup\$

GolfScript, 10 bytes

~${.*}/-+!

Try it online!

Takes an array as input and outputs 1 for true and 0 for false.

~$           # Sort the array                  [3 4 5]       [1 2 3]
  {.*}/      # Square all elements             9 16 25       1 4 9
       -     # Subtract the last two numbers   9 -9          1 -5
        +    # Add the numbers left            0             -4
         !   # Negate the answer               1             0
\$\endgroup\$
2
\$\begingroup\$

Templates Considered Harmful, 140 bytes

Fun<Ap<Fun<bor<Eq<Add<A<1>,A<2>>,A<3>>,bor<Eq<Add<A<2>,A<3>>,A<1>>,Eq<Add<A<3>,A<1>>,A<2>>>>>,Mul<A<1>,A<1>>,Mul<A<2>,A<2>>,Mul<A<3>,A<3>>>>

Try it online!

Anonymous function (all TCH functions are anonymous) that takes 3 integers as inputs.

Cool language that uses C++ templates evaluated by a typedef. Interestingly, this is a similar length to the actual C++ answer.


Fun<  //Anonymous function declaration
  Ap< //Apply following function with arguments (a²,b²,c²)
    Fun<
      bor<
        Eq<Add<A<1>,A<2>>,A<3>>,  // (a²+b²=c²)|
        bor<                      //((b²+c²=a²)|
          Eq<Add<A<2>,A<3>>,A<1>>,// (c²+a²=b²))
          Eq<Add<A<3>,A<1>>,A<2>>
        >
      >
    >,
    Mul<A<1>,A<1>>,Mul<A<2>,A<2>>,Mul<A<3>,A<3>> //arguments (a²,b²,c²)
  >
>
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 35 bitsv2, 4.375 bytes

²:∑½c

Try it Online!

Ports Thunno 2

Explained (old)

s²Ṙḣ∑=
s²     # sort and square the input
  Ṙ    # reverse the list so that it's in descending order
   ḣ   # push the head of that, and the rest of that to the stack
    ∑= # does the sum of the list equal the other item? 
\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 18 14 bytes

#.#==2Max@#^2&

Try it online!

Thanks to @att for –4 bytes!

Checks if the sum of the squared numbers is equal to twice the square of the maximum number.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Tr[#^2]->#.# \$\endgroup\$
    – att
    Jun 10, 2023 at 4:00
  • \$\begingroup\$ @att I must have been asleep at the wheel when I wrote this code. Thanks for the heads-up! \$\endgroup\$
    – Roman
    Jun 10, 2023 at 8:34
1
\$\begingroup\$

Mathematica 39 24 bytes

With 15 bytes saved thanks to Jenny_mathy.

#^2+#2^2==#3^2&@@Sort@#&

Sort ensures that the diagonal will be the third element of z. z[[1]]^2 means

Example

#^2+#2^2==#3^2&@@Sort@#&[{3,5,4}]

 (*True*)
\$\endgroup\$
4
  • \$\begingroup\$ Can it be done as a function? \$\endgroup\$
    – mdahmoune
    Oct 23, 2017 at 15:39
  • \$\begingroup\$ It is a pure function, employed as if it were a single word. \$\endgroup\$
    – DavidC
    Oct 23, 2017 at 20:12
  • 1
    \$\begingroup\$ 3 bytes shorter #&@@(z=Sort@#)^2+z[[2]]^2==z[[3]]^2& \$\endgroup\$
    – Keyu Gan
    Oct 23, 2017 at 23:07
  • 5
    \$\begingroup\$ 24 bytes: #^2+#2^2==#3^2&@@Sort@#& \$\endgroup\$
    – ZaMoC
    Oct 24, 2017 at 16:50
1
\$\begingroup\$

PHP, 48 47 bytes

sort($a);echo($a[2]**2==$a[1]**2+$a[0]**2)?1:0;

Try it online!

Outputs 1 for true, 0 for false.

\$\endgroup\$
1
  • \$\begingroup\$ If it is acceptable to emit empty string for false, you can shave the four character ternary. \$\endgroup\$
    – Umbrella
    Jun 19, 2018 at 21:34
1
\$\begingroup\$

Excel VBA, 49 Bytes

Anonymous VBE immediate window function that takes input from range [A1:C1] and output to the VBE immediate window.

[2:2]=[(1:1)^2]:?[Or(A2+B2=C2,B2+C2=A2,A2+C2=B2)]
\$\endgroup\$
1
\$\begingroup\$

Lua, 66 bytes

function f(...)t={...}table.sort(t)print(t[1]^2+t[2]^2==t[3]^2)end

This could be simplified by using table call syntax which would mean that the table does not need to be constructed in the function, saving 9 bytes.

Try it online!

Try it online on tio.run!

\$\endgroup\$
3
  • \$\begingroup\$ PLZ could you add the two solutions? \$\endgroup\$
    – mdahmoune
    Oct 26, 2017 at 14:40
  • \$\begingroup\$ Can be golfed with function f(...)t={...} \$\endgroup\$
    – ATaco
    Oct 29, 2017 at 22:24
  • 2
    \$\begingroup\$ Are those not 66 bytes? \$\endgroup\$ Oct 30, 2017 at 0:41
1
\$\begingroup\$

Pyth - 23 22 Bytes, 22 21 if falsy value doesn't need to be the same every time

!-+^@KSQZ2^@K1 2^@K2 2

Try it online!

Returns True or False

or (if falsy value does not need to be the same every time)

-+^@KSQZ2^@K1 2^@K2 2

Try it online!

Returns 0 or a number other than 0

This can probably be golfed a lot

Explanation:

!        Logical negate; Makes 0 true and others false. Not necessary if falsy values can be different 
 -       Subtract
  +      Add
   ^     To the power of
    @    Index in
     K   Assign variable K, returning K
      S  Sorted
       Q Input
     Z   Zero
    2    2
   ^     To the power of
    @    Index in
     K   Variable K
     1   1
    2    2
   ^     To the power of
    @    Index In
     K   Variable K
     2   2
    2    2
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes

à‚nOË

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ some explanations plz :)? \$\endgroup\$
    – mdahmoune
    Feb 2, 2018 at 13:33
  • \$\begingroup\$ nàsOQ is another. Not thinking it's possible below 5. \$\endgroup\$ Feb 2, 2018 at 15:23
1
\$\begingroup\$

Julia 0.6, 14 bytes

L->L'L∈2L.^2

Try it online!

Based @mdahmoune's hint that "The problem is equivalent to whether (a² + b² + c²) ÷ 2 is in {a², b², c²}" - this expresses the condition "(a² + b² + c²) is in {2a², 2b², 2c²}" for a given array L=[a,b,c].

L'L is multiplying the array by itself as a matrix multiplication, so

[a b c]*[a    = a^2 + b^2 + c^2
         b
         c]

L.^2 is elementwise squaring, so is equal to [a^2 b^2 c^2].

is a synonym to in, and checks membership - so the code checks that "sum of squares evaluates to twice of any one of the squares".

Just saw @Dennis' previous Julia answer and saved a few bytes thanks to that. This golf improves on it by two bytes, by using L'L instead of L⋅L (⋅ is a 3-byte Unicode character).

\$\endgroup\$
0
1
\$\begingroup\$

PHP, 44 bytes

echo($a**2+$b**2+$c**2)/2==max($a,$b,$c)**2;

Try it online!

Half the sum of the squares of the sides should equal the square of the max side. Emits "1" for true and "" (empty string) for false.

The TIO link runs all the tests in a loop.

\$\endgroup\$
1
  • \$\begingroup\$ Let me know if taking the inputs as three independently pre-set variables abuses the The input and output can be given in any convenient format. rule. \$\endgroup\$
    – Umbrella
    Jun 19, 2018 at 21:52
1
\$\begingroup\$

JavaScript (Node.js), 45 bytes

(a,b,c)=>[a*=a,b*=b,c*=c].includes((a+b+c)/2)

Try it online!

Half the sum of the squares of the sides should equal the square of the max side. Returns a bool.

The TIO link runs all the tests in a loop.

\$\endgroup\$
1
  • \$\begingroup\$ Making a shorthand function seemed shorter than a console.log(). \$\endgroup\$
    – Umbrella
    Jun 19, 2018 at 21:52
1
\$\begingroup\$

APL NARS 14 chars

{⍵∊⍨√2÷⍨+/⍵*2}

(seen in some other answer) test:

  f←{⍵∊⍨√2÷⍨+/⍵*2}
  f 3 4 5
1
  f 1 1 1
0
  f 5 3 4
1
  f 3 5 4
1
  f 12 37 35
1
  f 21 38 50
0
  f 210 308 250
0

Here ⍵ is the argument of function f.

{⍵∊⍨√2÷⍨+/⍵*2}   
           ⍵*2} if ⍵=1 2 3, ⍵*2 will be 1 4 9 (square the argument ⍵)
         +/     if ⍵*2 is 1 4 9 here sum it 1+4+9=14(sum list)
     √2÷⍨       here makes d=sqrt( (sum list above)/2 )
 ⍵∊⍨            here return 1 if d is element of ⍵, else return 0
                 because ⍨ reverse arguments of its left operator ∊

This follow from this: Given a, b, c the length of one triangle, they are the length of one right triangle <=> a^2+b^2=c^2 and a,b,c different from 0.

  |\
  | \
  |  \ 
 a|   \c
  |    \
  |_____\
     b
  a^2+b^2=c^2 <=> (a^2+b^2+c^2)/2=(2*a^2+2*b^2)/2=a^2+b^2=c^2 <=> 
  <=> (a^2+b^2+c^2)/2 ∊ {a^2, b^2, c^2}
\$\endgroup\$
6
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$
    – mdahmoune
    Feb 2, 2018 at 11:02
  • \$\begingroup\$ @mdahmoune the code would run only in a Nars interpreter... I don't know where find that interpreter in the net...( Because I use one symbol function sqrt, not present in other Apl interpreters) So no trust in what I say... (for see if run ok one would have download Apl Nars, load the code , execute it) \$\endgroup\$
    – user58988
    Feb 2, 2018 at 11:41
  • \$\begingroup\$ So could u plz explain your solution? \$\endgroup\$
    – mdahmoune
    Feb 2, 2018 at 11:56
  • \$\begingroup\$ @mdahmoune done... I possibly make wrong something... \$\endgroup\$
    – user58988
    Feb 2, 2018 at 12:50
  • \$\begingroup\$ @mdahmoune because I make one error in understand the code, so I change the explanation where I make that error (sqrt( sum/2)) \$\endgroup\$
    – user58988
    Feb 2, 2018 at 13:00
1
\$\begingroup\$

Rust, 44 bytes

|n|{n.sort();n[0]*n[0]+n[1]*n[1]==n[2]*n[2]}

Try it online! Takes in an &mut[u64] of at least three elements and proceeds to sort and test if the Pythagorean theorem holds. If sorting floats in rust were easier (it's hard because they can't implement the Ord trait, which is required by the sorting functions) I could use the hypot function and wind up with this:

|n|{n.sort();n[0].hypot(n[1])==n[2]}

which is much shorter.

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 55 bytes

DEF R(T)SORT T
RETURN SQR(T[0]*T[0]+T[1]*T[1])==T[2]END
\$\endgroup\$
5
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$
    – mdahmoune
    Oct 23, 2017 at 20:54
  • \$\begingroup\$ I don't think one exists, unfortunately. \$\endgroup\$
    – 12Me21
    Oct 23, 2017 at 21:52
  • \$\begingroup\$ Could you PLZ tell us how can test your code? \$\endgroup\$
    – mdahmoune
    Oct 25, 2017 at 12:46
  • \$\begingroup\$ A quick search found this: en.m.wikipedia.org/wiki/Petit_Computer . I don't know if you can download it and run it on DSi emulator though. \$\endgroup\$
    – Heimdall
    Nov 10, 2017 at 8:52
  • \$\begingroup\$ It appears it defines a function R with argument T and assumes T to be a zero indexed array containing 3 numbers. SQR must be square root function. Unlike other BASICs this one uses C-like == rather than = for comparison. I guess I would just have to find a copy of the manual online to assess it as the code is really straightforward. \$\endgroup\$
    – Heimdall
    Nov 10, 2017 at 9:04
0
\$\begingroup\$

Lua, 90 bytes

t={...}for k,v in pairs(t)do t[k]=tonumber(v)end table.sort(t)print(t[1]^2+t[2]^2==t[3]^2)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 7 bytes

øUx²z q

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 5 bytes

²DS½Ƈ

Attempt This Online!

Port of DELETE_ME's Jelly answer.

Explanation

²DS½Ƈ  # Implicit input         ->  [5, 3, 4]
²      # Square each value      ->  [25, 9, 16]
 DS    # Duplicate and sum      ->  [25, 9, 16]  50
   ½   # Halve the sum          ->  [25, 9, 16]  25
    Ƈ  # Check for containment  ->  1
       # Implicit output
\$\endgroup\$
1 2
3

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.