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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
85 Answers 85
Jelly, 5 bytes
2μSHe
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.
-
\$\begingroup\$ well... I jelly. \$\endgroup\$Félix Gagnon-Grenier– Félix Gagnon-Grenier2017年10月23日 16:55:50 +00:00Commented 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\$Charlie– Charlie2017年10月26日 08:15:54 +00:00Commented Oct 26, 2017 at 8:15 -
2\$\begingroup\$ @Charlie Answer edited for clarification. \$\endgroup\$user202729– user2027292017年10月26日 10:35:29 +00:00Commented Oct 26, 2017 at 10:35
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).
-
-
\$\begingroup\$ @mbomb007
exec(code)hmmm, whyexec (code)instead ofexec code? :D ;-p \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月25日 11:47:01 +00:00Commented 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\$FlipTack– FlipTack2017年10月25日 12:23:08 +00:00Commented Oct 25, 2017 at 12:23
-
1\$\begingroup\$ @FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月25日 12:26:36 +00:00Commented 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\$mbomb007– mbomb0072017年10月25日 16:15:55 +00:00Commented Oct 25, 2017 at 16:15
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)
-
\$\begingroup\$ Does it work without the parenthesis on the
(c*=c)? The*=might have precidence over the==and you can save two bytes. \$\endgroup\$corsiKa– corsiKa2017年10月23日 20:39:02 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2017年10月24日 06:43:35 +00:00Commented 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\$Olivier Grégoire– Olivier Grégoire2017年10月24日 07:56:15 +00:00Commented Oct 24, 2017 at 7:56
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
-
\$\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\$Kevin Cruijssen– Kevin Cruijssen2017年10月23日 14:57:03 +00:00Commented Oct 23, 2017 at 14:57 -
\$\begingroup\$
>>1is unsafe, this returns true for[1, 1, 1]. \$\endgroup\$Neil– Neil2017年10月23日 15:23:58 +00:00Commented Oct 23, 2017 at 15:23 -
2\$\begingroup\$ How about
Math.hypot(...a,...a)==n*2? \$\endgroup\$Neil– Neil2017年10月23日 15:26:07 +00:00Commented Oct 23, 2017 at 15:26 -
\$\begingroup\$ @Neil Very nice fix :) \$\endgroup\$Arnauld– Arnauld2017年10月23日 15:28:55 +00:00Commented Oct 23, 2017 at 15:28
-
2\$\begingroup\$ @Neil There should be a
~=operator for "rougly equal" ;) \$\endgroup\$JollyJoker– JollyJoker2017年10月24日 14:12:53 +00:00Commented Oct 24, 2017 at 14:12
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.
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
-
1\$\begingroup\$ Welcome to the site! This answer is only 32 bytes, maybe you counted an extra newline? \$\endgroup\$H.PWiz– H.PWiz2017年10月23日 20:07:24 +00:00Commented Oct 23, 2017 at 20:07
-
3
-
\$\begingroup\$ also, the parentheses around
sumcan be thrown away. nice solution! \$\endgroup\$proud haskeller– proud haskeller2017年10月26日 16:46:57 +00:00Commented Oct 26, 2017 at 16:46
Perl 6, 24 bytes
{(*2+*2==*2)(|.sort)}
*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 |.
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.
-
\$\begingroup\$ For your previous version,
!sort(scan())^2%*%c(1,1,-1)is 27 bytes. but I think you still need acat. \$\endgroup\$Giuseppe– Giuseppe2017年10月23日 23:48:30 +00:00Commented 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\$MickyT– MickyT2017年10月24日 00:21:44 +00:00Commented Oct 24, 2017 at 0:21
-
\$\begingroup\$ @Giuseppe Also nice twist with the matrix multiplication. I would never have come up with that. \$\endgroup\$MickyT– MickyT2017年10月24日 00:27:48 +00:00Commented Oct 24, 2017 at 0:27
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
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))
-
\$\begingroup\$ The challenge was updated to limit inputs to integers. \$\endgroup\$Martin Ender– Martin Ender2017年10月23日 14:49:20 +00:00Commented Oct 23, 2017 at 14:49
-
\$\begingroup\$ 77 bytes tio.run/##hY/BCoMwDIbvPkVgF@ti0dayMXCgPsbYQZ0yL1U6d/… \$\endgroup\$mdahmoune– mdahmoune2017年10月23日 14:49:22 +00:00Commented Oct 23, 2017 at 14:49
-
14\$\begingroup\$
A*Ais shorter... \$\endgroup\$Socratic Phoenix– Socratic Phoenix2017年10月23日 15:03:10 +00:00Commented Oct 23, 2017 at 15:03 -
-
1\$\begingroup\$ 41 bytes:
lambda*x:sum(y*y for y in x)/2==max(x)**2\$\endgroup\$Alex Varga– Alex Varga2017年11月15日 22:23:19 +00:00Commented Nov 15, 2017 at 22:23
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
-
3\$\begingroup\$ Welcome to PPCG! \$\endgroup\$2017年10月24日 09:39:57 +00:00Commented Oct 24, 2017 at 9:39
-
\$\begingroup\$ Does it always work if you're using bitwise operations? \$\endgroup\$l4m2– l4m22019年05月25日 02:25:45 +00:00Commented May 25, 2019 at 2:25
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...
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
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;}
-
\$\begingroup\$ Outputs
1for parameters of1, 1, 1which is wrong... \$\endgroup\$Neil– Neil2017年10月23日 15:47:06 +00:00Commented Oct 23, 2017 at 15:47 -
\$\begingroup\$ @Neil It's fixed now. \$\endgroup\$Steadybox– Steadybox2017年10月23日 15:52:33 +00:00Commented Oct 23, 2017 at 15:52
-
\$\begingroup\$ Question was updated to use ints, might save some bytes. \$\endgroup\$corsiKa– corsiKa2017年10月23日 20:40:55 +00:00Commented 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\$Christoph– Christoph2017年10月24日 09:31:00 +00:00Commented Oct 24, 2017 at 9:31 -
\$\begingroup\$ 52 bytes \$\endgroup\$ceilingcat– ceilingcat2019年11月22日 21:31:55 +00:00Commented Nov 22, 2019 at 21:31
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 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.
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 {:
-
\$\begingroup\$ that's awfully damn clever \$\endgroup\$Jonah– Jonah2018年02月02日 17:53:17 +00:00Commented Feb 2, 2018 at 17:53
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))
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.
Gaia, 6 bytes
s¦ΣḥuĖ
s¦- square each.Σ- sum.ḥ- ḥalve.u- square root.Ė- contains? Check if the square root is in the input.
-
\$\begingroup\$ Is it better than sorting version? \$\endgroup\$mdahmoune– mdahmoune2017年10月25日 12:47:40 +00:00Commented 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\$Mr. Xcoder– Mr. Xcoder2017年10月25日 12:55:44 +00:00Commented Oct 25, 2017 at 12:55
-
\$\begingroup\$ Upvote any way :) \$\endgroup\$mdahmoune– mdahmoune2017年10月25日 12:57:29 +00:00Commented Oct 25, 2017 at 12:57
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?
-
\$\begingroup\$ Why duplicate the list? \$\endgroup\$mdahmoune– mdahmoune2017年10月24日 19:29:03 +00:00Commented 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\$ATaco– ATaco2017年10月24日 19:49:47 +00:00Commented Oct 24, 2017 at 19:49
-
\$\begingroup\$ Thanx upvote ;) \$\endgroup\$mdahmoune– mdahmoune2017年10月24日 19:52:49 +00:00Commented Oct 24, 2017 at 19:52
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.
-
\$\begingroup\$ Awesome ;) but i think
(define funmust be a part of the code... \$\endgroup\$mdahmoune– mdahmoune2017年10月23日 16:03:18 +00:00Commented 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 of3 4 5))if you prefer.) \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月23日 16:07:52 +00:00Commented 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 racketin the code; some haven't.) \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月23日 16:08:46 +00:00Commented 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 aletbinding, 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\$xnor– xnor2017年10月23日 19:07:01 +00:00Commented Oct 23, 2017 at 19:07 -
2\$\begingroup\$ @MishaLavrov Then how about
(*(expt(max a b c)2)2)? \$\endgroup\$xnor– xnor2017年10月23日 21:25:17 +00:00Commented Oct 23, 2017 at 21:25
-
\$\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\$Nick Loughlin– Nick Loughlin2017年10月24日 15:07:21 +00:00Commented Oct 24, 2017 at 15:07
-
\$\begingroup\$ @NickLoughlin Oops, removed first example \$\endgroup\$Okx– Okx2017年10月24日 17:36:21 +00:00Commented Oct 24, 2017 at 17:36
-
\$\begingroup\$ You could do
n{RÆ_to save a byte. \$\endgroup\$Emigna– Emigna2017年11月29日 11:39:46 +00:00Commented 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 is0and falsy otherwise. Since1is the only truthy value in 05AB1E,>will increment0to1and output it; for other values, it will increment it and output it. Since any value other than1is falsy, it will be a falsy output. \$\endgroup\$Makonede– Makonede2021年03月03日 21:30:42 +00:00Commented Mar 3, 2021 at 21:30
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.
-
\$\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\$Reinstate Monica -- notmaynard– Reinstate Monica -- notmaynard2017年10月24日 19:31:37 +00:00Commented 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\$anna328p– anna328p2017年10月24日 19:58:18 +00:00Commented 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\$mdahmoune– mdahmoune2017年10月25日 15:46:25 +00:00Commented Oct 25, 2017 at 15:46
Java (OpenJDK 8), 68 bytes
a->{java.util.Arrays.sort(a);return a[0]*a[0]+a[1]*a[1]==a[2]*a[2];}
-
\$\begingroup\$ Can you save some bytes using currying rather than an array? \$\endgroup\$AdmBorkBork– AdmBorkBork2017年10月23日 15:04:37 +00:00Commented Oct 23, 2017 at 15:04
-
1\$\begingroup\$ @AdmBorkBork Nope, because
sorttakes an array. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年10月24日 07:59:16 +00:00Commented Oct 24, 2017 at 7:59 -
\$\begingroup\$ Suggest
Math.hypot(a[0],a[1])==a[2]instead ofa[0]*a[0]+a[1]*a[1]==a[2]*a[2]\$\endgroup\$ceilingcat– ceilingcat2019年11月22日 20:46:58 +00:00Commented Nov 22, 2019 at 20:46
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
-
\$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$mdahmoune– mdahmoune2017年10月25日 15:45:35 +00:00Commented Oct 25, 2017 at 15:45
-
\$\begingroup\$ This only detects sorted right triangles: input of
A=5,B=4,C=3would not be correctly handled. \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月25日 16:29:59 +00:00Commented 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\$Timtech– Timtech2017年10月25日 22:06:34 +00:00Commented Oct 25, 2017 at 22:06
-
\$\begingroup\$ If we leave off a single
), thenmax(Ans=R►Pr(min(Ans),median(Ansis also valid (though the computation we're doing here is different) and is one byte shorter. \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月25日 22:24:43 +00:00Commented 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\$Timtech– Timtech2017年10月26日 11:37:18 +00:00Commented Oct 26, 2017 at 11:37
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
-
\$\begingroup\$ Some explanations ;) ? \$\endgroup\$mdahmoune– mdahmoune2017年10月26日 14:41:18 +00:00Commented Oct 26, 2017 at 14:41
-
\$\begingroup\$ @mdahmoune here you go \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2017年10月26日 16:26:51 +00:00Commented Oct 26, 2017 at 16:26
-
\$\begingroup\$ Dang it. Didn't you write that language? Doesn't seem fair. (joke) \$\endgroup\$kaine– kaine2017年11月01日 18:03:51 +00:00Commented Nov 1, 2017 at 18:03
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.
-
\$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$mdahmoune– mdahmoune2017年10月25日 13:34:20 +00:00Commented Oct 25, 2017 at 13:34
-
\$\begingroup\$ @mdahmoune You can use this
tio.runlink. However, it is much nicer to just install PARI/GP locally. \$\endgroup\$Jeppe Stig Nielsen– Jeppe Stig Nielsen2017年10月25日 13:52:00 +00:00Commented Oct 25, 2017 at 13:52
Explore related questions
See similar questions with these tags.
21, 38, 5, because 21 + 5 < 38. Is this an intentional pathological case that we have to handle? \$\endgroup\$