# 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, 2017 at 14:57
• Very related. I'll leave it up to the rest of the community to decide if its a dup. Oct 23, 2017 at 17:18
• I think that using coordinates instead of lengths changes the challenge significantly Oct 23, 2017 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, 2017 at 22:31
• @Kevin no you have not to handle this case. User202729 has already asked this question :) Oct 24, 2017 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. Oct 23, 2017 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 Oct 26, 2017 at 8:15
• @Charlie Answer edited for clarification. Oct 26, 2017 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 Oct 24, 2017 at 20:14
• @mbomb007 exec(code) hmmm, why exec (code) instead of exec code? :D ;-p Oct 25, 2017 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 Oct 25, 2017 at 12:23
• @FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) Oct 25, 2017 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. Oct 25, 2017 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. Oct 23, 2017 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. Oct 24, 2017 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... Oct 24, 2017 at 7:56

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

# 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. Oct 23, 2017 at 14:57
• >>1 is unsafe, this returns true for [1, 1, 1].
– Neil
Oct 23, 2017 at 15:23
• How about Math.hypot(...a,...a)==n*2?
– Neil
Oct 23, 2017 at 15:26
• @Neil Very nice fix :) Oct 23, 2017 at 15:28
• @Neil There should be a ~= operator for "rougly equal" ;) Oct 24, 2017 at 14:12

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

# Gaia, 6 bytes

s¦ΣḥuĖ


Try it online!

• s¦ - square each.

• Σ - sum.

• ḥ - alve.

• u - square root.

• Ė - contains? Check if the square root is in the input.

• Is it better than sorting version? Oct 25, 2017 at 12:47
• @mdahmoune I believe it is better. I will try to solve it the other way around, but TBH I think that'd be longer. Oct 25, 2017 at 12:55
• Upvote any way :) Oct 25, 2017 at 12:57

# 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? Oct 24, 2017 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. Oct 24, 2017 at 19:49
• Thanx upvote ;) Oct 24, 2017 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... Oct 23, 2017 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.) Oct 23, 2017 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.) Oct 23, 2017 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, 2017 at 19:07
• @MishaLavrov Then how about (*(expt(max a b c)2)2)?
– xnor
Oct 23, 2017 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. Oct 24, 2017 at 15:07
• @NickLoughlin Oops, removed first example
– Okx
Oct 24, 2017 at 17:36
• You could do n{RÆ_ to save a byte. Nov 29, 2017 at 11:39
• @Emigna The _ could also be a >, since in this case they have the same effect - making a truthy output if the top of stack is 0 and falsy otherwise. Since 1 is the only truthy value in 05AB1E, > will increment 0 to 1 and output it; for other values, it will increment it and output it. Since any value other than 1 is falsy, it will be a falsy output. Mar 3, 2021 at 21:30

# 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. Oct 24, 2017 at 19:31
• @iamnotmaynard It's pretty much the same thing. this was a funny coincidence lol. Thanks for letting me know Oct 24, 2017 at 19:58
• If possible, please include a link to an online testing environment so other people can try out your code! Oct 25, 2017 at 15:46

# Java (OpenJDK 8), 68 bytes

a->{java.util.Arrays.sort(a);return a*a+a*a==a*a;}


Try it online!

• Can you save some bytes using currying rather than an array? Oct 23, 2017 at 15:04
• @AdmBorkBork Nope, because sort takes an array. Oct 24, 2017 at 7:59
• Suggest Math.hypot(a,a)==a instead of a*a+a*a==a*a Nov 22, 2019 at 20:46

# 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! Oct 25, 2017 at 15:45
• This only detects sorted right triangles: input of A=5, B=4, C=3 would not be correctly handled. Oct 25, 2017 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. Oct 25, 2017 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. Oct 25, 2017 at 22:24
• @MishaLavrov That's interesting, I see what you mean. I think the expressions are equivalent for all non-negative inputs. Oct 26, 2017 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 ;) ? Oct 26, 2017 at 14:41
• @mdahmoune here you go Oct 26, 2017 at 16:26
• Dang it. Didn't you write that language? Doesn't seem fair. (joke) Nov 1, 2017 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! Oct 25, 2017 at 13:34
• @mdahmoune You can use this tio.run link. However, it is much nicer to just install PARI/GP locally. Oct 25, 2017 at 13:52

# Ohm v2, 8 6 bytes

²DS)Σε


Try it online!

# Common Lisp, 64 bytes

(apply(lambda(x y z)(=(+(* x x)(* y y))(* z z)))(sort(read)#'<))


Try it online!

As usual in Common Lisp, true is T and false is NIL.