Is my triangle right?

47

4

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.

mdahmoune

Posted 2017-10-23T14:33:24.553

Reputation: 2 605

1Must we handle negative values or invalid edge triple? – user202729 – 2017-10-23T14:57:31.897

@user202729 No ;) – mdahmoune – 2017-10-23T14:59:07.980

2Very related. I'll leave it up to the rest of the community to decide if its a dup. – Digital Trauma – 2017-10-23T17:18:00.357

2I think that using coordinates instead of lengths changes the challenge significantly – Luis Mendo – 2017-10-23T18:24:13.737

8

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 – 2017-10-23T22:31:37.297

1@Kevin no you have not to handle this case. User202729 has already asked this question :) – mdahmoune – 2017-10-24T06:44:03.660

Answers

38

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.

user202729

Posted 2017-10-23T14:33:24.553

Reputation: 14 620

well... I jelly. – Félix Gagnon-Grenier – 2017-10-23T16:55:50.497

1Just a technical note: symbols ² and µ cost two bytes each in UTF-8, so your code has actually 7 bytes, not 5 – Charlie – 2017-10-26T08:15:54.520

2@Charlie Answer edited for clarification. – user202729 – 2017-10-26T10:35:29.187

20

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

Erik the Outgolfer

Posted 2017-10-23T14:33:24.553

Reputation: 38 134

Bah. Came up with the exact same answer. You could modify the test suite to allow for any number of test cases: see here

– mbomb007 – 2017-10-24T20:14:32.950

@mbomb007 exec(code) hmmm, why exec (code) instead of exec code? :D ;-p – Erik the Outgolfer – 2017-10-25T11:47:01.520

Haha, how does this answer have double the upvotes of xnor's shorter one? Maybe people just like the sweet simpleness of it – FlipTack – 2017-10-25T12:23:08.763

1@FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) – Erik the Outgolfer – 2017-10-25T12:26:36.860

@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. – mbomb007 – 2017-10-25T16:15:55.553

@mbomb007 I'd say that's not necessary, since this won't work in Python 3 anyways. – Erik the Outgolfer – 2017-10-25T16:45:46.980

You could use 1/(a*a+b*b!=c*c) to output by exit code. – FlipTack – 2017-10-30T20:24:13.267

Use - instead of != to save a byte. – Craig Gidney – 2017-11-09T22:12:18.700

17

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)

Kevin Cruijssen

Posted 2017-10-23T14:33:24.553

Reputation: 67 575

Does it work without the parenthesis on the (c*=c)? The *= might have precidence over the == and you can save two bytes. – corsiKa – 2017-10-23T20:39:02.017

@corsiKa I'm afraid it's the other way around. == has precedence over *=. =, +=, *=, and similar assignments actually have the lowest precedence in Java operators.

– Kevin Cruijssen – 2017-10-24T06:43:35.477

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... – Olivier Grégoire – 2017-10-24T07:56:15.920

12

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

Arnauld

Posted 2017-10-23T14:33:24.553

Reputation: 111 334

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. – Kevin Cruijssen – 2017-10-23T14:57:03.910

>>1 is unsafe, this returns true for [1, 1, 1]. – Neil – 2017-10-23T15:23:58.840

2How about Math.hypot(...a,...a)==n*2? – Neil – 2017-10-23T15:26:07.857

@Neil Very nice fix :) – Arnauld – 2017-10-23T15:28:55.297

@Neil I was trying to do Math.hypot(...a)**2==2*n*n and couldn't understand why it didn't work :( – JollyJoker – 2017-10-24T08:53:17.143

@JollyJoker Floating-point errors perhaps? – Neil – 2017-10-24T10:10:28.520

@Neil Yes, I logged Math.hypot(...a)**2-2*n*n and get errors of order 10^-15. hypot(...a,...a) seems to get around it, likely because the (desired) result is always an integer. – JollyJoker – 2017-10-24T10:37:08.120

@JollyJoker Indeed, it's always an integer for Pythagorean triples, which is how I came up with the idea! – Neil – 2017-10-24T12:48:28.777

2@Neil There should be a ~= operator for "rougly equal" ;) – JollyJoker – 2017-10-24T14:12:53.917

