# Is my triangle right?

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.
• Must we handle negative values or invalid edge triple? Oct 23 '17 at 14:57
• Very related. I'll leave it up to the rest of the community to decide if its a dup. Oct 23 '17 at 17:18
• I think that using coordinates instead of lengths changes the challenge significantly Oct 23 '17 at 18:24
• There is no triangle with lengths 21, 38, 5, because 21 + 5 < 38. Is this an intentional pathological case that we have to handle? Oct 23 '17 at 22:31
• @Kevin no you have not to handle this case. User202729 has already asked this question :) Oct 24 '17 at 6:44

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. • You think it can be more golfed? Oct 25 '17 at 12:42 • Oct 26 '17 at 0:15 • Oct 26 '17 at 0:20 • Thanx for the additional less golfed solutions, could you add them to the answer plz? Oct 26 '17 at 9:46 • I already tried to understand some solutions posted on other answers, and it happens, I will add more golfy solutions to main answer using the same approach. I will not publish the fails on the main answer as they are more recent and less golfy; and are also clickable links on the comments to working online demos. Oct 26 '17 at 17:19 # Pyt, 128 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! • If possible, please include a link to an online testing environment so other people can try out your code! Feb 2 '18 at 11:02 • Back when I posted this answer, it wasn't on TIO Feb 2 '18 at 13:13 # 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  # 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²) > >  # 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*)  • Can it be done as a function? Oct 23 '17 at 15:39 • It is a pure function, employed as if it were a single word. Oct 23 '17 at 20:12 • 3 bytes shorter #&@@(z=Sort@#)^2+z[[2]]^2==z[[3]]^2& Oct 23 '17 at 23:07 • 24 bytes: #^2+#2^2==#3^2&@@Sort@#& Oct 24 '17 at 16:50 # 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. • If it is acceptable to emit empty string for false, you can shave the four character ternary. Jun 19 '18 at 21:34 # 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)]  # 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! • PLZ could you add the two solutions? Oct 26 '17 at 14:40 • Can be golfed with function f(...)t={...} Oct 29 '17 at 22:24 • Are those not 66 bytes? Oct 30 '17 at 0:41 # 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  # 05AB1E, 5 bytes à‚nOË  Try it online! • some explanations plz :)? Feb 2 '18 at 13:33 • nàsOQ is another. Not thinking it's possible below 5. Feb 2 '18 at 15:23 # 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). # 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.

• 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. Jun 19 '18 at 21:52

# 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.

• Making a shorthand function seemed shorter than a console.log(). Jun 19 '18 at 21:52

# 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}

• If possible, please include a link to an online testing environment so other people can try out your code! Feb 2 '18 at 11:02
• @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)
– user58988
Feb 2 '18 at 11:41
• So could u plz explain your solution? Feb 2 '18 at 11:56
• @mdahmoune done... I possibly make wrong something...
– user58988
Feb 2 '18 at 12:50
• @mdahmoune because I make one error in understand the code, so I change the explanation where I make that error (sqrt( sum/2))
– user58988
Feb 2 '18 at 13:00

# Wolfram Language (Mathematica), 18 bytes

Tr[#^2]==2Max@#^2&


Try it online!

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

# 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.

# SmileBASIC, 55 bytes

DEF R(T)SORT T
RETURN SQR(T[0]*T[0]+T[1]*T[1])==T[2]END

• If possible, please include a link to an online testing environment so other people can try out your code! Oct 23 '17 at 20:54
• I don't think one exists, unfortunately. Oct 23 '17 at 21:52
• Could you PLZ tell us how can test your code? Oct 25 '17 at 12:46
• 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. Nov 10 '17 at 8:52
• 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. Nov 10 '17 at 9:04

# 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!

# Japt, 7 bytes

øUx²z q


Try it online!