# 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? – user202729 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. – Digital Trauma Oct 23 '17 at 17:18
• I think that using coordinates instead of lengths changes the challenge significantly – Luis Mendo 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? – Kevin Oct 23 '17 at 22:31
• @Kevin no you have not to handle this case. User202729 has already asked this question :) – mdahmoune Oct 24 '17 at 6:44

# Jelly, 5 bytes

²µSHe


Try it online!

Technical note: Bytes are counted in Jelly codepage.

Explanation:

²µSHe  Main link.
²      Square each number.
µ     With the new list,
S    calculate its sum,
H   and halve it.
e  Check if the result exists in the new list (squared input)


The problem is equivalent to being given three numbers a, b, c, and asking if there is a permutation such that a² + b² = c². This is equivalent to whether (a² + b² + c²) ÷ 2 is one of a², b² or c², so the program just checks that.

• well... I jelly. – Félix Gagnon-Grenier Oct 23 '17 at 16:55
• Just a technical note: symbols ² and µ cost two bytes each in UTF-8, so your code has actually 7 bytes, not 5 – Charlie Oct 26 '17 at 8:15
• @Charlie Answer edited for clarification. – user202729 Oct 26 '17 at 10:35

# Python 2, 37 bytes

a,b,c=sorted(input())
1/(a*a+b*b-c*c)


Try it online!

-2 thanks to FlipTack.
-1 thanks to Craig Gidney.

Outputs via exit code (0 = false, 1 = true).

• Bah. Came up with the exact same answer. You could modify the test suite to allow for any number of test cases: see here – mbomb007 Oct 24 '17 at 20:14
• @mbomb007 exec(code) hmmm, why exec (code) instead of exec code? :D ;-p – Erik the Outgolfer Oct 25 '17 at 11:47
• Haha, how does this answer have double the upvotes of xnor's shorter one? Maybe people just like the sweet simpleness of it – FlipTack Oct 25 '17 at 12:23
• @FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) – Erik the Outgolfer Oct 25 '17 at 12:26
• @EriktheOutgolfer Because the boilerplate isn't the part that is to be golfed. I made it so it'd work in either Python 2 or 3. – mbomb007 Oct 25 '17 at 16:15

# Java 8, 44 bytes

(a,b,c)->(a*=a)+(b*=b)==(c*=c)|a+c==b|b+c==a


Explanation:

Try it here.

(a,b,c)->                // Method with three integer parameters and boolean return-type
(a*=a)+(b*=b)==(c*=c)  //  Return if a*a + b*b == c*c
|a+c==b                //  or a*a + c*c == b*b
|b+c==a                //  or b*b + c*c == a*a
// End of method (implicit / single-line return-statement)

• Does it work without the parenthesis on the (c*=c)? The *= might have precidence over the == and you can save two bytes. – corsiKa Oct 23 '17 at 20:39
• @corsiKa I'm afraid it's the other way around. == has precedence over *=. =, +=, *=, and similar assignments actually have the lowest precedence in Java operators. – Kevin Cruijssen Oct 24 '17 at 6:43
• Can't find anything shorter... I tried to get the variables swapped to have the max value assigned to a (for instance), without any success. Well, I could do it, but around 65 characters... – Olivier Grégoire Oct 24 '17 at 7:56

# JavaScript (ES6), 4341 40 bytes

Saved 1 byte and fixed a bug thanks to @Neil

Takes input as an array of 3 integers. Returns true for right-angled and false otherwise.

a=>a.some(n=>Math.hypot(...a,...a)==n*2)


let f =

a=>a.some(n=>Math.hypot(...a,...a)==n*2)

console.log(f([5, 3, 4     ])) //  --> yes
console.log(f([3, 5, 4     ])) //  --> yes
console.log(f([12, 37, 35  ])) //  --> yes
console.log(f([21, 38, 5   ])) //  --> no
console.log(f([210, 308, 15])) //  --> no

# Original version, 44 bytes

Takes input as 3 integers. Returns 1 for right-angled and 0 otherwise.

(a,b,c)=>(a*=a)+(b*=b)==(c*=c)|a+c==b|b+c==a


### Test cases

let f =

(a,b,c)=>(a*=a)+(b*=b)==(c*=c)|a+c==b|b+c==a

console.log(f(5, 3, 4     )) //  --> yes
console.log(f(3, 5, 4     )) //  --> yes
console.log(f(12, 37, 35  )) //  --> yes
console.log(f(21, 38, 5   )) //  --> no
console.log(f(210, 308, 15)) //  --> no

