# 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

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

# JavaScript 6, recursive way, 40 Bytes

f=(a,b,c)=>c<a|c<b?f(b,c,a):a*a+b*b==c*c


f=(a,b,c)=>c<a|c<b?f(b,c,a):a*a+b*b==c*c
console.log(f(4,5,3));
console.log(f(4,5,6));

# C (gcc), 42 bytes

f(a,b,c){c=c<a|c<b?f(b,c,a):a*a+b*b==c*c;}


Try it online!

• That's a nice approach! – Arnauld Dec 26 '17 at 11:06

# Perl 5, 34 +2 (-ap) bytes

$x+=($_*=$_)/2for@F;$_=grep/$x/,@F  seems that can be shortened to 29 +2 but there is a warning: smartmatch is experimental $x+=($_*=$_)/2for@F;$_=$x~~@F


the question i couldn't prove but it works in all tests i've tried is if the number (a^2+b^2+c^2)/2 can be a substring of a number (a^2/2 b^2/2 or c^2/2) which would give a false positive.

Try It Online

• This can cut down to 35 + 2 by eliminating the @F assignment: Try it online! – Xcali Feb 2 '18 at 17:46
• @Xcali, thanks, and even 34 +2, editing – Nahuel Fouilleul Feb 3 '18 at 7:45
• We don't count -ap as +2 bytes, but, instead, Perl 5 with the -ap flags is not the same language as Perl 5 without any flags. – Erik the Outgolfer Jun 16 '18 at 19:11
• thanks Erik seems the rules have changed since i've posted, don't hesitate to update – Nahuel Fouilleul Jun 17 '18 at 18:36

# Excel, 47 bytes

Returns FALSE for right-angled Triangles, else TRUE:

=ISERR(FIND(SQRT((A1^2+B1^2+C1^2)/2),A1&B1&C1))

• Now that is clever – Taylor Scott Jun 26 '18 at 12:08

# Proton, 31 bytes

k=>(sum(j*j for j:k)/2)**.5in k


Try it online!

Credit to user202729 for the idea go upvote them

# Neim, 6 bytes

ᛦD𝐬ᚺS𝕚


Try it online!

• Could you plz add some explanations? – mdahmoune Oct 25 '17 at 15:54
• @mdahmoune You can activate debug mode to see what's going on. – Okx Oct 25 '17 at 15:56

# Swift 3, 81 bytes

func r(v:[Int]){let a=v.sorted{$0<$1};print("\(a*a+a*a==a*a)")}


## Batch, 61 bytes

@cmd/cset/ax=%1*%1,y=%2*%2,z=%3*%3,!((x+y-z)*(y+z-x)*(z+x-y))


Outputs 1 or 0 appropriately.

# Pyth, 14 bytes

K_m*ddSQqstKhK


Try it online!

# Eukleides, 81 bytes

x=number("")^2;y=number("")^2;z=number("")^2
print x==y+z or y==x+z or z==y+x?1|0


I thought Eukleides would handle this nicely using geometric builtins, but this actually turned out to be longer than just testing against the Pythagorean theorem, because the right triangle assertion is picky about order, and we have to turn our sides into a triangle first:

d e f triangle number(""),number(""),number("")
print right(d,e,f)or right(e,f,d)or right(f,d,e)?1|0


...which is 100 bytes. One nice thing about that one is it'll error out given an impossible triangle. Anyway, for either one, input is via STDIN, output is 1 for right, 0 for non-right via STDOUT. Doing a function was actually longer in this instance.

# C++, 156 bytes

bool f(int*a){int g=[](int*b){return b>b?b>b?0:2:b>b?1:2;}(a);int c={g?0:1,g>1?1:2};return pow(a[c],2)+pow(a[c],2)==pow(a[g],2);}

• Since this doesn't work without the #include<math.h>, you need to include it in the bytecount. Note that instead of using pow, you can just multiply each element with itself to avoid the #include. – Steadybox Oct 23 '17 at 20:05
• If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune Oct 23 '17 at 20:53
• 135 bytes – ceilingcat May 29 '20 at 18:40

# Racket, 109 bytes

#lang racket/base
(define (isright a b c)
(if (equal? (+ (* a a) (* b b)) (* c c))
"yes"
"no"))


Try it online!

