Who doesn't love the pythagorean theorem a²+b²=c²? 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.

@Cruncher: I tried to encode the string but couldn't get the decoding routine small enough.
–
marinusFeb 5 '14 at 19:37

By the pigeon hole principle, I think you'd have a really hard time (maybe impossible. It has to be impossible for at least some strings) trying to compress the string. Maybe if the string had some logical pattern, but that doesn't appear to be the case. I'm interested to see the attempts you've had so far though
–
CruncherFeb 5 '14 at 20:38

5

Correct the spelling of "hypotenuse" to save a character.
–
Tim S.Feb 5 '14 at 20:45

1

@Cruncher: Actually, I beat it in Sclipting...
–
TimwiFeb 6 '14 at 15:04

Expects the input as two numbers (can be fractional!) separated by a space.

This is shorter than APL, despite having to use a few inconvenient tricks.

Explanation

글坼 | split at space
各 | for each...
갠方 | to the power of two
終
加 | add
감半方 | to the power of one half
갾밈乘 | multiply by 1000
增貶 | increment, then decrement (kludge for rounding)
껠矽 | insert '.' at 4th-last character position
녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶ | "The hypotenuse..."

Perl 6 (68 74 bytes)

{printf "The hypotenuse of this right triangle is %.3f
",sqrt [+] @_ X**2}

{} declares a lambda function. [+] is sum operator, X** is cross power operator (for example, 1, 2 X+ 10, 20 gives 11, 21, 12, 22). In this case, cross power operator takes one argument, so the result has the same length as @_. @_ contains all function arguments.

If it's disallowed to have function that may take wrong number of arguments (unsafe), it's possible to replace [+] @_ X**2 with $^a**2+$^b**2, where $^a and $^b are placeholder arguments.

doesn' really work for me. dc -e '2^r2^+3kv[The hypotenuse of this right triangle is ]Pp' doesn't wait for any input, prints "dc: stack empty" 3 times and then "The hypotenuse of this right triangle is 2.000".
–
TomasFeb 6 '14 at 13:19

@Tomas it's sort of a a function; you need to put the parameters on the stack first, like I show in the test, or if you want to invoke your way, it would be dc -e '3 4 2^r2^+3kv[... where 3 and 4 are the parameters.
–
danieroFeb 6 '14 at 14:03

Nice work. Looks like you did this while I was busy with my own solution, which beats this by about 62 characters. For future reference, Code Golf answers are expected to be "golfed" and have their "score" included. "Golfing" means that you should make every effort to reduce the character length by using short-hand aliases, syntax tricks, and other means of stretching the language's rules. You should also remove unnecessary whitespace where possible (there's at least three spaces that can be removed from your solution). The "score", in this case, is your character count - currently 173.
–
IsziFeb 6 '14 at 8:23

I suggest reading the code golf tag wiki, various portions of the Help Center, and the Golfing Tips for PowerShell thread to get a better feel for how to write a competitive answer to code golf challenges here.
–
IsziFeb 6 '14 at 8:25

Also, I get an error with your script. "...the parameter name 'p' is ambiguous..." with regards to Read-Host.
–
IsziFeb 6 '14 at 8:29

to late for an edit, so again.. Edit: You can get 2 characters off by changing integer to int16 You dont have to include the first 2 lines for your answer, and you can remove whitespace. doing all that gives you 106 characters.
–
Teun PronkFeb 6 '14 at 9:15

Heres my complete program. 141 chars...you can save a few chars using the formatstring overload of write using System;class P{static void Main(){double a=3,b=4;Console.Write("The hypotenuse of this right triangle is {0:N3}",Math.Sqrt(aa+bb));}}
–
Tim BaileyFeb 6 '14 at 3:27

1

You can use string formatting in the Console.Write instead of calling ToString() to save 9 characters.
–
RikFeb 7 '14 at 10:16

Thanks Tim and Rik. I learnt a new thing from you guys.
–
Merin NakarmiFeb 7 '14 at 20:23

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))
–
James WebsterFeb 6 '14 at 15:57

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
–
michaFeb 8 '14 at 0:03

PowerShell: 111

1..2|%{sv $_ (read-host)};"The hypotenuse of this right triangle is $("{0:N3}"-f[math]::sqrt($1/1*$1+$2/1*$2))"

Walkthrough

1..2|%{sv $_ (read-host)}; Gets two inputs interactively from the user, and stores them in $1 and $2. Might be able to cut some length by using arguments or pipeline inputs instead.

"The hypotenuse of this right triangle is Required text in the output, per the challenge specifications.

$(...)" Encapsulated code block will be processed as script before being included in the output.

"{0:N3}"-f Formats output from the next bit of code as a number with exactly three digits after the decimal point.

[math]::sqrt(...) Gets the square root of the encapsulated value.

$1/1*$1+$2/1*$2 Serves as our "a^2+b^2". Multiplying a number by itself is the shortest way to square it in PowerShell, but the variables need to be divided by 1 first to force them to integers. Otherwise, they are treated as text and 3*3+4*4 would be 3334444 instead of 25.

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));}}
–
Michaël DemeyFeb 6 '14 at 10:41

@Michaël Demey, hah, i didnt think of that changing the function to static .
–
BeyondProgrammerFeb 6 '14 at 11:25