• Looks like we came up with the exact same answer (except for the => and -> difference between JavaScript and Java 8). ;) So obvious +1 from me. – Kevin Cruijssen Oct 23 '17 at 14:57
• >>1 is unsafe, this returns true for [1, 1, 1]. – Neil Oct 23 '17 at 15:23
• How about Math.hypot(...a,...a)==n*2? – Neil Oct 23 '17 at 15:26
• @Neil Very nice fix :) – Arnauld Oct 23 '17 at 15:28
• @Neil There should be a ~= operator for "rougly equal" ;) – JollyJoker Oct 24 '17 at 14:12

# Python 3, 37 bytes

lambda*l:sum(x*x/2for x in l)**.5in l


Try it online!

Might run into float precision issues with large inputs.

# Triangular, 57 bytes

I haven't seen any in this language yet and it seemed appropriate to try and do one. It took a bit ... as I had to get my head around it first and I believe this could be golfed some more.

,$\:$:*/%*$"=P:pp.0"*>/>-:S!>/U+<U"g+..>pS:U/U"p!g<>/  Try it online! This expands to the following triangle.  ,$ \
: $: * / % *$ "  = P
: p p . 0 "
* > / > -  :
S ! > / U + < U
" g + . . > p  S
: U / U " p  ! g <
> /


The path is quite convoluted, but I'll try and explain what I have done. I will skip the directional pointers. Most of the code is stack manipulation.

• $:* Square the first input. • $:* Square the second input.
• S":Ug! Test if the second value is greater than the first.
• true p" Swap with the first.
• false p Do Nothing.

# JavaScript 34 bytes (without D=)

D=(d,[a,b,c]=d.sort())=>a*a+b*b==c*c

console.log(D([5, 3, 4       ])== true)
console.log(D([3, 5, 4       ])== true)
console.log(D([12, 37, 35    ])== true)
console.log(D([21, 38, 5     ])== false)
console.log(D([210, 308, 15  ])== false)

• I had a similar answer at 34: a=>a.sort()[0]**2+a[1]**2==a[2]**2 in ES6. So props to you @DanielIndie – WallyWest Oct 23 '17 at 22:37
• Unfortunately, sort() uses the lexicographical order when no callback is provided, making this code fail for instance for [10,6,8]. – Arnauld Dec 26 '17 at 11:10

# RProgN 2, 10 bytes

§²2^r]‘\+e


## Explained

§²2^r]‘\+e
§           # Sort the input list
²2^r       # Square each element in the list.
]      # Duplicate it on the reg stack.
‘     # Pop the top (largest) element off it
\+   # Swap it, sum the rest of the list.
e  # Are they equal?


Try it online!

• Why duplicate the list? – mdahmoune Oct 24 '17 at 19:29
• @mdahmoune RProgN2 doesn't keep the original list on the stack when popping an element off it, but stacks are by reference, so to keep the stack to do the sum part of it, it needs to be duplicated first. – ATaco Oct 24 '17 at 19:49
• Thanx upvote ;) – mdahmoune Oct 24 '17 at 19:52

# Racket, 64 60 bytes

(λ(a b c)(=(+(* a a)(* b b)(* c c))(*(expt(max a b c)2)2)))


Try it online!

### How it works

Tests if a^2 + b^2 + c^2 is equal to twice the largest of a^2, b^2, and c^2.

Returns #t for right triangles and #f for all other inputs.

