50
\$\begingroup\$

Given a, b, c the length of the three sides of a triangle, say if the triangle is right-angled (i.e. has one angle equal to 90 degrees) or not.

Input

Three positive integer values in any order

Output

Either a specific true output (true, 1, yes, ...) or a specific false output (false, 0, no, ...)

Example

5, 3, 4        --> yes
3, 5, 4        --> yes
12, 37, 35     --> yes
21, 38, 50     --> no
210, 308, 250  --> no

Rules

  • The input and output can be given in any convenient format.
  • In your submission, please state the true and the false values.
  • No need to handle negative values or invalid edge triple
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Must we handle negative values or invalid edge triple? \$\endgroup\$
    – DELETE_ME
    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\$ Oct 23, 2017 at 17:18
  • 2
    \$\begingroup\$ I think that using coordinates instead of lengths changes the challenge significantly \$\endgroup\$
    – Luis Mendo
    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\$
    – Kevin
    Oct 23, 2017 at 22:31
  • 1
    \$\begingroup\$ @Kevin no you have not to handle this case. User202729 has already asked this question :) \$\endgroup\$
    – mdahmoune
    Oct 24, 2017 at 6:44

81 Answers 81

38
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ well... I jelly. \$\endgroup\$ 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
    Oct 26, 2017 at 8:15
  • 2
    \$\begingroup\$ @Charlie Answer edited for clarification. \$\endgroup\$
    – DELETE_ME
    Oct 26, 2017 at 10:35
21
\$\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).

\$\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\$
    – mbomb007
    Oct 24, 2017 at 20:14
  • \$\begingroup\$ @mbomb007 exec(code) hmmm, why exec (code) instead of exec code? :D ;-p \$\endgroup\$ 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
    Oct 25, 2017 at 12:23
  • 1
    \$\begingroup\$ @FlipTack ¯_(ツ)_/¯ (also xnor's isn't in Python 2) \$\endgroup\$ 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
    Oct 25, 2017 at 16:15
17
\$\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)
\$\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\$
    – corsiKa
    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\$ 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\$ Oct 24, 2017 at 7:56
13
\$\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.

\$\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

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

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

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

\$\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!

\$\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\$
    – Giuseppe
    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
    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
    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
\$\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!

\$\endgroup\$
8
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

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Oct 24, 2017 at 9:39
  • \$\begingroup\$ Does it always work if you're using bitwise operations? \$\endgroup\$
    – l4m2
    May 25, 2019 at 2:25
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
\$\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!

\$\endgroup\$
5
  • \$\begingroup\$ Outputs 1 for parameters of 1, 1, 1 which is wrong... \$\endgroup\$
    – Neil
    Oct 23, 2017 at 15:47
  • \$\begingroup\$ @Neil It's fixed now. \$\endgroup\$
    – Steadybox
    Oct 23, 2017 at 15:52
  • \$\begingroup\$ Question was updated to use ints, might save some bytes. \$\endgroup\$
    – corsiKa
    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
    Oct 24, 2017 at 9:31
  • \$\begingroup\$ 52 bytes \$\endgroup\$
    – ceilingcat
    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 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².

\$\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!

\$\endgroup\$
1
  • \$\begingroup\$ that's awfully damn clever \$\endgroup\$
    – Jonah
    Feb 2, 2018 at 17:53
4
\$\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...

\$\endgroup\$
4
\$\begingroup\$

Japt, 8 bytes

m²ø½*Ux²

Try it

Japt -x¡, 7 bytes

˲ѶUx²

Try it

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I like the square symbols in your solution ;) \$\endgroup\$
    – mdahmoune
    Oct 23, 2017 at 20:11
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.

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

\$\endgroup\$
3
  • \$\begingroup\$ Is it better than sorting version? \$\endgroup\$
    – mdahmoune
    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
    Oct 25, 2017 at 12:55
  • \$\begingroup\$ Upvote any way :) \$\endgroup\$
    – mdahmoune
    Oct 25, 2017 at 12:57
3
\$\begingroup\$

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!

\$\endgroup\$
3
  • \$\begingroup\$ Why duplicate the list? \$\endgroup\$
    – mdahmoune
    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
    Oct 24, 2017 at 19:49
  • \$\begingroup\$ Thanx upvote ;) \$\endgroup\$
    – mdahmoune
    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.
\$\endgroup\$
7
  • \$\begingroup\$ Awesome ;) but i think (define fun must be a part of the code... \$\endgroup\$
    – mdahmoune
    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\$ 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\$ 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\$
    – xnor
    Oct 23, 2017 at 19:07
  • 2
    \$\begingroup\$ @MishaLavrov Then how about (*(expt(max a b c)2)2)? \$\endgroup\$
    – xnor
    Oct 23, 2017 at 21:25
3
\$\begingroup\$

05AB1E, 6 bytes

n{R`+Q

Try it online!

\$\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\$ Oct 24, 2017 at 15:07
  • \$\begingroup\$ @NickLoughlin Oops, removed first example \$\endgroup\$
    – Okx
    Oct 24, 2017 at 17:36
  • \$\begingroup\$ You could do n{RÆ_ to save a byte. \$\endgroup\$
    – Emigna
    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\$
    – Makonede
    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.

\$\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\$ 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
    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
    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!

\$\endgroup\$
3
  • \$\begingroup\$ Can you save some bytes using currying rather than an array? \$\endgroup\$ Oct 23, 2017 at 15:04
  • 1
    \$\begingroup\$ @AdmBorkBork Nope, because sort takes an array. \$\endgroup\$ 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\$
    – ceilingcat
    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

\$\endgroup\$
5
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$
    – mdahmoune
    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\$ 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
    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\$ 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
    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
\$\endgroup\$
3
  • \$\begingroup\$ Some explanations ;) ? \$\endgroup\$
    – mdahmoune
    Oct 26, 2017 at 14:41
  • \$\begingroup\$ @mdahmoune here you go \$\endgroup\$ Oct 26, 2017 at 16:26
  • \$\begingroup\$ Dang it. Didn't you write that language? Doesn't seem fair. (joke) \$\endgroup\$
    – kaine
    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.

\$\endgroup\$
2
  • \$\begingroup\$ If possible, please include a link to an online testing environment so other people can try out your code! \$\endgroup\$
    – mdahmoune
    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\$ Oct 25, 2017 at 13:52
3
\$\begingroup\$

Ohm v2, 8 6 bytes

²DS)Σε

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

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

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