• It seems not working for 5 3 4? – mdahmoune Oct 23 '17 at 20:51
• So this answer is not valid for the challenge. Also if you don't do trivial golfs such as those ones just because you're lazy then you are not a serious contender to the challenge. – user202729 Oct 24 '17 at 1:08
• @user202729 While valid to point out, that may not be the most helpful way to address a new user. – Misha Lavrov Oct 24 '17 at 17:18
• @sdfbhg The "trivial golfs" being pointed out are that (1) by convention we say that a pure function (λ expression) is a valid solution, leaving the # lang and define to the header and (2) an easy way to make the code shorter is to delete spaces around parentheses. Also, you can return #t and #f rather than "yes" and "no", bringing us here. – Misha Lavrov Oct 24 '17 at 17:18
• The point of both of these is so that different Racket submissions can be on an equal footing: if one person decides to write #lang racket and leave in spaces while another does not, we can't easily compare the actually different optimizations they found. (Anyway, as has been also mentioned, your submission needs to be fixed to work for right triangles not listed in a-b-c order.) – Misha Lavrov Oct 24 '17 at 17:20

# Dyalog APL, 12 bytes

(+/2÷⍨×⍨)∊×⍨


Uses the same method as this Jelly answer.

Try it online!

# Pyth, 11 10 bytes

}/sK^R2Q2K


Try it online!

My first Pyth submission! Explanation:

     R        right map
^ 2       squaring
Q      of the input
K          save this in K
s           sum up
/      2     divide by 2
}        K    test if this sum is in K


saved a byte thanks to Mr. Xcoder

• 10 bytes: }/sK^R2Q2K or /K^R2Q/sK2 (the former returns True/False, the latter returns 1/0) – Mr. Xcoder Oct 23 '17 at 19:35

# APL (Dyalog), 10 bytes

(+/∊×∘2)×⍨


Try it online!

×⍨ square (lit. multiplication selfie)

() apply the following anonymous tacit function on that:

+/ the sum

∊ is a member of

×∘2 the doubled amounts

# C, 52 bytes

f(a,b,c){return(a*=a)+(b*=b)==(c*=c)|b+c==a|c+a==b;}


# C, 74 bytes

#define _(a,b,c)a*a+b*b==c*c||
f(a,b,c){return _(a,b,c)_(b,c,a)_(c,a,b)0;}


# Test program

#include <stdio.h>
int test(int a, int b, int c, int expected)
{
int actual = f(a,b,c);
if (expected != actual) {
fprintf(stderr, "%d %d %d => %d\n ", a,b,c, actual);
return 1;
}
return 0;
}

int main()
{
return test(5, 3, 4, 1)
+  test(3, 5, 4, 1)
+  test(12, 37, 35, 1)
+  test(21, 38, 50, 0)
+  test(210, 308, 250, 0)
;
}

• 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:50
• I've provided a main() - anyone can try the code! It's all standard C... – Toby Speight Oct 25 '17 at 16:28

# Swift 4, 68 bytes

func f(v:inout[Int]){v.sort();print(v*v+v*v==v*v)}


Accepts a mutable array as inout argument and prints true or false.

# Dyalog APL, 17 16 bytes

{(+/2÷⍨⍵*2)∊⍵*2}


Try it!

Outputs 1 for true, 0 for false.

Thanks to @Adám for 1 byte.

### How it works

{(+/2÷⍨⍵*2)∊⍵*2}          # Anonymous function
(     ⍵*2)               # Each input (⍵) to the 2nd power
2÷⍨                   # divided by 2
+/                      # Sum
∊              # "is in"
⍵*2           # Each input (⍵) to the 2nd power


This uses the same logic as @user202729's Jelly answer, so some credit goes to them.

• You should probably update the code on the top, it still has those parens around ⍵*2 – Zacharý Oct 23 '17 at 19:57
• (⍵*2)÷2 can be golfed to 2÷⍨⍵*2. – Adám Oct 24 '17 at 8:25

## C# (53)

The shortest possible code I could find was the one that looks like the JAVA solution:

(a,b,c)=>{return(a*=a)+(b*=b)==(c*=c)|a+c==b|b+c==a;}


Try it online

## Array-input (58)

a=>{Array.Sort(a);return a*a+a*a==a*a;};


D,f,?,caaBcB*#s/2=


Try it online!

-7 bytes thanks to mdahmoune

Yes, I got outgolfed in my own language by 7 bytes. So what?

## How does it work?

D,f,?,      - Create a variadic function, f. Example arguments: [5 3 4]
c     - Clear the stack;      STACK = []
a     - Push the arguments;   STACK = [[5 3 4]]
a     - Push the arguments;   STACK = [[5 3 4] [5 3 4]]
Bc    - Push the columns;     STACK = [[5 5] [3 3] [4 4]]
B*    - Product of each;      STACK = [25 9 16]
#     - Sort the stack;       STACK = [9 16 25]
s     - Push the sum;         STACK = [9 16 25 50]
/     - Divide;               STACK = [9 16 2]
2     - Push 2;               STACK = [9 16 2 2]
=     - Equal;                STACK = [9 16 1]


