51
\$\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.
asked Oct 23, 2017 at 14:33
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Must we handle negative values or invalid edge triple? \$\endgroup\$ Commented Oct 23, 2017 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\$ Commented Oct 23, 2017 at 17:18
  • 2
    \$\begingroup\$ I think that using coordinates instead of lengths changes the challenge significantly \$\endgroup\$ Commented Oct 23, 2017 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\$ Commented Oct 23, 2017 at 22:31
  • 1
    \$\begingroup\$ @Kevin no you have not to handle this case. User202729 has already asked this question :) \$\endgroup\$ Commented Oct 24, 2017 at 6:44

85 Answers 85

1
2 3
41
\$\begingroup\$

Jelly, 5 bytes

2μSHe

Try it online!

Technical note: Bytes are counted in Jelly codepage.

Explanation:

2μSHe Main link.
2 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 a2 + b2 = c2. This is equivalent to whether (a2 + b2 + c2) ÷ 2 is one of a2, b2 or c2, so the program just checks that.

answered Oct 23, 2017 at 14:46
\$\endgroup\$
3
  • \$\begingroup\$ well... I jelly. \$\endgroup\$ Commented Oct 23, 2017 at 16:55
  • 1
    \$\begingroup\$ Just a technical note: symbols ² and µ cost two bytes each in UTF-8, so your code has actually 7 bytes, not 5 \$\endgroup\$ Commented Oct 26, 2017 at 8:15
  • 2
    \$\begingroup\$ @Charlie Answer edited for clarification. \$\endgroup\$ Commented Oct 26, 2017 at 10:35
22
\$\begingroup\$

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

answered Oct 23, 2017 at 14:53
\$\endgroup\$
8
  • \$\begingroup\$ Bah. Came up with the exact same answer. You could modify the test suite to allow for any number of test cases: see here \$\endgroup\$ Commented Oct 24, 2017 at 20:14
  • \$\begingroup\$ @mbomb007 exec(code) hmmm, why exec (code) instead of exec code? :D ;-p \$\endgroup\$ Commented Oct 25, 2017 at 11:47
  • \$\begingroup\$ Haha, how does this answer have double the upvotes of xnor's shorter one? Maybe people just like the sweet simpleness of it \$\endgroup\$ Commented Oct 25, 2017 at 12:23
  • 1
    \$\begingroup\$ @FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) \$\endgroup\$ Commented Oct 25, 2017 at 12:26
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 25, 2017 at 16:15
18
\$\begingroup\$

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)
answered Oct 23, 2017 at 14:52
\$\endgroup\$
3
  • \$\begingroup\$ Does it work without the parenthesis on the (c*=c)? The *= might have precidence over the == and you can save two bytes. \$\endgroup\$ Commented Oct 23, 2017 at 20:39
  • \$\begingroup\$ @corsiKa I'm afraid it's the other way around. == has precedence over *=. =, +=, *=, and similar assignments actually have the lowest precedence in Java operators. \$\endgroup\$ Commented Oct 24, 2017 at 6:43
  • \$\begingroup\$ 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... \$\endgroup\$ Commented Oct 24, 2017 at 7:56
14
\$\begingroup\$

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.

answered Oct 23, 2017 at 15:44
\$\endgroup\$
0
12
\$\begingroup\$

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

answered Oct 23, 2017 at 14:50
\$\endgroup\$
15
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 23, 2017 at 14:57
  • \$\begingroup\$ >>1 is unsafe, this returns true for [1, 1, 1]. \$\endgroup\$ Commented Oct 23, 2017 at 15:23
  • 2
    \$\begingroup\$ How about Math.hypot(...a,...a)==n*2? \$\endgroup\$ Commented Oct 23, 2017 at 15:26
  • \$\begingroup\$ @Neil Very nice fix :) \$\endgroup\$ Commented Oct 23, 2017 at 15:28
  • 2
    \$\begingroup\$ @Neil There should be a ~= operator for "rougly equal" ;) \$\endgroup\$ Commented Oct 24, 2017 at 14:12
9
\$\begingroup\$

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.
answered Oct 26, 2017 at 20:57
\$\endgroup\$
6
\$\begingroup\$

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

answered Oct 23, 2017 at 20:02
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to the site! This answer is only 32 bytes, maybe you counted an extra newline? \$\endgroup\$ Commented Oct 23, 2017 at 20:07
  • 3
    \$\begingroup\$ You can use the function Monad to save some more bytes here \$\endgroup\$ Commented Oct 23, 2017 at 20:15
  • \$\begingroup\$ also, the parentheses around sum can be thrown away. nice solution! \$\endgroup\$ Commented Oct 26, 2017 at 16:46
6
\$\begingroup\$

Perl 6, 24 bytes

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

Try it online!

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

answered Oct 23, 2017 at 20:45
\$\endgroup\$
6
\$\begingroup\$

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!