a=>Math.hypot(...a,...a)==a.sort()[2]*2 – l4m2 – 2017-12-26T10:41:54.240

@l4m2 This would fail for instance for [10,6,8], because sort() uses lexicographical order by default. – Arnauld – 2017-12-26T10:50:52.250

Checked. And thus the solution using sort are also invalid

– l4m2 – 2017-12-26T11:02:35.007

@l4m2 Oh, I didn't notice that one. But it would fail indeed on such test cases. – Arnauld – 2017-12-26T11:05:45.173

Builtin function way and recursive way exactly both 40c

– l4m2 – 2017-12-26T11:08:37.923

12

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.

xnor

Posted 2017-10-23T14:33:24.553

Reputation: 115 687

7

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<>/

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.
  • $:* 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
  • 0=% test equality to zero and output result.

MickyT

Posted 2017-10-23T14:33:24.553

Reputation: 11 735

6

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.

Try it online!

MickyT

Posted 2017-10-23T14:33:24.553

Reputation: 11 735

For your previous version, !sort(scan())^2%*%c(1,1,-1) is 27 bytes. but I think you still need a cat. – Giuseppe – 2017-10-23T23:48:30.210

Cheers @Guiseppe, forgot about the cat. The rules around REPL annoy me, but they are what they are. – MickyT – 2017-10-24T00:21:44.927

@Giuseppe Also nice twist with the matrix multiplication. I would never have come up with that. – MickyT – 2017-10-24T00:27:48.903

6

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

butterdogs

Posted 2017-10-23T14:33:24.553

Reputation: 101

1Welcome to the site! This answer is only 32 bytes, maybe you counted an extra newline? – H.PWiz – 2017-10-23T20:07:24.563

3

You can use the function Monad to save some more bytes here

– H.PWiz – 2017-10-23T20:15:52.687

also, the parentheses around sum can be thrown away. nice solution! – proud haskeller – 2017-10-26T16:46:57.853

6

Perl 6, 24 bytes

{(*²+*²==*²)(|.sort)}

Try it online!

*²+*²==*² 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 |.

Sean

Posted 2017-10-23T14:33:24.553

Reputation: 4 136

6

Brain-Flak, 68 bytes

({({({})({}[()])}{}<>)<>})<>({<(({}){}<>[({})])>(){[()](<{}>)}{}<>})

Try it online!

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

Nitrodon

Posted 2017-10-23T14:33:24.553

Reputation: 9 181

5

C (gcc), 49 bytes

n(a,b,c){return(a*=a)+(b*=b)-(c*=c)&a+c-b&b+c-a;}

Try it online!

Improves on Kevin Cruijssens technique

Returns 0 for a valid triangle, and a non-zero value otherwise

Popeko

Posted 2017-10-23T14:33:24.553

Reputation: 51

3Welcome to PPCG! – caird coinheringaahing – 2017-10-24T09:39:57.463

Does it always work if you're using bitwise operations? – l4m2 – 2019-05-25T02:25:45.470

4

Python 2, 43 bytes

lambda a,b,c:(a*a+b*b+c*c)/2in(a*a,b*b,c*c)

Try it online!

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

Try it online!

TFeld

Posted 2017-10-23T14:33:24.553

Reputation: 19 246

The challenge was updated to limit inputs to integers. – Martin Ender – 2017-10-23T14:49:20.583

14A*A is shorter... – Socratic Phoenix – 2017-10-23T15:03:10.423

@mdahmoune 67 bytes; flipping the truth value's meanings and using - instead of ==.

– Jonathan Frech – 2017-10-23T16:04:34.203

@JonathanFrech you right :-) – mdahmoune – 2017-10-23T16:05:19.643

43 bytes: lambda a,b,c:(a*a+b*b+c*c)/2in(a*a,b*b,c*c). – imallett – 2017-10-24T04:50:33.480

41 bytes: lambda*x:sum(y*y for y in x)/2==max(x)**2 – Alex Varga – 2017-11-15T22:23:19.863

4

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!

Try it online!

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;}

Try it online!

Steadybox

Posted 2017-10-23T14:33:24.553

Reputation: 15 798

Outputs 1 for parameters of 1, 1, 1 which is wrong... – Neil – 2017-10-23T15:47:06.170

