46
\$\begingroup\$

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.
\$\endgroup\$
  • 1
    \$\begingroup\$ Must we handle negative values or invalid edge triple? \$\endgroup\$ – user202729 Oct 23 '17 at 14:57
  • 2
    \$\begingroup\$ Very related. I'll leave it up to the rest of the community to decide if its a dup. \$\endgroup\$ – Digital Trauma Oct 23 '17 at 17:18
  • 2
    \$\begingroup\$ I think that using coordinates instead of lengths changes the challenge significantly \$\endgroup\$ – Luis Mendo Oct 23 '17 at 18:24
  • 8
    \$\begingroup\$ There is no triangle with lengths 21, 38, 5, because 21 + 5 < 38. Is this an intentional pathological case that we have to handle? \$\endgroup\$ – Kevin Oct 23 '17 at 22:31
  • 1
    \$\begingroup\$ @Kevin no you have not to handle this case. User202729 has already asked this question :) \$\endgroup\$ – mdahmoune Oct 24 '17 at 6:44

76 Answers 76

2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ – mdahmoune Oct 23 '17 at 20:53
  • \$\begingroup\$ @mdahmoune sure. done. \$\endgroup\$ – kaine Oct 23 '17 at 20:57
2
\$\begingroup\$

Tcl, 70 bytes

proc R {a b c} {expr $a[set H ==hypot(]$b,$c)||$b$H$a,$c)||$c$H$a,$b)}

Try it online!

Still too long.

\$\endgroup\$
2
\$\begingroup\$

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ – mdahmoune Feb 2 '18 at 11:02
  • \$\begingroup\$ Back when I posted this answer, it wasn't on TIO \$\endgroup\$ – mudkip201 Feb 2 '18 at 13:13
1
\$\begingroup\$

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*)
\$\endgroup\$
  • \$\begingroup\$ Can it be done as a function? \$\endgroup\$ – mdahmoune Oct 23 '17 at 15:39
  • \$\begingroup\$ It is a pure function, employed as if it were a single word. \$\endgroup\$ – DavidC Oct 23 '17 at 20:12
  • 1
    \$\begingroup\$ 3 bytes shorter #&@@(z=Sort@#)^2+z[[2]]^2==z[[3]]^2& \$\endgroup\$ – Keyu Gan Oct 23 '17 at 23:07
  • 5
    \$\begingroup\$ 24 bytes: #^2+#2^2==#3^2&@@Sort@#& \$\endgroup\$ – J42161217 Oct 24 '17 at 16:50
1
\$\begingroup\$

PHP, 48 47 bytes

sort($a);echo($a[2]**2==$a[1]**2+$a[0]**2)?1:0;

Try it online!

Outputs 1 for true, 0 for false.

\$\endgroup\$
  • \$\begingroup\$ If it is acceptable to emit empty string for false, you can shave the four character ternary. \$\endgroup\$ – Umbrella Jun 19 '18 at 21:34
1
\$\begingroup\$

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)]
\$\endgroup\$
1
\$\begingroup\$

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.

Try it online!

Try it online on tio.run!

\$\endgroup\$
  • \$\begingroup\$ PLZ could you add the two solutions? \$\endgroup\$ – mdahmoune Oct 26 '17 at 14:40
  • \$\begingroup\$ Can be golfed with function f(...)t={...} \$\endgroup\$ – ATaco Oct 29 '17 at 22:24
  • 2
    \$\begingroup\$ Are those not 66 bytes? \$\endgroup\$ – Jonathan Frech Oct 30 '17 at 0:41
1
\$\begingroup\$

Pyth - 23 22 Bytes, 22 21 if falsy value doesn't need to be the same every time

!-+^@KSQZ2^@K1 2^@K2 2

Try it online!

Returns True or False

or (if falsy value does not need to be the same every time)

-+^@KSQZ2^@K1 2^@K2 2

Try it online!

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
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes

à‚nOË

Try it online!

\$\endgroup\$
  • \$\begingroup\$ some explanations plz :)? \$\endgroup\$ – mdahmoune Feb 2 '18 at 13:33
  • \$\begingroup\$ nàsOQ is another. Not thinking it's possible below 5. \$\endgroup\$ – Magic Octopus Urn Feb 2 '18 at 15:23
1
\$\begingroup\$

Julia 0.6, 14 bytes

L->L'L∈2L.^2

Try it online!

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

\$\endgroup\$
1
\$\begingroup\$

PHP, 44 bytes

echo($a**2+$b**2+$c**2)/2==max($a,$b,$c)**2;

Try it online!

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.

\$\endgroup\$
  • \$\begingroup\$ Let me know if taking the inputs as three independently pre-set variables abuses the The input and output can be given in any convenient format. rule. \$\endgroup\$ – Umbrella Jun 19 '18 at 21:52
1
\$\begingroup\$

JavaScript (Node.js), 45 bytes

(a,b,c)=>[a*=a,b*=b,c*=c].includes((a+b+c)/2)

Try it online!

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.

\$\endgroup\$
  • \$\begingroup\$ Making a shorthand function seemed shorter than a console.log(). \$\endgroup\$ – Umbrella Jun 19 '18 at 21:52
1
\$\begingroup\$

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}
\$\endgroup\$
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ – mdahmoune Feb 2 '18 at 11:02
  • \$\begingroup\$ @mdahmoune the code would run only in a Nars interpreter... I don't know where find that interpreter in the net...( Because I use one symbol function sqrt, not present in other Apl interpreters) So no trust in what I say... (for see if run ok one would have download Apl Nars, load the code , execute it) \$\endgroup\$ – RosLuP Feb 2 '18 at 11:41
  • \$\begingroup\$ So could u plz explain your solution? \$\endgroup\$ – mdahmoune Feb 2 '18 at 11:56
  • \$\begingroup\$ @mdahmoune done... I possibly make wrong something... \$\endgroup\$ – RosLuP Feb 2 '18 at 12:50
  • \$\begingroup\$ @mdahmoune because I make one error in understand the code, so I change the explanation where I make that error (sqrt( sum/2)) \$\endgroup\$ – RosLuP Feb 2 '18 at 13:00
0
\$\begingroup\$

SmileBASIC, 55 bytes

DEF R(T)SORT T
RETURN SQR(T[0]*T[0]+T[1]*T[1])==T[2]END
\$\endgroup\$
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ – mdahmoune Oct 23 '17 at 20:54
  • \$\begingroup\$ I don't think one exists, unfortunately. \$\endgroup\$ – 12Me21 Oct 23 '17 at 21:52
  • \$\begingroup\$ Could you PLZ tell us how can test your code? \$\endgroup\$ – mdahmoune Oct 25 '17 at 12:46
  • \$\begingroup\$ A quick search found this: en.m.wikipedia.org/wiki/Petit_Computer . I don't know if you can download it and run it on DSi emulator though. \$\endgroup\$ – Heimdall Nov 10 '17 at 8:52
  • \$\begingroup\$ It appears it defines a function R with argument T and assumes T to be a zero indexed array containing 3 numbers. SQR must be square root function. Unlike other BASICs this one uses C-like == rather than = for comparison. I guess I would just have to find a copy of the manual online to assess it as the code is really straightforward. \$\endgroup\$ – Heimdall Nov 10 '17 at 9:04
0
\$\begingroup\$

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)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 7 bytes

øUx²z q

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.