# 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

# 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! Dec 26, 2017 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! Feb 2, 2018 at 17:46
• @Xcali, thanks, and even 34 +2, editing Feb 3, 2018 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. Jun 16, 2018 at 19:11
• thanks Erik seems the rules have changed since i've posted, don't hesitate to update Jun 17, 2018 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 Jun 26, 2018 at 12:08
• Is there proof why it work?
– l4m2
Jan 26, 2022 at 8:11

# MS Excel, 48 Bytes

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

=Let(a,A1^2,b,B1^2,c,C1^2,Or(a+b=c,b+c=a,a+c=b))


# 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? Oct 25, 2017 at 15:54
• @mdahmoune You can activate debug mode to see what's going on.
– Okx
Oct 25, 2017 at 15:56

# Swift 3, 81 bytes

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


## 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[0]>b[1]?b[0]>b[2]?0:2:b[1]>b[2]?1:2;}(a);int c[2]={g?0:1,g>1?1:2};return pow(a[c[0]],2)+pow(a[c[1]],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. Oct 23, 2017 at 20:05
• If possible, please include a link to an online testing environment so other people can try out your code! Oct 23, 2017 at 20:53
• 135 bytes May 29, 2020 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? Oct 23, 2017 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. Oct 24, 2017 at 1:08
• @user202729 While valid to point out, that may not be the most helpful way to address a new user. Oct 24, 2017 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. Oct 24, 2017 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.) Oct 24, 2017 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) Oct 23, 2017 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! Oct 25, 2017 at 13:50
• I've provided a main() - anyone can try the code! It's all standard C... Oct 25, 2017 at 16:28

# Swift 4, 68 bytes

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


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 Oct 23, 2017 at 19:57
• (⍵*2)÷2 can be golfed to 2÷⍨⍵*2.
Oct 24, 2017 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[0]*a[0]+a[1]*a[1]==a[2]*a[2];};


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? Oct 24, 2017 at 22:20
• @mdahmoune because I over complicate things. Golfing now Oct 24, 2017 at 22:23
• Upvote ;) please can you put the first solution too, it’s interesting Oct 24, 2017 at 22:45
• @mdahmoune added! Oct 24, 2017 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 ;) Oct 24, 2017 at 22:26
• Is it possible to add a link to test the solution please? Oct 24, 2017 at 22:28
• @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. Oct 30, 2017 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. Oct 30, 2017 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 ;) Oct 26, 2017 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, 2017 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)==.[2]


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) == .[2]    # 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? Oct 25, 2017 at 13:50
• Indeed that's a better approach. Thanks! Oct 25, 2017 at 15:35
• Could you plz add also the first solution too :) Oct 25, 2017 at 15:42

# 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? Oct 26, 2017 at 9:48
• Sure. There you go.
– Bass
Oct 26, 2017 at 10:39

# Go, 96 94 bytes

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


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? Oct 25, 2017 at 13:34
• I tried to do some stuff with complex numbers, but they were all higher character count. import "math/cmplx";cmplx.Abs() Oct 26, 2017 at 5:17
• You can drop the space between import and "sort" Oct 26, 2017 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)             [1]
#  \ 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? Oct 26, 2017 at 9:28
• @mdahmoune done. Oct 27, 2017 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 [email protected] Oct 24, 2017 at 10:44
• Is it possible to avoid computing ω^2 twice? Oct 25, 2017 at 13:33
• Maybe somehow, but MY doesn't have a duplicate command yet. Oct 25, 2017 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)

## CJam 10

q~$_.*~-+!  Simplest one I found but also shortest q reads input as string. Leave input formatted as array [3 4 5] ~ dumps it to stack$ sorts array
_ duplicates array
.* multiplies each element by itself
~ dumps array to stack
-+ determines largest minus two smallest numbers
! if 0 return 1 and 0 if anything else
`

Try it online

• If possible, please include a link to an online testing environment so other people can try out your code! Oct 23, 2017 at 20:53
• @mdahmoune sure. done. Oct 23, 2017 at 20:57