17
\$\begingroup\$

Who doesn't love the Pythagorean theorem \$a^2+b^2=c^2\$? Write the shortest method you can in any language that takes in value a and b and prints out The hypotenuse of this right triangle is c. Keep c to only three decimal places.

\$\endgroup\$
3
  • \$\begingroup\$ Shortest in characters or bytes? :) \$\endgroup\$ Commented Feb 5, 2014 at 20:53
  • 3
    \$\begingroup\$ shortest in characters \$\endgroup\$
    – Vik P
    Commented Feb 5, 2014 at 20:55
  • 8
    \$\begingroup\$ The code-golf tag explicitly says "Code-golf is a competition to solve a particular problem in the fewest bytes of source code." See Scoring code golf (bytes vs. characters). \$\endgroup\$
    – r.e.s.
    Commented Feb 6, 2014 at 12:47

67 Answers 67

1
\$\begingroup\$

JavaScript, 70 bytes

(a,b)=>'The hypotenuse of the triangle is '+((a*a+b*b)**.5).toFixed(3)

The latter function rounds the number to 3 decimal places.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ It would be shorter to use addition instead of a template string. \$\endgroup\$
    – Razetime
    Commented Feb 8, 2021 at 11:24
  • \$\begingroup\$ Thanks for the advice, 70 bytes now \$\endgroup\$
    – user100690
    Commented Feb 8, 2021 at 11:57
1
\$\begingroup\$

Rust, 72 bytes

|a,b|print!("The hypotenuse of this right triangle is {:.3}",a.hypot(b))

Try it online!

Woohoo, rust has built-ins for this. The hypot method calculates the hypotenuse between the numbers. Instead of the usual "{}" format substitution I used "{:.3}" which forces the number to display with three decimal places.

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

Vyxal, 32 30 bytes