answered Oct 23, 2017 at 19:50
\$\endgroup\$
3
  • \$\begingroup\$ For your previous version, !sort(scan())^2%*%c(1,1,-1) is 27 bytes. but I think you still need a cat. \$\endgroup\$ Commented Oct 23, 2017 at 23:48
  • \$\begingroup\$ Cheers @Guiseppe, forgot about the cat. The rules around REPL annoy me, but they are what they are. \$\endgroup\$ Commented Oct 24, 2017 at 0:21
  • \$\begingroup\$ @Giuseppe Also nice twist with the matrix multiplication. I would never have come up with that. \$\endgroup\$ Commented Oct 24, 2017 at 0:27
6
\$\begingroup\$

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
answered Oct 24, 2017 at 1:38
\$\endgroup\$
5
\$\begingroup\$

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!

answered Oct 23, 2017 at 14:40
\$\endgroup\$
8
  • \$\begingroup\$ The challenge was updated to limit inputs to integers. \$\endgroup\$ Commented Oct 23, 2017 at 14:49
  • \$\begingroup\$ 77 bytes tio.run/##hY/BCoMwDIbvPkVgF@ti0dayMXCgPsbYQZ0yL1U6d/… \$\endgroup\$ Commented Oct 23, 2017 at 14:49
  • 14
    \$\begingroup\$ A*A is shorter... \$\endgroup\$ Commented Oct 23, 2017 at 15:03
  • \$\begingroup\$ 68 bytes tio.run/… \$\endgroup\$ Commented Oct 23, 2017 at 15:11
  • 1
    \$\begingroup\$ 41 bytes: lambda*x:sum(y*y for y in x)/2==max(x)**2 \$\endgroup\$ Commented Nov 15, 2017 at 22:23
5
\$\begingroup\$

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

answered Oct 24, 2017 at 9:37
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Oct 24, 2017 at 9:39
  • \$\begingroup\$ Does it always work if you're using bitwise operations? \$\endgroup\$ Commented May 25, 2019 at 2:25
5
\$\begingroup\$

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

answered Feb 2, 2018 at 10:35
\$\endgroup\$
4
\$\begingroup\$

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
answered Oct 23, 2017 at 15:22
\$\endgroup\$
4
\$\begingroup\$

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!

answered Oct 23, 2017 at 15:18
\$\endgroup\$
5
  • \$\begingroup\$ Outputs 1 for parameters of 1, 1, 1 which is wrong... \$\endgroup\$ Commented Oct 23, 2017 at 15:47
  • \$\begingroup\$ @Neil It's fixed now. \$\endgroup\$ Commented Oct 23, 2017 at 15:52
  • \$\begingroup\$ Question was updated to use ints, might save some bytes. \$\endgroup\$ Commented Oct 23, 2017 at 20:40
  • \$\begingroup\$ f(a,b,c){a=!((a*=a)+(b*=b)-(c*=c)&&a-b+c&&a-b-c);} \$\endgroup\$ Commented Oct 24, 2017 at 9:31
  • \$\begingroup\$ 52 bytes \$\endgroup\$ Commented Nov 22, 2019 at 21:31
4
\$\begingroup\$

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 a2 + b2 + c2.

2x.*x is the element-wise product of 2x and x, so it yields [2a2, 2b2, 2c2].

Finally, tests if the integer a2 + b2 + c2 belongs to the vector [2a2, 2b2, 2c2], which is true iff
a2 + b2 + c2 = 2a2 or a2 + b2 + c2 = 2b2 or a2 + b2 + c2 = 2c2, which itself is true iff
b2 + c2 = a2 or a2 + c2 = b2 or a2 + b2 = c2.

answered Oct 24, 2017 at 2:38
\$\endgroup\$
0
4
\$\begingroup\$

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!

answered Oct 23, 2017 at 15:28
\$\endgroup\$
1
  • \$\begingroup\$ that's awfully damn clever \$\endgroup\$ Commented Feb 2, 2018 at 17:53
4
\$\begingroup\$

Japt, 8 bytes

m2ø1⁄2*Ux2

Try it

Japt -x¡, 7 bytes

Ë2ѶUx2

Try it

answered Oct 23, 2017 at 17:13
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I like the square symbols in your solution ;) \$\endgroup\$ Commented Oct 23, 2017 at 20:11
4
\$\begingroup\$

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))
answered Oct 29, 2017 at 22:37
\$\endgroup\$
3
\$\begingroup\$

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.

answered Oct 23, 2017 at 14:57
\$\endgroup\$
3
\$\begingroup\$

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.

answered Oct 23, 2017 at 19:46
\$\endgroup\$
3
  • \$\begingroup\$ Is it better than sorting version? \$\endgroup\$ Commented Oct 25, 2017 at 12:47
  • \$\begingroup\$ @mdahmoune I believe it is better. I will try to solve it the other way around, but TBH I think that'd be longer. \$\endgroup\$ Commented Oct 25, 2017 at 12:55
  • \$\begingroup\$ Upvote any way :) \$\endgroup\$ Commented Oct 25, 2017 at 12:57
3
\$\begingroup\$

RProgN 2, 10 bytes

§22^r]‘\+e

Explained

§22^r]‘\+e
§ # Sort the input list
 22^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!

