| Bytes | Lang | Time | Link |
|---|---|---|---|
| 068 | Tcl | 171023T161808Z | sergiol |
| 008 | Uiua | 241205T140644Z | nyxbird |
| 046 | AWK | 241202T195851Z | xrs |
| 065 | Swift 6 | 240907T225849Z | macOSist |
| 007 | Husk | 240907T185354Z | Glory2Uk |
| 014 | Wolfram Language Mathematica | 201021T180138Z | Roman |
| 4375 | Vyxal | 220127T050941Z | lyxal |
| 005 | Thunno 2 | 230609T143252Z | The Thon |
| 048 | MS Excel | 171029T223752Z | Taylor R |
| 044 | Rust | 210302T193421Z | Aiden4 |
| 140 | Templates Considered Harmful | 210302T153244Z | Zack C. |
| 010 | GolfScript | 201021T163814Z | 2014MELO |
| 008 | Japt | 171023T171358Z | Shaggy |
| 014 | APL NARS | 171225T214753Z | user5898 |
| 007 | Japt | 190203T042121Z | Oliver |
| 047 | Excel | 180626T105719Z | Wernisch |
| 090 | Lua | 180620T020534Z | Alex All |
| 045 | JavaScript Node.js | 180619T214532Z | Umbrella |
| 044 | PHP | 180619T213352Z | Umbrella |
| 342 | Perl 5 | 171023T151444Z | Nahuel F |
| 014 | Julia 0.6 | 180615T202634Z | Sundar R |
| 031 | Triangularity | 180202T103519Z | Mr. Xcod |
| 010 | J | 171023T152827Z | Jonah |
| 006 | Pyt | 171225T152607Z | mudkip20 |
| 005 | 05AB1E | 180202T125057Z | Erik the |
| 040 | JavaScript 6 | 171226T105312Z | l4m2 |
| nan | 171128T191254Z | Tornado5 | |
| 037 | Python 2 | 171023T145319Z | Erik the |
| 010 | CJam | 171023T205224Z | kaine |
| 066 | Lua | 171026T131714Z | MCAdvent |
| 064 | Common Lisp | 171029T211753Z | Renzo |
| 006 | Ohm v2 | 171023T153821Z | Cinaski |
| 039 | Axiom | 171029T231724Z | user5898 |
| 049 | Excel VBA | 171029T223921Z | Taylor R |
| 057 | Triangular | 171026T205710Z | MickyT |
| 024 | Pari/GP | 171023T230804Z | Jeppe St |
| 013 | MY | 171023T202218Z | Adalynn |
| 047 | PHP | 171027T160935Z | Calimero |
| 008 | Pushy | 171024T102821Z | FlipTack |
| 094 | Go | 171023T231229Z | Riking |
| 009 | CJam | 171026T130559Z | aditsu q |
| 010 | TIBasic | 171024T105922Z | Timtech |
| 029 | Octave | 171024T113934Z | Bass |
| 005 | Jelly | 171023T144640Z | user2027 |
| 068 | Java OpenJDK 8 | 171023T145244Z | Roberto |
| 023 | Jq 1.5 | 171024T065400Z | jq170727 |
| 016 | Julia 0.6 | 171024T023819Z | Dennis |
| 032 | GNUOctave | 171025T041334Z | IEatBage |
| 018 | Stacked | 171025T033914Z | Conor O& |
| 011 | CJam | 171024T220550Z | Esolangi |
| 018 | Add++ | 171024T220120Z | caird co |
| 031 | Ruby | 171024T192302Z | anna328p |
| 024 | Mathematica 39 | 171023T153432Z | DavidC |
| 006 | 05AB1E | 171023T150852Z | Okx |
| 053 | C# | 171024T134313Z | Abbas |
| 016 | Dyalog APL | 171023T151147Z | J. Sall& |
| 068 | Swift 4 | 171024T105400Z | xoudini |
| nan | 171023T151837Z | Steadybo | |
| 049 | C gcc | 171024T093705Z | Popeko |
| nan | 171024T082850Z | Toby Spe | |
| 010 | APL Dyalog | 171024T082329Z | Adá |
| 043 | Python 2 | 171023T144017Z | TFeld |
| 010 | Pyth | 171023T174825Z | L3viatha |
| 068 | BrainFlak | 171024T013803Z | Nitrodon |
| 031 | R | 171023T195022Z | MickyT |
| 060 | Racket | 171023T160133Z | Misha La |
| 010 | RProgN 2 | 171023T220653Z | ATaco |
| 024 | Perl 6 | 171023T204512Z | Sean |
| 012 | Dyalog APL | 171023T200427Z | Adalynn |
| 031 | Haskell | 171023T200246Z | butterdo |
| 109 | Racket | 171023T195022Z | sdfbhg |
| 156 | C++ | 171023T193947Z | Dylan Tu |
| 006 | Gaia | 171023T194646Z | Mr. Xcod |
| 055 | SmileBASIC | 171023T171642Z | 12Me21 |
| 081 | Eukleides | 171023T163705Z | brhfl |
| 014 | Pyth | 171023T155139Z | Ian H. |
| 061 | Batch | 171023T154620Z | Neil |
| 037 | Python 3 | 171023T154408Z | xnor |
| 040 | JavaScript ES6 | 171023T145045Z | Arnauld |
| 007 | MATL | 171023T152235Z | Luis Men |
| 081 | Swift 3 | 171023T151710Z | Anonymou |
| 006 | Neim | 171023T151022Z | Okx |
| 039 | PowerShell | 171023T145740Z | AdmBorkB |
| 044 | Java 8 | 171023T145230Z | Kevin Cr |
| 031 | Proton | 171023T145153Z | hyperneu |
Uiua, 8 bytes
=+°⊟₃⍆°√
°√ square each element, ⍆ sort, °⊟₃ untriple the array onto the stack, and check if + adding the first and second elements = equals the third
Swift 6, 65 bytes
let r={{a in a[0]*a[0]+0+a[1]*a[1]==a[2]*a[2]}(($0+[]).sorted())}
r takes an Array of Ints. The array is expected to have exactly three values (less than that and it goes out with a bang, more than that and it goes out with a whimper).
Husk, 7 bytes
Ẋo=+m□O
Takes a list of side lengths and outputs a 1-element list: [1] if true, [0] - otherwise. I have started with a slightly different idea (§=ΣoD▲m□, 8 bytes) but later realized that Ẋ works on triples too.
Commented:
O # sort the list: put the longest side at the end
m□ # square every side
Ẋo # construct a function with 3 arguments that operates on list:
+ # sum the 1st and the 2nd elements
= # check whether the result is equal to the 3rd element
Wolfram Language (Mathematica), 18 14 bytes
#.#==2Max@#^2&
Thanks to @att for –4 bytes!
Checks if the sum of the squared numbers is equal to twice the square of the maximum number.
Vyxal, 35 bitsv2, 4.375 bytes
²:∑½c
Ports Thunno 2
Explained (old)
s²Ṙḣ∑=
s² # sort and square the input
Ṙ # reverse the list so that it's in descending order
ḣ # push the head of that, and the rest of that to the stack
∑= # does the sum of the list equal the other item?
Thunno 2, 5 bytes
²DS½Ƈ
Port of DELETE_ME's Jelly answer.
Explanation
²DS½Ƈ # Implicit input -> [5, 3, 4]
² # Square each value -> [25, 9, 16]
DS # Duplicate and sum -> [25, 9, 16] 50
½ # Halve the sum -> [25, 9, 16] 25
Ƈ # Check for containment -> 1
# Implicit output
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))
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.
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>>>>
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²)
>
>
GolfScript, 10 bytes
~${.*}/-+!
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
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}
Excel, 47 bytes
Returns FALSE for right-angled Triangles, else TRUE:
=ISERR(FIND(SQRT((A1^2+B1^2+C1^2)/2),A1&B1&C1))
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)
JavaScript (Node.js), 45 bytes
(a,b,c)=>[a*=a,b*=b,c*=c].includes((a+b+c)/2)
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.
PHP, 44 bytes
echo($a**2+$b**2+$c**2)/2==max($a,$b,$c)**2;
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.
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.
Julia 0.6, 14 bytes
L->L'L∈2L.^2
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).
Triangularity, 49 31 bytes
...)...
..IEO..
.M)2s^.
}Re+=..
Explanation
Every Triangularity program must have a triangular padding (excuse the pun). That is, the ith line counting from the bottom of the program must be padded with i - 1 dots (.) on each side. In order to keep the dot-triangles symmetrical and aesthetically pleasant, each line must consist of 2L - 1 characters, where L is the number of lines in the program. Removing the characters that make up for the necessary padding, here is how the code works:
)IEOM)2s^}Re+= Full program. Input: STDIN, Output: STDOUT, either 1 or 0.
) Pushes a zero onto the stack.
IE Evaluates the input at that index.
O Sorts the ToS (Top of the Stack).
M)2s^} Runs the block )2s^ on a separate stack, thus squaring each.
R Reverse.
e Dump the contents separately onto the stack.
+ Add the top two items.
= Check if their sum is equal to the other entry on the stack (c^2).
Checking if a triangle is right-angled in Triangularity...
J, 10 bytes
-6 bytes thanks to FrownyFrog
=`+/@\:~*:
original answer
(+/@}:={:)@/:~*:
/: sort the squares *:, then check if the sum of the first two +/@}: equals the last {:
Pyt, 12 8 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
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;}
Pyth - 23 22 Bytes, 22 21 if falsy value doesn't need to be the same every time
!-+^@KSQZ2^@K1 2^@K2 2
Returns True or False
or (if falsy value does not need to be the same every time)
-+^@KSQZ2^@K1 2^@K2 2
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
Python 2, 37 bytes
a,b,c=sorted(input())
1/(a*a+b*b-c*c)
-2 thanks to FlipTack.
-1 thanks to Craig Gidney.
Outputs via exit code (0 = false, 1 = true).
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
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.
Common Lisp, 64 bytes
(apply(lambda(x y z)(=(+(* x x)(* y y))(* z z)))(sort(read)#'<))
As usual in Common Lisp, true is T and false is NIL.
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)
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)]
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+..>p`S:U/U"p`!g<>/
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
pDo Nothing.
- true
$:*Square the third input.P":USg!Test if the third value is greater than the greatest of the previous.- true
p+U-sum the current stack and take away stored third value - false
p"U+-sum the least and stored third and subtract from greatest
- true
0=%test equality to zero and output result.
Pari/GP, 29 24 bytes
f(v)=v~==2*vecmax(v)^2
Saved five bytes by an obvious change from norml2(v) to v*v~.
Inspired by other answers.
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.
MY, 13 bytes
22ω^÷Σ2ω^=Σ𝔹←
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
Pushy, 10 8 bytes
GK2ek+=#
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
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))
}
CJam, 9
q~$W%~mh=
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
TI-Basic, 13 11 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
Octave, 29 bytes
sum(a=input("").^2)==max(2*a)
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
Jelly, 5 bytes
²µSHe
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.
Java (OpenJDK 8), 68 bytes
a->{java.util.Arrays.sort(a);return a[0]*a[0]+a[1]*a[1]==a[2]*a[2];}
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.
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?
Julia 0.6, 16 bytes
!x=x⋅x∈2x.*x
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².
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.
Stacked, 18 bytes
[sorted:*rev...+=]
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.
CJam, 12 11 bytes
This is depressingly long.
{$2f#)\:+=}
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/#)}
Anonymous block that takes an array of integers on the stack and returns a truthy or falsy integer.
Add++, 25 18 bytes
D,f,?,caaBcB*#s/2=
-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]
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.
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*)
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;}
Array-input (58)
a=>{Array.Sort(a);return a[0]*a[0]+a[1]*a[1]==a[2]*a[2];};
Dyalog APL, 17 16 bytes
{(+/2÷⍨⍵*2)∊⍵*2}
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.
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.
C, 68 54 bytes
Using user202729's solution.
f(a,b,c){return!((a*=a)+(b*=b)-(c*=c)&&a-b+c&&a-b-c);}
Thanks to @Christoph for golfing 14 bytes!
C, 85 bytes
#define C(a,b,c)if(a*a+b*b==c*c)return 1;
f(a,b,c){C(a,b,c)C(b,c,a)C(c,a,b)return 0;}
C (gcc), 49 bytes
n(a,b,c){return(a*=a)+(b*=b)-(c*=c)&a+c-b&b+c-a;}
Improves on Kevin Cruijssens technique
Returns 0 for a valid triangle, and a non-zero value otherwise
Two answers found by refining the answer by Steadybox and incorporating the technique in the Java answer by Kevin Cruijssen:
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)
;
}
APL (Dyalog), 10 bytes
(+/∊×∘2)×⍨
×⍨ square (lit. multiplication selfie)
(…) apply the following anonymous tacit function on that:
+/ the sum
∊ is a member of
×∘2 the doubled amounts
Python 2, 43 bytes
lambda a,b,c:(a*a+b*b+c*c)/2in(a*a,b*b,c*c)
Python 2, 79 70 68 62 bytes
lambda*l:any(A*A+B*B==C*C for A,B,C in zip(l,l[1:]+l,l[2:]+l))
Pyth, 11 10 bytes
}/sK^R2Q2K
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
Brain-Flak, 68 bytes
({({({})({}[()])}{}<>)<>})<>({<(({}){}<>[({})])>(){[()](<{}>)}{}<>})
Uses the observation in user202729's answer.
{ } for each input number
{({})({}[()])}{} compute the square
( <>)<> push onto second stack
( ) push sum of squares onto first stack
<> move to second stack
{ } for each square
(({}){}<>[({})]) compute 2 * this square - sum of squares
< >(){[()](<{}>)}{}<> evaluate loop iteration as 1 iff equal
( ) push 1 if any squares matched, 0 otherwise
R, 31 26 30 bytes
cat(sum(a<-scan()^2)/max(a)==2)
I don't like this one as much, but it is shorter. Sums the squares and divides by the largest square. Truthy if 2.
Previous Version (modified with cat and with @Guiseppe's tip)
cat(!sort(scan())^2%*%c(1,1,-1))
Do a sum of the sorted input with the last item negated and return the ! not.
Racket, 64 60 bytes
(λ(a b c)(=(+(* a a)(* b b)(* c c))(*(expt(max a b c)2)2)))
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.
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?
Perl 6, 24 bytes
{(*²+*²==*²)(|.sort)}
*²+*²==*² is an anonymous function that returns true if the sum of the squares of its first two arguments is equal to the square of its third argument. We pass the sorted input list to this function, flattening it into the argument list with |.
Haskell (33 32 31 bytes)
(\x->(sum x)/2`elem`x).map(^2)
Original version:
(\x->2*maximum x==sum x).map(^2)
Anonymous function. Takes a list in the form [a,b,c]. Outputs True or False.
First version checked if the sum of the squares was twice the square of the maximum.
Second, slightly better version checks if half the sum of squares is an element in the list of squares.
Edit: Accidentally counted a newline, thanks H.PWiz
Racket, 109 bytes
#lang racket/base
(define (isright a b c)
(if (equal? (+ (* a a) (* b b)) (* c c))
"yes"
"no"))
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);}
Gaia, 6 bytes
s¦ΣḥuĖ
s¦- square each.Σ- sum.ḥ- ḥalve.u- square root.Ė- contains? Check if the square root is in the input.
SmileBASIC, 55 bytes
DEF R(T)SORT T
RETURN SQR(T[0]*T[0]+T[1]*T[1])==T[2]END
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.
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.
Python 3, 37 bytes
lambda*l:sum(x*x/2for x in l)**.5in l
Might run into float precision issues with large inputs.
JavaScript (ES6), 43 41 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
MATL, 7 bytes
SU&0)s=
Explanation
Consider input [12, 37, 35].
S % Implicit input. Sort
% [12, 35, 37]
U % Square each entry
% [144, 1225, 1369]
&0) % Push last entry and remaining entries
% STACK: 1369, [144, 1225]
s % Sum of array
% STACK: 1369, 1369
= % Isequal? Implicit display
% STACK: 1
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])")}
PowerShell, 39 bytes
$a,$b,$c=$args|sort;$a*$a+$b*$b-eq$c*$c
Sorts the input, stores that into $a,$b,$c variables. Then uses Pythagorean theorem to check whether a*a + b*b = c*c. Output is either Boolean True or False.
Java 8, 44 bytes
(a,b,c)->(a*=a)+(b*=b)==(c*=c)|a+c==b|b+c==a
Explanation:
(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)