∆/`λƛ h⁼Ǐ¢Ṗλ₇ of λ× ¬ẋ Ȧ⇩ is Π

Try it Online!

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

Fig, \$36\log_{256}(96)\approx\$ 29.632 bytes

+qDFQ$XS)s2lSXreS8x%twBEZ:x@KUX"mqS*

Try it online!

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

ARBLE, 73 60 bytes

"The hypotenuse of this right triangle is %.3f"%(a^2+b^2)^.5

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @H.PWiz I must say, for a challenge that should just be sqrt(a^2+b^2), this has a lot of unnecessary boilerplate. \$\endgroup\$
    – ATaco
    Commented Oct 22, 2017 at 23:12
0
\$\begingroup\$

Javascript, 109

A=1,B=1,s=1e3,console.log("The hypotenuse of this right triangle is " + (Math.round(Math.sqrt(A*A+B*B)*s)/s))

Fiddledee

\$\endgroup\$
3
  • 4
    \$\begingroup\$ with alert you could save 6 chars \$\endgroup\$
    – Fez Vrasta
    Commented Feb 5, 2014 at 18:23
  • \$\begingroup\$ Down to 101: A=1,B=1,s=1e3;alert("The hypotenuse of this right triangle is "+(Math.round(Math.sqrt(A*A+B*B)*s)/s)) \$\endgroup\$ Commented Feb 6, 2014 at 15:57
  • \$\begingroup\$ Using suggestions from other entries so far I get it to 90: A=1,B=1;alert("The hypotenuse of this right triangle is "+(Math.sqrt(A*A+B*B).toFixed(3))) Even less if you leave out alert \$\endgroup\$
    – micha
    Commented Feb 8, 2014 at 0:03
0
\$\begingroup\$

Scala (95c)

import Math._;val x=sqrt(pow(a,2)+pow(b,2));f"The hypotenuse of this right triangle is $x%1.3g"
\$\endgroup\$
0
\$\begingroup\$

Java : 223

   class l {
        public static void main(String[] args) {      
          new l().H(2,4);
        }   
        void H(float a, float b)
    {
        System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));
    }
    }
\$\endgroup\$
2
  • \$\begingroup\$ I brought this to 170 by making it a one liner, changing the method to a static: class h{public static void main(String[]args){H(2,4);}static void H(float a,float b){System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}} \$\endgroup\$ Commented Feb 6, 2014 at 10:41
  • \$\begingroup\$ @Michaël Demey, hah, i didnt think of that changing the function to static . \$\endgroup\$ Commented Feb 6, 2014 at 11:25
0
\$\begingroup\$

Groovy : 78 Characters

a={x,y->printf"The hypotenuse of this right triangle is %.3f",Math.hypot(x,y)}

Assuming the task is just writing a method. If you need to take input parameters:

99 Characters

printf"The hypotenuse of this right triangle is %.3f",Math.hypot(args[0].toLong(),args[1].toLong())

I found two ways to it with the same length:

printf"The hypotenuse of this right triangle is %.3f",Math.sqrt(args.collect{it.toLong()**2}.sum())
\$\endgroup\$
0
\$\begingroup\$

newLISP, 103 characters

(let(a 3.1 b 4)(println"The hypotenuse of this right triangle is "(round(pow(add(pow a)(pow b)).5)-3)))

The unspecified input method helps a lot... :)

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

F#, 78

let f a b=sqrt(a*a+b*b)|>printf"The hypotenuse of this right triangle is %.3f"
\$\endgroup\$
0
\$\begingroup\$

Java : 155

class A{A(int x,int y){System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(x,y));}public static void main(String[]s){new A(1,4);}}
\$\endgroup\$
1
  • \$\begingroup\$ You can take out the public on the constructor saving 7 \$\endgroup\$ Commented Feb 6, 2014 at 16:12
0
\$\begingroup\$

Fortran (90)

read(*,*)a,b;print'(a,f9.3)',"The hypotenuse of this right triangle is ",sqrt(a*a+b*b);end

Uses implicit typing so that a,b are single precision reals.

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

Javascript 98 characters/bytes

This is implemented as a browser alert in a function.

function p(a,b){alert('The hypotenuse of this right triangle is: '+Math.sqrt(a*a+b*b).toFixed(3))};
\$\endgroup\$
0
\$\begingroup\$

Ruby 2.1+ (73 characters)

f=->a,b{puts"The hypotenuse of this right triangle is %.3f"%(a+b*1i).abs}
\$\endgroup\$
0
\$\begingroup\$

Prolog

h(A,B):-X is A*A+B*B, C is sqrt(X),format('The hypotenuse of this right triangle is ~3f',C).
\$\endgroup\$
0
\$\begingroup\$

Bash with bc, 91

No bash script yet?!

read a b
echo -n 'The hypotenuse of this right triangle is '
bc<<<"scale=3;sqrt($a^2+$b^2)"

There is no way this can win.

\$\endgroup\$
2
  • \$\begingroup\$ To save 1 character, you could use printf instead of echo -n. \$\endgroup\$ Commented Apr 19, 2014 at 14:19
  • 1
    \$\begingroup\$ Instead of bc, use dc: dc<<<"3k$a d*$b d*+vp" \$\endgroup\$ Commented Apr 19, 2014 at 14:29
0
\$\begingroup\$

Haskell, 77 characters

(\x y->print$"The hypotenuse of this right triangle is "++(show$sqrt$x^2+y^2))

Useage:

(\x y->print$"The hypotenuse of this right triangle is "++(show$sqrt$x^2+y^2)) 3.0 4.0
The hypotenuse of this right triangle is 5.0
\$\endgroup\$
0
\$\begingroup\$

Fortran 2008 (87 characters)

Based on the Fortran 90 solution, but now in Fortran 2008, HYPOT is an intrinsic function.

read(*,*)a,b;print'(a,f9.3)','The hypotenuse of this right triangle is ',hypot(a,b);end
\$\endgroup\$
0
\$\begingroup\$

J, 56 bytes

'The hypotenuse of this right triangle is '&,@":@(+&.*:)

explanation

J's "Under" verb &. is a nice fit here. It applies a transformation, executes a verb, and then applies the reverse transformation to the result. In this case, squaring is the transformation *:, its reverse is taking the square root, and the verb in question is addition. So we do addition "under" the square transformation. The rest is just formatting the number and cat'ing it onto the sentence.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ |@j. is even nicer. \$\endgroup\$
    – FrownyFrog
    Commented Oct 21, 2017 at 20:33
  • \$\begingroup\$ @FrownyFrog that is wonderful, thanks. \$\endgroup\$
    – Jonah
    Commented Oct 21, 2017 at 23:05
0
\$\begingroup\$

Perl 5, 69 bytes

printf"The hypotenuse of this right triangle is %.3f",sqrt<>**2+<>**2

Try it online!

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

Tcl, 80 bytes

puts "The hypotenuse of this right triangle is [expr int(hypot($a,$b)*1e3)/1e3]"

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I doubt that setting variables is an allowed I/O method... Nevertheless, your program does not seem to [k]eep c to only three decimal places. \$\endgroup\$ Commented Oct 21, 2017 at 15:14
  • \$\begingroup\$ @JonathanFrech: Fixed the 3 decimal places issue. About the setting variables issue, I will wait feedback from questions asker, as it seems I've seen some answers here doing the same. \$\endgroup\$
    – sergiol
    Commented Oct 21, 2017 at 15:35
0
\$\begingroup\$

Jq 1.5, 80 bytes

"The hypotenuse of this right triangle is \(1e3*pow(map(.*.)|add;.5)|floor/1e3)"

Assumes input is [a,b]

Try it online at tio.run or jqplay.org

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

Axiom, 92 bytes

g(a,b)=="The hypotenuse of this right triangle is "((round((a^2+b^2)^.5*1.e3)/1.e3)::String)

test

(4) -> g(12, 19)
   (4)  "The hypotenuse of this right triangle is 22.472"
                                                             Type: String
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 53 characters

"The hypotenuse of this right triangle is "|nOt₄*ï₄/«

Try it online!

I could not figure out how to compress 05AB1E strings. :(

Explanation:

"..."|nOt₄*ï₄/«

     |             Push array of inputs.
      n            Square them.
       O           Get the sum.
        t          Get the square root.
         ₄*        Multiply by 1000
           ï       Convert to integer. This rounds it.
            ₄/     Divide by 1000
"..."         «    Append it to the string.
                   Implicit print.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ "The hypotenuse of this triangle is nOt 4" \$\endgroup\$ Commented Feb 8, 2021 at 3:11
  • 1
    \$\begingroup\$ compressed string version (i believe it is the most optimal compression here) Try it online! \$\endgroup\$
    – emirps
    Commented Mar 16, 2023 at 15:08
0
\$\begingroup\$

Petit Computer BASIC, 66 bytes

INPUT A,B?"The hypotenuse of this right triangle is ";SQR(A*A+B*B)

PTC only displays the first 3 decimal places when printing.

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

Stax, 24 bytes

╬Z.-êû$P¿[▐Ä√#╙δdr▼▀Ö☺┐ç

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

`1/yyH\fBXNR&X+{.SC`p   print "The hypotenuse of this right triangle is "
:J+                     square and add the inputs
|Q                      square root
3j                      round to 3 places
p                       print

Run this one

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

APL (Dyalog), 51 bytes

'The hypotenuse of this right triangle is',3⍕|0j1⊥⎕

Try it online!

0j1⊥⎕: Convert the input into base i. e.g a b -> b+ai

|: Magnitude

3⍕: Round to 3 decimal places

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

Japt, 37 bytes

`T hypÇQ«e  È ght â0Ø8  `+Nx²¬x3

Try it

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

JavaScript (Node.js), 76 bytes

a=>b=>'The hypotenuse of this right triangle is '+Math.hypot(a,b).toFixed(3)

Try it online!

Explanation

a => b => // the two arguments (curry method)
    `The hypotenuse of this right triangle is // write the text required
    ${Math.hypot(a,b)}` // and use JS Math.hypot function to calculate the hypotenuse. 

Alternately, if the you spare me the pain of writing this text.

f=Math.hypot // is also a valid answer
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG. Note, though, the requirement that the output be rounded to 3 decimal places. \$\endgroup\$
    – Shaggy
    Commented Aug 24, 2018 at 15:59

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.