answered Oct 23, 2017 at 22:06
\$\endgroup\$
3
  • \$\begingroup\$ Why duplicate the list? \$\endgroup\$ Commented Oct 24, 2017 at 19:29
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 24, 2017 at 19:49
  • \$\begingroup\$ Thanx upvote ;) \$\endgroup\$ Commented Oct 24, 2017 at 19:52
3
\$\begingroup\$

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.
answered Oct 23, 2017 at 16:01
\$\endgroup\$
7
  • \$\begingroup\$ Awesome ;) but i think (define fun must be a part of the code... \$\endgroup\$ Commented Oct 23, 2017 at 16:03
  • \$\begingroup\$ 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.) \$\endgroup\$ Commented Oct 23, 2017 at 16:07
  • \$\begingroup\$ (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.) \$\endgroup\$ Commented Oct 23, 2017 at 16:08
  • 1
    \$\begingroup\$ Racket 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? \$\endgroup\$ Commented Oct 23, 2017 at 19:07
  • 2
    \$\begingroup\$ @MishaLavrov Then how about (*(expt(max a b c)2)2)? \$\endgroup\$ Commented Oct 23, 2017 at 21:25
3
\$\begingroup\$

05AB1E, 6 bytes

n{R`+Q

Try it online!

answered Oct 23, 2017 at 15:08
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 24, 2017 at 15:07
  • \$\begingroup\$ @NickLoughlin Oops, removed first example \$\endgroup\$ Commented Oct 24, 2017 at 17:36
  • \$\begingroup\$ You could do n{RÆ_ to save a byte. \$\endgroup\$ Commented Nov 29, 2017 at 11:39
  • \$\begingroup\$ @Emigna The _ could also be a >, since in this case they have the same effect - making a truthy output if the top of stack is 0 and falsy otherwise. Since 1 is the only truthy value in 05AB1E, > will increment 0 to 1 and output it; for other values, it will increment it and output it. Since any value other than 1 is falsy, it will be a falsy output. \$\endgroup\$ Commented Mar 3, 2021 at 21:30
3
\$\begingroup\$

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.

answered Oct 24, 2017 at 19:23
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 24, 2017 at 19:31
  • \$\begingroup\$ @iamnotmaynard It's pretty much the same thing. this was a funny coincidence lol. Thanks for letting me know \$\endgroup\$ Commented Oct 24, 2017 at 19:58
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ Commented Oct 25, 2017 at 15:46
3
\$\begingroup\$

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!

mdahmoune
3,0622 gold badges16 silver badges28 bronze badges
answered Oct 23, 2017 at 14:52
\$\endgroup\$
3
  • \$\begingroup\$ Can you save some bytes using currying rather than an array? \$\endgroup\$ Commented Oct 23, 2017 at 15:04
  • 1
    \$\begingroup\$ @AdmBorkBork Nope, because sort takes an array. \$\endgroup\$ Commented Oct 24, 2017 at 7:59
  • \$\begingroup\$ Suggest Math.hypot(a[0],a[1])==a[2] instead of a[0]*a[0]+a[1]*a[1]==a[2]*a[2] \$\endgroup\$ Commented Nov 22, 2019 at 20:46
3
\$\begingroup\$

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

answered Oct 24, 2017 at 10:59
\$\endgroup\$
5
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ Commented Oct 25, 2017 at 15:45
  • \$\begingroup\$ This only detects sorted right triangles: input of A=5, B=4, C=3 would not be correctly handled. \$\endgroup\$ Commented Oct 25, 2017 at 16:29
  • \$\begingroup\$ @MishaLavrov Thanks for pointing that out, it's actually shorter handling as a list. Now it works for inputs in any order. \$\endgroup\$ Commented Oct 25, 2017 at 22:06
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 25, 2017 at 22:24
  • \$\begingroup\$ @MishaLavrov That's interesting, I see what you mean. I think the expressions are equivalent for all non-negative inputs. \$\endgroup\$ Commented Oct 26, 2017 at 11:37
3
\$\begingroup\$

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
answered Oct 26, 2017 at 13:05
\$\endgroup\$
3
  • \$\begingroup\$ Some explanations ;) ? \$\endgroup\$ Commented Oct 26, 2017 at 14:41
  • \$\begingroup\$ @mdahmoune here you go \$\endgroup\$ Commented Oct 26, 2017 at 16:26
  • \$\begingroup\$ Dang it. Didn't you write that language? Doesn't seem fair. (joke) \$\endgroup\$ Commented Nov 1, 2017 at 18:03
3
\$\begingroup\$

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.

answered Oct 23, 2017 at 23:08
\$\endgroup\$
2
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$ Commented Oct 25, 2017 at 13:34
  • \$\begingroup\$ @mdahmoune You can use this tio.run link. However, it is much nicer to just install PARI/GP locally. \$\endgroup\$ Commented Oct 25, 2017 at 13:52
3
\$\begingroup\$

Ohm v2, (削除) 8 (削除ここまで) 6 bytes

2DS)Σε

Try it online!

answered Oct 23, 2017 at 15:38
\$\endgroup\$
1
2 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.