@Neil It's fixed now. – Steadybox – 2017-10-23T15:52:33.650

Question was updated to use ints, might save some bytes. – corsiKa – 2017-10-23T20:40:55.763

f(a,b,c){a=!((a*=a)+(b*=b)-(c*=c)&&a-b+c&&a-b-c);} – Christoph – 2017-10-24T09:31:00.137

52 bytes – ceilingcat – 2019-11-22T21:31:55.703

4

MATL, 7 bytes

SU&0)s=

Try it online!

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

Luis Mendo

Posted 2017-10-23T14:33:24.553

Reputation: 87 464

4

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 {:

Try it online!

Jonah

Posted 2017-10-23T14:33:24.553

Reputation: 8 729

that's awfully damn clever – Jonah – 2018-02-02T17:53:17.647

4

Japt, 8 bytes

Takes input as an array.

m²
ø½*Ux

Try it

Shaggy

Posted 2017-10-23T14:33:24.553

Reputation: 24 623

1I like the square symbols in your solution ;) – mdahmoune – 2017-10-23T20:11:31.537

4

Triangularity,  49  31 bytes

...)...
..IEO..
.M)2s^.
}Re+=..

Try it online!

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

Mr. Xcoder

Posted 2017-10-23T14:33:24.553

Reputation: 39 774

3

Java (OpenJDK 8), 68 bytes

a->{java.util.Arrays.sort(a);return a[0]*a[0]+a[1]*a[1]==a[2]*a[2];}

Try it online!

Roberto Graham

Posted 2017-10-23T14:33:24.553

Reputation: 1 305

Can you save some bytes using currying rather than an array? – AdmBorkBork – 2017-10-23T15:04:37.523

1@AdmBorkBork Nope, because sort takes an array. – Olivier Grégoire – 2017-10-24T07:59:16.267

Suggest Math.hypot(a[0],a[1])==a[2] instead of a[0]*a[0]+a[1]*a[1]==a[2]*a[2] – ceilingcat – 2019-11-22T20:46:58.393

3

PowerShell, 39 bytes

$a,$b,$c=$args|sort;$a*$a+$b*$b-eq$c*$c

Try it online!

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.

AdmBorkBork

Posted 2017-10-23T14:33:24.553

Reputation: 41 581

3

05AB1E, 6 bytes