Implicitly return the top element on the stack

## Old version

D,f,?,c2 2 2B]a@BcB#s/2=


The only difference is the squaring of each element (aaBcB* in the golfed version, 2 2 2B]a@BcB in this version), so I'll quickly explain how this part works

              - Stack is currently empty
2 2 2         - Push 2 three times;             STACK = [2 2 2]
B]       - Wrap the stack in an array;     STACK = [[2 2 2]]
a      - Push the arguments as an array; STACK = [[2 2 2] [5 3 4]]
@     - Reverse the stack;              STACK = [[5 3 4] [2 2 2]]
Bc   - Push the columns of the stack;  STACK = [[5 2] [3 2] [4 2]]
B - Reduce each by exponentiation;  STACK = [25 9 16]

• Why not push the input twice and reduce by multiplication? – mdahmoune Oct 24 '17 at 22:20
• @mdahmoune because I over complicate things. Golfing now – caird coinheringaahing Oct 24 '17 at 22:23
• Upvote ;) please can you put the first solution too, it’s interesting – mdahmoune Oct 24 '17 at 22:45
• @mdahmoune added! – caird coinheringaahing Oct 24 '17 at 22:51

## CJam, 12 11 bytes

This is depressingly long.

{$2f#)\:+=}  Try it online! Anonymous block that takes input as an array of integers on the stack and returns 1 (truthy) or 0 (falsey). Explanation: { e# Stack: [12 37 35]$     e# Sort:         [12 35 37]
2f#   e# Square each:  [144 1225 1369]
)     e# Pop:          [144 1225] 1369
\     e# Swap:         1369 [144 1225]
:+    e# Sum:          1369 1369
=     e# Equal:        1
}      e# Result: 1 (truthy)


### Old solution

{2f#_:+2/#)}


Try it online!

Anonymous block that takes an array of integers on the stack and returns a truthy or falsy integer.

• I like the swap trick ;) – mdahmoune Oct 24 '17 at 22:26
• Is it possible to add a link to test the solution please? – mdahmoune Oct 24 '17 at 22:28
• @mdahmoune TIO links added, thanks. – Esolanging Fruit Oct 25 '17 at 2:33
• @Challenger5 what are the rules for including code in the header/footer like that? If I accept the input as array (realize I can do now) and include "q~" in the header like you did my answer is only 8. – kaine Oct 30 '17 at 17:46
• @kaine The way CJam works is that {} defines an anonymous function that accepts input from the stack. q~ reads input and places it on the stack, and the ~ at the end runs the block. I don't have to score that part because anonymous functions are allowed by default, so I put it in the header/footer. – Esolanging Fruit Oct 30 '17 at 23:49

# Stacked, 18 bytes

[sorted:*rev...+=]


Try it online!

## Explanation

This is an anonymous function that takes an array of three integers. It sorts (sorted), squares each element of the array (:*), and reverses the array with rev. This will give an array with the largest value in the front. ... pushes each individual member of the array onto the stack, which would make the stack look like:

c^2  b^2  a^2


+ addes the top two members, yielding:

c^2  (b^2+a^2)


= tests for equality. yielding the expression a^2 + b^2 == c^2, which is only true for right triangles.

• Like the :* part ;) – mdahmoune Oct 26 '17 at 9:50

## GNUOctave, 32 bytes

A=input("")
sum(A.^2)/2==max(A)^2


The input must be like : [1,2,3] so Octave can evaluate it to a Matrix.

A.^2 squares all the elements of the matrix.

Outputs 1 if the triangle is right-angled, 0 otherwise.

You can try it online : https://octave-online.net

Edit (27 bytes): I saw this answer I thought I could find a better solution. I took the idea of squaring the matrix at the beginning. I don't feel like this deserves upvotes since it wasn't really my whole idea to begin with.

A=input("").^2
sum(A)/max(A)


Outputs 2 if the triangle is right-angled and anything else if it isn't.

• The newline counts as a byte too, better to replace it with ";", which also eliminates the spurious output (so the edited version is actually 28 bytes). The rules do require a specific output for both true and false cases; I don't think "anything else than 2" qualifies as specific. – Bass Oct 26 '17 at 10:55

# Jq 1.5, 31 23 bytes