• -4 bytes thanks to @xnor's suggestion to use expt.
• Awesome ;) but i think (define fun must be a part of the code... – mdahmoune Oct 23 '17 at 16:03
• Thank you! I think it's conventional to say that pure functions are allowed as answers. The (define fun ...) on TIO is just for convenience: we could equally well use this function as (... 3 4 5) where ... is the function. (So we could have a header of (print ( and a footer of 3 4 5)) if you prefer.) – Misha Lavrov Oct 23 '17 at 16:07
• (But this is one of my first Racket submissions, so I'm not too clear on what Racket-specific conventions there are, if any. Some past solutions using Racket have included #lang racket in the code; some haven't.) – Misha Lavrov Oct 23 '17 at 16:08
• Racket is so wordy that it's shorter to repeat (max a b c) than to do a let binding, huh? I don't suppose it would be shorter to bind as an argument to a λ? Or, isn't there an exponentiation built-in? – xnor Oct 23 '17 at 19:07
• @MishaLavrov Then how about (*(expt(max a b c)2)2)? – xnor Oct 23 '17 at 21:25

# 05AB1E, 6 bytes

n{R+Q


Try it online!

• The first example fails to detect [1,1,1] is not a valid input (common issue on some other attempts), but the second works fine. – Nick Loughlin Oct 24 '17 at 15:07
• @NickLoughlin Oops, removed first example – Okx Oct 24 '17 at 17:36
• You could do n{RÆ_ to save a byte. – Emigna Nov 29 '17 at 11:39

# Ruby, 31 bytes

->a{a,b,c=*a.sort;a*a+b*b==c*c}


Takes input as a list of 3 integers. Uses some ideas from other solutions.

• I just realized the answer I just posted is almost identical to yours. I promise I didn't copy yours (I actually had it sitting for a while in the "Post an answer" box), but since yours was submitted first, if you think mine is too close, I'll delete it. – iamnotmaynard Oct 24 '17 at 19:31
• @iamnotmaynard It's pretty much the same thing. this was a funny coincidence lol. Thanks for letting me know – dkudriavtsev Oct 24 '17 at 19:58
• If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune Oct 25 '17 at 15:46

# Julia 0.6, 16 bytes

!x=x⋅x∈2x.*x


Try it online!

### How it works

Let x = [a, b, c].

x⋅x is the dot product of x and itself, so it yields a² + b² + c².

2x.*x is the element-wise product of 2x and x, so it yields [2a², 2b², 2c²].

Finally, ∈ tests if the integer a² + b² + c² belongs to the vector [2a², 2b², 2c²], which is true iff
a² + b² + c² = 2a² or a² + b² + c² = 2b² or a² + b² + c² = 2c², which itself is true iff
b² + c² = a² or a² + c² = b² or a² + b² = c².

# Java (OpenJDK 8), 68 bytes

a->{java.util.Arrays.sort(a);return a[0]*a[0]+a[1]*a[1]==a[2]*a[2];}


Try it online!

• Can you save some bytes using currying rather than an array? – AdmBorkBork Oct 23 '17 at 15:04
• @AdmBorkBork Nope, because sort takes an array. – Olivier Grégoire Oct 24 '17 at 7:59

# TI-Basic, 1311 10 bytes

max(Ans=R►Pr(min(Ans),median(Ans


Now works for inputs in any order and is shorter as well. Another -1 thanks to @MishaLavrov

• If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune Oct 25 '17 at 15:45
• This only detects sorted right triangles: input of A=5, B=4, C=3 would not be correctly handled. – Misha Lavrov Oct 25 '17 at 16:29
• @MishaLavrov Thanks for pointing that out, it's actually shorter handling as a list. Now it works for inputs in any order. – Timtech Oct 25 '17 at 22:06
• If we leave off a single ), then max(Ans=R►Pr(min(Ans),median(Ans is also valid (though the computation we're doing here is different) and is one byte shorter. – Misha Lavrov Oct 25 '17 at 22:24
• @MishaLavrov That's interesting, I see what you mean. I think the expressions are equivalent for all non-negative inputs. – Timtech Oct 26 '17 at 11:37

# CJam, 9

q~$W%~mh=  Try it online Explanation: q~ read and evaluate the input (given as an array)$W%     sort and reverse the array
~       dump the array on the stack
mh      get the hypotenuse of a right triangle with the given 2 short sides
=       compare with the longer side

• Some explanations ;) ? – mdahmoune Oct 26 '17 at 14:41
• @mdahmoune here you go – aditsu Oct 26 '17 at 16:26
• Dang it. Didn't you write that language? Doesn't seem fair. (joke) – kaine Nov 1 '17 at 18:03

# Pari/GP, 29 24 bytes

f(v)=v~==2*vecmax(v)^2


Try it online!

Saved five bytes by an obvious change from norml2(v) to v*v~.

Here v must be a row vector or a column vector with three coordinates.

Example of use: f([3,4,5])

Of course, you get rational side lengths for free, for example f([29/6, 10/3, 7/2]).

If I do not count the f(v)= part, that is 19 bytes. The first part can also be written v-> (total 22 bytes).

Explanation: If the three coordinates of v are x, y and z, then the product of v and its transpose v~ gives a scalar x^2+y^2+^z^2, and we need to check if that is equal to twice the square of the maximum of the coordinates x, y, z.

Extra: The same f tests for a Pythagorean quadruple if your input vector has four coordinates, and so on.

• If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune Oct 25 '17 at 13:34
• @mdahmoune You can use this tio.run link. However, it is much nicer to just install PARI/GP locally. – Jeppe Stig Nielsen Oct 25 '17 at 13:52

# MS Excel, 49 Bytes

Anonymous worksheet function that takes input from the range [A1:C1] and outputs to the calling cell.

=OR(A1^2+B1^2=C1^2,B1^2+C1^2=A1^2,A1^2+C1^2=B1^2)


# Ohm v2, 8 6 bytes

²DS)Σε


Try it online!