n{R`+Q

Try it online!

Okx

Posted 2017-10-23T14:33:24.553

Reputation: 15 025

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. – Nick Loughlin – 2017-10-24T15:07:21.637

@NickLoughlin Oops, removed first example – Okx – 2017-10-24T17:36:21.407

You could do n{RÆ_ to save a byte. – Emigna – 2017-11-29T11:39:46.857

3

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

Nahuel Fouilleul

Posted 2017-10-23T14:33:24.553

Reputation: 5 582

This can cut down to 35 + 2 by eliminating the @F assignment: Try it online!

– Xcali – 2018-02-02T17:46:44.227

@Xcali, thanks, and even 34 +2, editing – Nahuel Fouilleul – 2018-02-03T07:45:55.150

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 – 2018-06-16T19:11:03.400

thanks Erik seems the rules have changed since i've posted, don't hesitate to update – Nahuel Fouilleul – 2018-06-17T18:36:56.313

3

Ohm v2, 8 6 bytes

²DS)Σε

Try it online!

Cinaski

Posted 2017-10-23T14:33:24.553

Reputation: 1 588

3

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.

Misha Lavrov

Posted 2017-10-23T14:33:24.553

Reputation: 4 846

Awesome ;) but i think (define fun must be a part of the code... – mdahmoune – 2017-10-23T16:03:18.983

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.) – Misha Lavrov – 2017-10-23T16:07:52.917

(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.) – Misha Lavrov – 2017-10-23T16:08:46.267

1Racket 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 – 2017-10-23T19:07:01.267

I tried some variants along those lines and none were shorter; I didn't try this one, but it looks like ((λ(x)(* x x 2))(max a b c)) doesn't beat (*(max a b c)(max a b c)2) either. There is an exponentiation built-in, but (expt x 2) is worse than (* x x). – Misha Lavrov – 2017-10-23T20:44:04.060

2@MishaLavrov Then how about (*(expt(max a b c)2)2)? – xnor – 2017-10-23T21:25:17.893

Can't believe I missed that one. Thanks! – Misha Lavrov – 2017-10-23T22:22:31.250

3

JavaScript 34 bytes (without D=)

D=(d,[a,b,c]=d.sort())=>a*a+b*b==c*c

console.log(D([5, 3, 4       ])== true)
console.log(D([3, 5, 4       ])== true)
console.log(D([12, 37, 35    ])== true)
console.log(D([21, 38, 5     ])== false)
console.log(D([210, 308, 15  ])== false)

DanielIndie

Posted 2017-10-23T14:33:24.553

Reputation: 1 220

I had a similar answer at 34: a=>a.sort()[0]**2+a[1]**2==a[2]**2 in ES6. So props to you @DanielIndie – WallyWest – 2017-10-23T22:37:35.497

1Unfortunately, sort() uses the lexicographical order when no callback is provided, making this code fail for instance for [10,6,8]. – Arnauld – 2017-12-26T11:10:10.777

3

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!

ATaco

Posted 2017-10-23T14:33:24.553

Reputation: 7 898

Why duplicate the list? – mdahmoune – 2017-10-24T19:29:03.650

@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. – ATaco – 2017-10-24T19:49:47.597

Thanx upvote ;) – mdahmoune – 2017-10-24T19:52:49.180

3

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

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.

Jeppe Stig Nielsen

Posted 2017-10-23T14:33:24.553

Reputation: 602

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-25T13:34:20.230

@mdahmoune You can use this tio.run link. However, it is much nicer to just install PARI/GP locally.

– Jeppe Stig Nielsen – 2017-10-25T13:52:00.287

3

Julia 0.6, 16 bytes

!x=x⋅x∈2x.*x

Try it online!

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

Dennis

Posted 2017-10-23T14:33:24.553

Reputation: 196 637

3

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

Timtech

Posted 2017-10-23T14:33:24.553

Reputation: 12 038

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-25T15:45:35.020

This only detects sorted right triangles: input of A=5, B=4, C=3 would not be correctly handled. – Misha Lavrov – 2017-10-25T16:29:59.703

@MishaLavrov Thanks for pointing that out, it's actually shorter handling as a list. Now it works for inputs in any order. – Timtech – 2017-10-25T22:06:34.870

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. – Misha Lavrov – 2017-10-25T22:24:43.337

@MishaLavrov That's interesting, I see what you mean. I think the expressions are equivalent for all non-negative inputs. – Timtech – 2017-10-26T11:37:18.367

3

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.

dkudriavtsev

Posted 2017-10-23T14:33:24.553

Reputation: 5 781

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. – Reinstate Monica -- notmaynard – 2017-10-24T19:31:37.713

@iamnotmaynard It's pretty much the same thing. this was a funny coincidence lol. Thanks for letting me know – dkudriavtsev – 2017-10-24T19:58:18.793

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-25T15:46:25.010

3

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

aditsu quit because SE is EVIL

Posted 2017-10-23T14:33:24.553

Reputation: 22 326

Some explanations ;) ? – mdahmoune – 2017-10-26T14:41:18.500

@mdahmoune here you go – aditsu quit because SE is EVIL – 2017-10-26T16:26:51.023

Dang it. Didn't you write that language? Doesn't seem fair. (joke) – kaine – 2017-11-01T18:03:51.127

3

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.

Renzo

Posted 2017-10-23T14:33:24.553

Reputation: 2 260

3

MS Excel, 49 Bytes

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

=OR(A1^2+B1^2=C1^2,B1^2+C1^2=A1^2,A1^2+C1^2=B1^2)

Taylor Scott

Posted 2017-10-23T14:33:24.553

Reputation: 6 709

3

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!

l4m2

Posted 2017-10-23T14:33:24.553

Reputation: 5 985

That's a nice approach! – Arnauld – 2017-12-26T11:06:55.110

3

Excel, 47 bytes

Returns FALSE for right-angled Triangles, else TRUE:

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

Wernisch

Posted 2017-10-23T14:33:24.553

Reputation: 2 534

1Now that is clever – Taylor Scott – 2018-06-26T12:08:17.867

2

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

HyperNeutrino

Posted 2017-10-23T14:33:24.553

Reputation: 26 575

2

Neim, 6 bytes

ᛦDᚺS

Try it online!

Okx

Posted 2017-10-23T14:33:24.553

Reputation: 15 025

Could you plz add some explanations? – mdahmoune – 2017-10-25T15:54:48.567

@mdahmoune You can activate debug mode to see what's going on.

– Okx – 2017-10-25T15:56:18.483

2

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.

J. Sallé

Posted 2017-10-23T14:33:24.553

Reputation: 3 233

You should probably update the code on the top, it still has those parens around ⍵*2 – Zacharý – 2017-10-23T19:57:42.583

(⍵*2)÷2 can be golfed to 2÷⍨⍵*2. – Adám – 2017-10-24T08:25:38.700

2

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])")}

AnonymousReality

Posted 2017-10-23T14:33:24.553

Reputation: 41

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.

Neil

Posted 2017-10-23T14:33:24.553

Reputation: 95 035

2

Pyth, 14 bytes

K_m*ddSQqstKhK

Try it online!

Ian H.

Posted 2017-10-23T14:33:24.553

Reputation: 2 431

2

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.

sergiol

Posted 2017-10-23T14:33:24.553

Reputation: 3 055

You think it can be more golfed? – mdahmoune – 2017-10-25T12:42:17.993

Failed outgolf: proc R L {expr hypot([lindex [set L [lsort $L]] 0],[lindex $L 1])==[lindex $L 2]}

– sergiol – 2017-10-26T00:15:34.150

Another fail: proc R L {expr hypot([[set I lindex] [set L [lsort $L]] 0],[$I $L 1])==[$I $L 2]}

– sergiol – 2017-10-26T00:20:01.633

Thanx for the additional less golfed solutions, could you add them to the answer plz? – mdahmoune – 2017-10-26T09:46:34.987

I already tried to understand some solutions posted on other answers, and it happens, I will add more golfy solutions to main answer using the same approach. I will not publish the fails on the main answer as they are more recent and less golfy; and are also clickable links on the comments to working online demos. – sergiol – 2017-10-26T17:19:40.990

2

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.

brhfl

Posted 2017-10-23T14:33:24.553

Reputation: 1 291

2

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

L3viathan

Posted 2017-10-23T14:33:24.553

Reputation: 3 151

10 bytes: }/sK^R2Q2K or /K^R2Q/sK2 (the former returns True/False, the latter returns 1/0) – Mr. Xcoder – 2017-10-23T19:35:33.050

2

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);}

Blue Okiris

Posted 2017-10-23T14:33:24.553

Reputation: 157

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 – 2017-10-23T20:05:48.643

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-23T20:53:52.140

2

Gaia, 6 bytes

s¦ΣḥuĖ

Try it online!

  • - square each.

  • Σ - sum.

  • - alve.

  • u - square root.

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

Mr. Xcoder

Posted 2017-10-23T14:33:24.553

Reputation: 39 774

Is it better than sorting version? – mdahmoune – 2017-10-25T12:47:40.060

@mdahmoune I believe it is better. I will try to solve it the other way around, but TBH I think that'd be longer. – Mr. Xcoder – 2017-10-25T12:55:44.320

Upvote any way :) – mdahmoune – 2017-10-25T12:57:29.407

2

Racket, 109 bytes

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

Try it online!

sdfbhg

Posted 2017-10-23T14:33:24.553

Reputation: 21

2It seems not working for 5 3 4? – mdahmoune – 2017-10-23T20:51:05.367

2

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 – 2017-10-24T01:08:12.353

1@user202729 While valid to point out, that may not be the most helpful way to address a new user. – Misha Lavrov – 2017-10-24T17:18:05.697

@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 – 2017-10-24T17:18:28.093

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 – 2017-10-24T17:20:36.993

2

Dyalog APL, 12 bytes

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

Uses the same method as this Jelly answer.

Try it online!

Zacharý

Posted 2017-10-23T14:33:24.553

Reputation: 5 710

2

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

Zacharý

Posted 2017-10-23T14:33:24.553

Reputation: 5 710

You can ask Dennis to pull it yourself here or by contacting him at feedback@tryitonline.net

– caird coinheringaahing – 2017-10-24T10:44:08.440

Is it possible to avoid computing ω^2 twice? – mdahmoune – 2017-10-25T13:33:42.283

Maybe somehow, but MY doesn't have a duplicate command yet. – Zacharý – 2017-10-25T16:08:27.623

2

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

kaine

Posted 2017-10-23T14:33:24.553

Reputation: 536

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-23T20:53:37.620

@mdahmoune sure. done. – kaine – 2017-10-23T20:57:07.480

2

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!

Riking

Posted 2017-10-23T14:33:24.553

Reputation: 249

Is it possible to do it without sorting? – mdahmoune – 2017-10-25T13:34:57.613

I tried to do some stuff with complex numbers, but they were all higher character count. import "math/cmplx";cmplx.Abs() – Riking – 2017-10-26T05:17:12.550

You can drop the space between import and "sort" – mdahmoune – 2017-10-26T07:10:45.577

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.

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!

jq170727

Posted 2017-10-23T14:33:24.553

Reputation: 411

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 – 2017-10-25T13:50:19.743

Indeed that's a better approach. Thanks! – jq170727 – 2017-10-25T15:35:02.957

Could you plz add also the first solution too :) – mdahmoune – 2017-10-25T15:42:03.747

Done! See also original edit

– jq170727 – 2017-10-25T15:55:42.340

2

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

Adám

Posted 2017-10-23T14:33:24.553

Reputation: 37 779

2

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)
        ;
}

Toby Speight

Posted 2017-10-23T14:33:24.553

Reputation: 5 058

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-25T13:50:55.000

I've provided a main() - anyone can try the code! It's all standard C... – Toby Speight – 2017-10-25T16:28:01.567

2

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

FlipTack

Posted 2017-10-23T14:33:24.553

Reputation: 13 242

Could you plz add the first solution too? – mdahmoune – 2017-10-26T09:28:59.047

@mdahmoune done. – FlipTack – 2017-10-27T15:12:01.710

2

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.

Swift Sandbox.

xoudini

Posted 2017-10-23T14:33:24.553

Reputation: 161

2

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

Bass

Posted 2017-10-23T14:33:24.553

Reputation: 151

Could you add some explanations? – mdahmoune – 2017-10-26T09:48:32.703

Sure. There you go. – Bass – 2017-10-26T10:39:26.523

2

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];};

Abbas

Posted 2017-10-23T14:33:24.553

Reputation: 349

2

Add++, 25 18 bytes

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]

caird coinheringaahing

Posted 2017-10-23T14:33:24.553

Reputation: 13 702

Why not push the input twice and reduce by multiplication? – mdahmoune – 2017-10-24T22:20:20.270

@mdahmoune because I over complicate things. Golfing now – caird coinheringaahing – 2017-10-24T22:23:51.037

Upvote ;) please can you put the first solution too, it’s interesting – mdahmoune – 2017-10-24T22:45:24.753

@mdahmoune added! – caird coinheringaahing – 2017-10-24T22:51:56.860

2

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.

Esolanging Fruit

Posted 2017-10-23T14:33:24.553

Reputation: 13 542

I like the swap trick ;) – mdahmoune – 2017-10-24T22:26:59.517

Is it possible to add a link to test the solution please? – mdahmoune – 2017-10-24T22:28:05.880

@mdahmoune TIO links added, thanks. – Esolanging Fruit – 2017-10-25T02:33:44.867

@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 – 2017-10-30T17:46:58.437

@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 – 2017-10-30T23:49:45.010

Ok. Thanks. That means i can make mine 10 by using those rules. The brackets, however are required. – kaine – 2017-10-30T23:52:27.777

@kaine Yes, that's true. – Esolanging Fruit – 2017-10-31T00:13:32.287

2

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.

Conor O'Brien

Posted 2017-10-23T14:33:24.553

Reputation: 36 228

Like the :* part ;) – mdahmoune – 2017-10-26T09:50:40.593

2

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.

IEatBagels

Posted 2017-10-23T14:33:24.553

Reputation: 189

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 – 2017-10-26T10:55:33.933

2

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)

RosLuP

Posted 2017-10-23T14:33:24.553

Reputation: 3 036

2

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!

mudkip201

Posted 2017-10-23T14:33:24.553

Reputation: 833

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2018-02-02T11:02:49.070

Back when I posted this answer, it wasn't on TIO – mudkip201 – 2018-02-02T13:13:10.733

1

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

DavidC

Posted 2017-10-23T14:33:24.553

Reputation: 24 524

Can it be done as a function? – mdahmoune – 2017-10-23T15:39:50.047

It is a pure function, employed as if it were a single word. – DavidC – 2017-10-23T20:12:02.040

13 bytes shorter #&@@(z=Sort@#)^2+z[[2]]^2==z[[3]]^2& – Keyu Gan – 2017-10-23T23:07:38.293

524 bytes: #^2+#2^2==#3^2&@@Sort@#& – J42161217 – 2017-10-24T16:50:08.903

1

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!

MCAdventure10

Posted 2017-10-23T14:33:24.553

Reputation: 103

PLZ could you add the two solutions? – mdahmoune – 2017-10-26T14:40:25.167

Can be golfed with function f(...)t={...} – ATaco – 2017-10-29T22:24:58.140

2

Are those not 66 bytes?

– Jonathan Frech – 2017-10-30T00:41:12.853

1

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.

Calimero

Posted 2017-10-23T14:33:24.553

Reputation: 131

If it is acceptable to emit empty string for false, you can shave the four character ternary. – Umbrella – 2018-06-19T21:34:43.090

1

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

Taylor Scott

Posted 2017-10-23T14:33:24.553

Reputation: 6 709

1

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

Tornado547

Posted 2017-10-23T14:33:24.553

Reputation: 389

1

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}

RosLuP

Posted 2017-10-23T14:33:24.553

Reputation: 3 036

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2018-02-02T11:02:28.337

@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) – RosLuP – 2018-02-02T11:41:43.123

So could u plz explain your solution? – mdahmoune – 2018-02-02T11:56:03.660

@mdahmoune done... I possibly make wrong something... – RosLuP – 2018-02-02T12:50:59.760

@mdahmoune because I make one error in understand the code, so I change the explanation where I make that error (sqrt( sum/2)) – RosLuP – 2018-02-02T13:00:26.507

This is 28 bytes. – Erik the Outgolfer – 2018-06-16T19:11:58.510

1

05AB1E, 5 bytes

à‚nOË

Try it online!

Erik the Outgolfer

Posted 2017-10-23T14:33:24.553

Reputation: 38 134

some explanations plz :)? – mdahmoune – 2018-02-02T13:33:10.103

nàsOQ is another. Not thinking it's possible below 5. – Magic Octopus Urn – 2018-02-02T15:23:43.570

1

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

sundar - Reinstate Monica

Posted 2017-10-23T14:33:24.553

Reputation: 5 296

1

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.

Umbrella

Posted 2017-10-23T14:33:24.553

Reputation: 867

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. – Umbrella – 2018-06-19T21:52:14.020

1

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.

Umbrella

Posted 2017-10-23T14:33:24.553

Reputation: 867

Making a shorthand function seemed shorter than a console.log(). – Umbrella – 2018-06-19T21:52:50.323

0

SmileBASIC, 55 bytes

DEF R(T)SORT T
RETURN SQR(T[0]*T[0]+T[1]*T[1])==T[2]END

12Me21

Posted 2017-10-23T14:33:24.553

Reputation: 6 110

If possible, please include a link to an online testing environment so other people can try out your code! – mdahmoune – 2017-10-23T20:54:00.677

I don't think one exists, unfortunately. – 12Me21 – 2017-10-23T21:52:26.993

Could you PLZ tell us how can test your code? – mdahmoune – 2017-10-25T12:46:22.250

A quick search found this: https://en.m.wikipedia.org/wiki/Petit_Computer . I don't know if you can download it and run it on DSi emulator though.

– Heimdall – 2017-11-10T08:52:26.077

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. – Heimdall – 2017-11-10T09:04:15.317

0

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!

Alex Allen

Posted 2017-10-23T14:33:24.553

Reputation: 91

0

Japt, 7 bytes

øUx²z q

Try it online!

Oliver

Posted 2017-10-23T14:33:24.553

Reputation: 7 160