map(.*.)|.[[add/2]]!=[]


Expects input in form of 3 element array, e.g. [5, 3, 4]

Expanded

  map(.*.)           # square each element
| .[[ add/2 ]]!=[]   # true if index of sum/2 element exists


Thanks to mdahmoune for suggesting shorter approach then my original one.

Try it online!

# Jq 1.5, 31 bytes

sort|map(.*.)|(.[:2]|add)==.


Expects input in form of 3 element array, e.g. [5, 3, 4]

Expanded

  sort                   # put elements in order
| map(.*.)               # square each element
| (.[:2]|add) == .    # is sum of first two equal to third?


Try it online!

• The problem is equivalent to whether (a² + b² + c²) ÷ 2 is in {a², b², c²}, do you think this approach will be shorter on Jq? – mdahmoune Oct 25 '17 at 13:50
• Indeed that's a better approach. Thanks! – jq170727 Oct 25 '17 at 15:35
• Could you plz add also the first solution too :) – mdahmoune Oct 25 '17 at 15:42
• Done! See also original edit – jq170727 Oct 25 '17 at 15:55

# Octave, 29 bytes

sum(a=input("").^2)==max(2*a)


Try it online!

Input has to be given in a format octave understands as a vector, like [3 5 4] or [12;35;37].

Checks for Pythagoras's theorem: first, square all the sides, then check if the sum of squares of all the sides is twice the square of the hypotenuse.

Output will be either ans = 1 or ans = 0

• Could you add some explanations? – mdahmoune Oct 26 '17 at 9:48
• Sure. There you go. – Bass Oct 26 '17 at 10:39

# Go, 96 94 bytes

package r;import"sort";func I(i...int)bool{sort.Ints(i);return i*i+i*i==i*i}


Test:

import "r"
import "fmt"

func main() {
fmt.Println(r.I(3, 5, 4))
fmt.Println(r.I(12, 37, 35))
fmt.Println(r.I(21, 38, 5))
fmt.Println(r.I(210, 308, 15))
}


Try it online!

• Is it possible to do it without sorting? – mdahmoune Oct 25 '17 at 13:34
• I tried to do some stuff with complex numbers, but they were all higher character count. import "math/cmplx";cmplx.Abs() – Riking Oct 26 '17 at 5:17
• You can drop the space between import and "sort" – mdahmoune Oct 26 '17 at 7:10

# Pushy, 10 8 bytes

GK2ek+=#


Try it online!

Managed to cut off two bytes by changing the approach, here's how it works now:

          \ Implicit Input                  eg. [3, 4, 5]
G         \ Sort the stack descendingly         [5, 4, 3]
K2e      \ Square all items                    [25, 16, 9]
k+    \ Sum last two                        [25, 25]
=   \ Check equality (1 or 0)             
#  \ Output result                       PRINT: 1


## Original Version, 10 bytes

gK2eSk2/=#

\ Input implicitly on stack.              eg. [5, 4, 3]
g         \ Sort the stack ascendingly.                 [3, 4, 5]
K2e       \ Square all items.                           [9, 16, 25]
Sk2/      \ Append stack sum divided by 2               [9, 16, 25, 25]
=#        \ Print equality of last two items (1/0)      PRINT: 1

• Could you plz add the first solution too? – mdahmoune Oct 26 '17 at 9:28
• @mdahmoune done. – FlipTack Oct 27 '17 at 15:12

# MY, 13 bytes

22ω^÷Σ2ω^=Σ𝔹←


Try it online!

This helped me realize ... I screwed up the NOT function (and the boolean conversion function).

## How it works (cross-compatible function)

22ω^÷Σ2ω^=Σ𝔹←
2             = push 2 to the stack
2ω^          = push ω^2 to the stack (functions vectorize)
÷         = pop a, then b. push a/b (rational) to the stack
Σ        = sum
2ω^=    = equality test with ω^2
Σ𝔹  = boolean conversion
← = output

• You can ask Dennis to pull it yourself here or by contacting him at feedback@tryitonline.net – caird coinheringaahing Oct 24 '17 at 10:44
• Is it possible to avoid computing ω^2 twice? – mdahmoune Oct 25 '17 at 13:33
• Maybe somehow, but MY doesn't have a duplicate command yet. – Zacharý Oct 25 '17 at 16:08

# Axiom, 39 bytes

f x==(y:=sort x;y.1^2+y.2^2=y.3^2=>1;0)
`

f(x) function Input one list of at last 3 numbers

Output 1 (it is right triangle)

Output 0 (it is not right triangle)