| Bytes | Lang | Time | Link |
|---|---|---|---|
| 036 | Japt S | 180824T155219Z | Shaggy |
| 035 | Thunno 2 j | 230614T163829Z | The Thon |
| nan | 230315T083831Z | The Thon | |
| 060 | ARBLE | 171022T224558Z | ATaco |
| 108 | Go | 221013T034809Z | bigyihsu |
| nan | Fig | 221013T033141Z | naffetS |
| 030 | Vyxal | 221013T024230Z | DialFros |
| 072 | Rust | 210209T030856Z | Aiden4 |
| 093 | Whispers v2 | 210208T030750Z | Razetime |
| 028 | Jelly | 210208T154322Z | caird co |
| 110 | Javascript | 140208T085837Z | Ismael M |
| 076 | Python 2.7 76 Characters | 140205T181753Z | Abhijit |
| 070 | JavaScript | 210208T104737Z | user1006 |
| 051 | Pyth | 210208T040714Z | hakr14 |
| 041 | Husk | 190326T103316Z | Unrelate |
| 076 | JavaScript Node.js | 180824T153846Z | user8232 |
| 051 | APL Dyalog | 180518T231443Z | H.PWiz |
| 024 | Stax | 180519T001902Z | recursiv |
| 066 | Petit Computer BASIC | 171023T214842Z | 12Me21 |
| 032 | Jelly | 171023T193415Z | sporkl |
| 053 | 05AB1E | 171023T200433Z | sporkl |
| 092 | Axiom | 171023T080327Z | user5898 |
| 080 | Jq 1.5 | 171023T033541Z | jq170727 |
| 057 | OML | 171023T010658Z | Conor O& |
| 052 | TIBASIC | 140205T164805Z | Timtech |
| 076 | R | 171021T194825Z | Rudier |
| 080 | Tcl | 171021T144858Z | sergiol |
| 066 | c64 basic v2 | 170913T210203Z | peterh |
| 069 | Perl 5 | 170913T204644Z | Xcali |
| 056 | J | 170913T192923Z | Jonah |
| 087 | Fortran 2008 | 151024T041833Z | Time Lai |
| 077 | Haskell | 140418T141528Z | Thom Wig |
| 091 | Bash with bc | 140418T103526Z | user1640 |
| 079 | Python 2 | 140216T103517Z | user344 |
| nan | 140217T153146Z | yedidyak | |
| 073 | Ruby 2.1+ | 140216T234447Z | O-I |
| nan | Ruby | 140205T191339Z | epson121 |
| 093 | JavaScript | 140206T102534Z | Eliseo D |
| 098 | Javascript 98 characters/bytes | 140206T201258Z | Claudia |
| nan | 140205T182957Z | Merin Na | |
| 090 | Fortran | 140207T152601Z | Kyle Kan |
| 065 | Golfscript | 140205T190413Z | Joachim |
| 155 | Java | 140206T155049Z | Sumedh |
| 112 | Java | 140206T160809Z | James We |
| 078 | F# | 140206T152602Z | Rik |
| 046 | Sclipting | 140206T150416Z | Timwi |
| 103 | newLISP | 140206T124445Z | cormulli |
| 055 | dc | 140206T132136Z | Tomas |
| 078 | Groovy 78 Characters | 140206T130845Z | Micha |
| 083 | JavaScript | 140206T105356Z | Fez Vras |
| 079 | EcmaScript 6 | 140205T171213Z | Florent |
| 223 | Java | 140206T100720Z | BeyondPr |
| nan | 140206T081125Z | bela | |
| 111 | PowerShell | 140206T081404Z | Iszi |
| 078 | AWK — 84 | 140205T181106Z | user1525 |
| nan | 140206T075226Z | Brandon | |
| nan | 140206T065134Z | Mika | |
| 7799 | C | 140205T164204Z | Josh |
| 1001 | C | 140205T232834Z | user1220 |
| 054 | dc | 140205T232252Z | daniero |
| 097 | Javascript | 140205T215952Z | elixenid |
| 054 | APL | 140205T184254Z | marinus |
| 074 | Perl 6 68 | 140205T170459Z | null |
| 090 | C++ | 140205T164630Z | user1076 |
| 080 | Python 2.7 | 140205T170054Z | Andrea C |
| 074 | MATLAB | 140205T165456Z | mmumboss |
| 109 | Javascript | 140205T165445Z | micha |
Thunno 2 j, 35 bytes
ÆH3ȯ‘The hypotenuse of ċṠ Ṗṇ ġḳ is
4 bytes (ÆH3ȯ) for the main bit, and 31 bytes for the compressed string.
Explanation
ÆH3ȯ‘... # Implicit input
ÆH # Compute hypotenuse
3ȯ # Round to 3 d.p.s
‘... # Push compressed string
# Join the stack
# Implicit output
Thunno Kj, \$ 46 \log_{256}(96) \approx \$ 37.86 bytes
AO3Zu"The hypotenuse of this right triangle is
Not having string compression really hurts in challenges like these.
Explanation
# Implicit input
AO # Push the hypotenuse of the inputs (yes, this is a built-in)
3Zu # Round to three decimal places
"... # Push the string
# Flags join the stack by spaces
# Implicit output
Go, 108 bytes
import(."fmt";."math")
func f(a,b float64){Printf("The hypotenuse of the triangle is %.3f\n",Sqrt(a*a+b*b))}
Rust, 72 bytes
|a,b|print!("The hypotenuse of this right triangle is {:.3}",a.hypot(b))
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.
Whispers v2, 133, 93 bytes
> Input
> Input
> "The hypotenuse of this right triangle is %.3f"
>> 1⊿2
>> 3%4
>> Output 5
The major part of this program is calculating the decimal point's index and truncating to 3 digits. -40 bytes from Leo!
Jelly, 28 bytes
ÆḊær3“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ»,K
How it works
ÆḊær3“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ»,K - Main link. Takes a pair [a, b] as input
ÆḊ - Take the norm √(a²+b²)
ær3 - Round the 3 decimal places
“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ» - "The hypotenuse of this right triangle is"
, - Pair the string with the norm
K - Insert a space between the two
Javascript, 110 bytes
function h(a,b){return a/1==a&&b/1==b?'The hypotenuse of this right triangle is '+(a*a+b*b||0).toFixed(3):!1;}
It's a monster!!!
But it can handle NaN and invalid numbers, like text, objects, 0/0 and other invalid numbers.
Python 2.7 - 76 Characters
print'The hypotenuse of this right triangle is %.3f'%abs(input()+1j*input())
Explanation
$$|a+bi| = \sqrt{a^2 + b^2} = c \\ \implies a^2 + b^2 = c^2$$
PJ on hypotenuse
Teacher: "Can you tell me, what is hypotenuse?"
LJ: "Hypotenuse, an easy question. If there's a high profile party last night, and you read it in the news paper, its called High Party News"
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.
Pyth, 51 bytes
+." y¢ó³-.n?ãÜPýD'9¹i¾5Á«£¿9WhI".R@s^RJ2QJ3
The actual math only takes up 8 bytes.
Despite having built-in string compression capabilities, Pyth is pretty bad at compressing strings.
Husk, 41 bytes
+¨ΣŸṗ¤μe₀fı≤ṙ↔ẏ«G¹∫¨J"."msλ‰⁰i*⁰)1000√¤+□
With rounding being involved, I sure didn't pick the right language for the task...
+ The concatenation of
¨...¨ the compressed prefix for the output
ms with the stringified elements of
λ‰⁰ )1000 the divmod by 1000 of
√ the square root of
+ the sum of
¤ □ the squares of the two arguments
*⁰ times 1000
i converted to an integer
J"." with a period inserted between them.
Computing the actual length of the hypotenuse only takes four bytes, rounding to three decimal places takes seventeen bytes, and adding on The hypotenuse of this right triangle is to the output takes twenty bytes. 90.244% of this answer is just formatting!
JavaScript (Node.js), 76 bytes
a=>b=>'The hypotenuse of this right triangle is '+Math.hypot(a,b).toFixed(3)
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
APL (Dyalog), 51 bytes
'The hypotenuse of this right triangle is',3⍕|0j1⊥⎕
0j1⊥⎕: Convert the input into base i. e.g a b -> b+ai
|: Magnitude
3⍕: Round to 3 decimal places
Stax, 24 bytes
╬Z.-êû$P¿[▐Ä√#╙δdr▼▀Ö☺┐ç
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
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.
Jelly, 32 characters
,²S½ær3µ,“¡ÆC⁷⁺ɱSoṿȤç½?⁶Ẏtḍỵŀ»ṚK
There is probably a better string compression that allows me to get around needing to join with spaces but I was having trouble finding it.
Explanation:
,²S½ær3µ,“...»ṚK Example inputs: 3, 4
, Pair the inputs. Result: [3, 4]
² Square them. Result: [9, 16]
S Sum them. Result: 25
½ Get the square root of the sum. Result: 5
ær3 Round to 3 decimal places. Result: 5
µ Take the result of that... Result: 5
“...» ...and the compressed string Result: "The hypotenuse of this right triangle is"
, And put them into a pair. Result: [5, "The hypotenuse of this right triangle is"]
Ṛ Reverse that. Result: ["The hypotenuse of this right triangle is", 5]
k Join it with spaces. Result: "The hypotenuse of this right triangle is 5.0"
Implicit output.
05AB1E, 53 characters
"The hypotenuse of this right triangle is "|nOt₄*ï₄/«
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.
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
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
OML, 57 bytes
"The hypotenuse of this right triangle is "shnhn+A6`*N3eD
Part 1
This simply outputs the string
"The hypotenuse of this right triangle is "s
Part 2
hnhn+A6`*N3eD
hn take input and square it
hn take another input and square it
+ add them
A6` push 10^6
* multiply the sum with that number
N take integer square root
3eD output with three places of precision
implicit output
TI-BASIC, 76 55 53 52 bytes
Input :Disp "THE HYPOTENUSE OF THIS RIGHT TRIANGLE IS
Fix 3:R▶Pr(X,Y
No, a closing parentheses is not required. Also, less bytes than that APL answer :)
R, 61 76 bytes
cat("The hypotenuse of this right triangle is",round(sqrt(sum(scan()^2)),3))
cat displays its content to STDOUT.
The scan() function takes user's input from keyboard. This input exists as a vector, on which the ^2 is applied (^function is vectorized), and the sum() sums the elements of the vector. sqrt outputs the square-root, which is rounded to 3 decimal places by round(,3)
Thanks to @caird coinheringaahing for noticing that the previous answer didn't round.
Tcl, 80 bytes
puts "The hypotenuse of this right triangle is [expr int(hypot($a,$b)*1e3)/1e3]"
c64 basic v2, 60 66 bytes
0inputa,b:?"The hypotenuse of this right triangle is";sQ(a*a+b*b)
Screenshot:
Perl 5, 69 bytes
printf"The hypotenuse of this right triangle is %.3f",sqrt<>**2+<>**2
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.
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
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
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.
Python 2 (79)
def p(a,b):print'The hypotenuse of this right triangle is %.3d'%((a*a+b*b)**.5)
Prolog
h(A,B):-X is A*A+B*B, C is sqrt(X),format('The hypotenuse of this right triangle is ~3f',C).
Ruby 2.1+ (73 characters)
f=->a,b{puts"The hypotenuse of this right triangle is %.3f"%(a+b*1i).abs}
Ruby, 94 90 82 chars
p "The hypotenuse of this right triangle is %.3f"%(Math.sqrt(gets.to_i**2+gets.to_i**2))
Update (thanks for the comments):
p "The hypotenuse of this right triangle is %.3f"%(gets.to_i**2+gets.to_i**2)**0.5
JavaScript 118 106 93
Unlike @micha's solution, mine takes in two variables via function and sends the alert of the result.
function(a,b){m=Math;c=d=>d*d,e=1e3;alert("The hypotenuse of this right triangle is "+m.round(m.sqrt(c(a)+c(b))*e)/e)}
function(a,b){e=1e3;alert("The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e)}
Fat arrow functions to the rescue!
h=(a,b,e=1e3)=>"The hypotenuse of this right triangle is "+Math.round(Math.sqrt(a*a+b*b)*e)/e
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))};
C#
Method Only (114)
void H(double a, double b)
{
Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b));
}
Complete Program (171)
using System;
class P
{
static void H(double a, double b)
{
Console.Write("The hypotenuse of this right triangle is {0:N3}", Math.Sqrt(a * a + b * b));
}
static void Main()
{
H(3, 4);
}
}
Complete Program (without using method - 141)
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(a*a+b*b));}}
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.
Golfscript (69 67 66 65)
This would be much easier if floating point was actually supported without resorting to workarounds... :)
~'The hypotenuse of this right triangle is '@.*@.*+2-1??+.'.'?4+<
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);}}
Java, 112
(Also prints out a No Such Method error, though I'm not sure if this is against the rules)
class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}}
Java, 149
(No error)
class A{static{int a=1,b=1;System.out.printf("The hypotenuse of this right triangle is %.3f",Math.hypot(a,b));}public static void main(String[] a){}}
F#, 78
let f a b=sqrt(a*a+b*b)|>printf"The hypotenuse of this right triangle is %.3f"
Sclipting, 46 characters
글坼各갠方終加감半方갾밈乘增貶껠矽녆둥긆둹댆뭴뉖멵댶넠닶눠덆둩댲걲늖덨덂건댦땡닦덬뉒걩댲밀⓶
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..."
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... :)
dc, 55
3k?d*?d*+v[The hypotenuse of this right triangle is ]Pp
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())
JavaScript: 83
i=prompt,'The hypotenuse of this right triangle is '+Math.hypot(i(),i()).toFixed(3)
Currently the shortest JS implementation using stdin :D
Works only on Firefox 27.0+ (EcmaScript 6)
JavaScript: 78
If we can use just two variables (as lot of scripts do here):
a=2,b=3,'The hypotenuse of this right triangle is '+Math.hypot(a,b).toFixed(3)
EcmaScript 6, 82 79
f=(a,b)=>"The hypotenuse of this right triangle is "+Math.hypot(a,b).toFixed(3)
Usage:
f(3, 5)
> "The hypotenuse of this right triangle is 5"
Update: Switch to Math.hypot()
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));
}
}
DELPHI / PASCAL
With indent (157)
program p;
{$APPTYPE CONSOLE}
var a,b:integer;
begin
readln(a,b);
writeln('the hypotenuse of this right triangle is',sqrt(b*b+a*a):2:3);
end.
PowerShell: 111
Golfed Code
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.
AWK — 84 78 characters
awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'
Thanks to Wasi for suggesting ^ operator and removing ()!
e.g.
$ echo 3 4 | awk '{printf"The hypotenuse of this right triangle is %.3f\n",($1^2+$2^2)^.5}'
The hypotenuse of this right triangle is 5.000
Powershell
Just to see if i could...
echo "The hypotenuse of this right triangle is " ([math]::round([math]::sqrt(([math]::pow(([double](Read-Host -p "A")),2) + [math]::pow(([double](Read-Host -p "B")),2))),3))
Scala (95c)
import Math._;val x=sqrt(pow(a,2)+pow(b,2));f"The hypotenuse of this right triangle is $x%1.3g"
C, 77 or 99
77 characters if input can just be the function arguments:
f(a,b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
99 if input must be read from stdin:
a,b;f(){scanf("%d %d",&a,&b);printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
A big thanks to @Yimin Rong!
C, 100 chars (beats the other C solution by 1!)
A ridiculously inefficient algorithm.
x;f(a,b){for(;x-a*a-b*b;x=rand());printf("The hypotenuse of this right triangle is %.3f",sqrt(x));}
dc 54
Tangents the score of the APL answer!
2^r2^+3kv[The hypotenuse of this right triangle is ]Pp
Test:
$ dc
3 4
2^r2^+3kv[The hypotenuse of this right triangle is ]Pp
The hypotenuse of this right triangle is 5.000
Javascript (97)
x=prompt;a=x(),b=x();x('The hypotenuse of this right triangle is '+Math.sqrt(a*a+b*b).toFixed(3))
APL (54)
'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2
Test:
'The hypotenuse of this right triangle is',3⍕.5*⍨+/⎕*2
⎕:
9 10
The hypotenuse of this right triangle is 13.454
Explanation:
⎕*2: raise the values in the input to the second power+/: take the sum.5*⍨: raise the result to the 0.5th power3⍕: round to 3 decimal places
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.
C++ - 90
void h(int a,int b){printf("The hypotenuse of this right triangle is %.3f\n",hypot(a,b));}
Python 2.7 - 80 chars
print'The hypotenuse of this right triangle is %.3f'%(input()**2+input()**2)**.5
MATLAB 79 74
@(a,b)sprintf('The hypotenuse of this right triangle is %.3f',norm([a b]))
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))
