21
\$\begingroup\$

Given three sides of a triangle, print area of this triangle.

Test cases:

In: 2,3,4

Out: 2.90473750965556

In: 3,4,5

Out: 6

Given the sides \$a\$, \$b\$, and \$c\$, you can assume that \$a>0\$, \$b>0\$, \$c>0\$, \$a+b>c\$, \$b+c>a\$, and \$c+a>b\$.

This is , shortest answer per language in bytes wins.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ What is the required precision? Can the input and outputs be integers? Rounded to the nearest hundredth? Single precision floats? Rationals? \$\endgroup\$ Commented Mar 22, 2021 at 22:39

30 Answers 30

8
\$\begingroup\$

J, 23 19 chars

 (4%~2%:[:*/+/-0,+:) (4%~2%:[:*/+/-0,+:) 2 3 4 2.90474 (4%~2%:[:*/+/-0,+:) 3,4,5 6 

17-char version if input is in i: 4%~%:*/(+/,+/-+:)i

original 23-char version: (%:@(+/**/@(+/-+:))%4:)

\$\endgroup\$
7
\$\begingroup\$

APL 21 20

Takes screen input via ←⎕

(×/(+/t÷2)-0,t←⎕)*.5 
\$\endgroup\$
7
\$\begingroup\$

Mathematica 23

√Times@@(+##/2-{0,##})& 
\$\endgroup\$
2
  • \$\begingroup\$ Just an aside comment. We (usually) try to provide Mma code as functions here. For example in your case Sqrt[Tr@#*Times@@(Tr@#-2#)]/4& \$\endgroup\$ Commented Mar 27, 2013 at 12:49
  • 2
    \$\begingroup\$ 28 chars, as a function (Tr@#Times@@(Tr@#-2#))^.5/4&, or 27 using a variable \$\endgroup\$ Commented Mar 27, 2013 at 13:09
6
\$\begingroup\$

Python 2, 53

t=input() s=a=sum(t)/2. for x in t:a*=s-x print a**.5 

Input: 2,3,4

Output: 2.90473750966

\$\endgroup\$
2
  • 3
    \$\begingroup\$ I wasted a lot of time trying to come up with a better solution. I'm convinced this is as good as it gets \$\endgroup\$ Commented Mar 30, 2013 at 11:02
  • 2
    \$\begingroup\$ @jamylak you and me both ;) \$\endgroup\$ Commented Mar 31, 2013 at 15:33
5
\$\begingroup\$

Python 57 bytes

a,b,c=input() s=(a+b+c)*.5 print(s*(s-a)*(s-b)*(s-c))**.5 

Using Heron's Formula.

Sample usage:

$ echo 2,3,4 | python triangle-area.py 2.90473750966 $ echo 3,4,5 | python triangle-area.py 6.0 

A 58 byte variant:

a,b,c=input() print((a+b+c)*(b+c-a)*(a+c-b)*(a+b-c))**.5/4 
\$\endgroup\$
4
  • \$\begingroup\$ I'm not terribly familiar with python, but why is line 2 *.5 instead of /2 ? \$\endgroup\$ Commented Apr 3, 2013 at 20:48
  • \$\begingroup\$ @jdstankosky The division operator in Python is by default integer division, so that if the sum of a+b+c is odd, the result will be erroneous. This did change in Python 3, although most golf submission are assumed to be Python 2.7 unless otherwise specified (just as Perl submissions are assumed to be 5.10+, and not Perl 6). \$\endgroup\$ Commented Apr 4, 2013 at 3:22
  • 3
    \$\begingroup\$ You could just say "Python 3" instead of "Python". \$\endgroup\$ Commented Apr 5, 2013 at 18:51
  • 1
    \$\begingroup\$ @JoeZ. Nope. This is Python 2; in Python 3, input() returns a string, breaking this solution. \$\endgroup\$ Commented Oct 18, 2017 at 2:15
4
\$\begingroup\$

GolfScript, 38 characters

~].~++:d\{2*d\-*}/'"#{'\+'**0.5/4}"'+~ 

Since the question didn't specify otherwise I chose to work only on integer lengths. Sides must be given on STDIN separated by spaces.

Example:

> 2 3 4 2.9047375096555625 
\$\endgroup\$
4
\$\begingroup\$

R : 48 43 characters

f=function(...)prod(sum(...)/2-c(0,...))^.5 

Using Heron's formula as well but taking advantage of R's vectorization.
Thanks to @flodel for the idea of the ellipsis.

Usage:

f(2,3,4) [1] 2.904738 f(3,4,5) [1] 6 
\$\endgroup\$
2
  • 1
    \$\begingroup\$ you can drop the curly brackets. And you can gain more by using ellipsis: function(...)prod(sum(...)/2-c(0,...))^.5. Or even function(x)prod(sum(x)/2-c(0,x))^.5 if you call your function with a vector. \$\endgroup\$ Commented Mar 30, 2013 at 2:45
  • \$\begingroup\$ @flodel thanks! I didn't thought of the ellipsis, that's nice. \$\endgroup\$ Commented Mar 30, 2013 at 8:01
4
\$\begingroup\$

Mathematica 20 16 or 22 18 bytes

With 4 bytes saved by @swish.

This returns an exact answer:

Area@SSSTriangle@ 

Example

Area@SSSTriangle[2,3,4] 

image


To return the answer in decimal form, two additional bytes are required.

N@Area@SSSTriangle[2,3,4] 

2.90474

\$\endgroup\$
4
  • \$\begingroup\$ Composition shaves couple of bytes Area@*SSSTriangle \$\endgroup\$ Commented Jun 24, 2019 at 7:29
  • \$\begingroup\$ @swish Thanks, Much appreciated. \$\endgroup\$ Commented Jun 24, 2019 at 8:23
  • \$\begingroup\$ @swish, the multiplication operation, represented by an asterisk, should be dropped. (I suspect you know that.) \$\endgroup\$ Commented Mar 23, 2021 at 9:19
  • \$\begingroup\$ of course mathematica has a builtin for this... \$\endgroup\$ Commented Aug 1 at 10:52
3
\$\begingroup\$

K, 23

{sqrt s**/(s:.5*+/x)-x} 

Example

k){sqrt s**/(s:.5*+/x)-x} 2 3 4 2.904738 
\$\endgroup\$
3
\$\begingroup\$

APL, 23 20 characters

{(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4 

Example:

> {(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4 2.90474 
\$\endgroup\$
3
\$\begingroup\$

Jelly, 7 bytes

SH;_P½ʋ 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Bit late, but here's a cheeky little -1: SH_ŻP½ \$\endgroup\$ Commented Jul 30 at 21:42
2
\$\begingroup\$

Javascript, 88 85

v=prompt().split(/,/g);s=v[0]/2+v[1]/2+v[2]/2;Math.sqrt(s*(s-v[0])*(s-v[1])*(s-v[2])) 

Not good but fun :) Also Heron... Demonstrates the ungolfability of simple problems in JS lol

Note: run from console to see result.

88->85: Removed a, b and c.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can save a bit by only dividing by 2 once. And you don't actually gain anything by assigning to variables: (a=v[0])a is longer than v[0]v[0]. \$\endgroup\$ Commented Mar 27, 2013 at 10:43
  • \$\begingroup\$ If I divided by 2 only one time, like s=(v[0]+v[1]+v[2])/2 with a,b,c=3,4,5 would result in "345"/2=172.5" and not 6. Improved without a, b` and c though. \$\endgroup\$ Commented Mar 27, 2013 at 16:32
  • \$\begingroup\$ Ah, JavaScript's wonderful type system. Ok, s=(-v[0]-v[1]-v[2])/2 and change the other - to +. It's an even number of terms, so it cancels out. \$\endgroup\$ Commented Mar 27, 2013 at 16:39
2
\$\begingroup\$

Tcl, 74 chars.

proc R {a b c} {set s ($a+$b+$c)/2. expr sqrt($s*($s-$a)*($s-$b)*($s-$c))} 

Pass the sides as argument.

For the input 2 3 4 the value of s is (2+3+4)/2. as string. Double evaluation FTW.

\$\endgroup\$
1
  • \$\begingroup\$ As a proc, it extends to only 81 bytes: tio.run/##NYo7CoAwEAV7T/… \$\endgroup\$ Commented Oct 16, 2017 at 13:47
2
\$\begingroup\$

TI-BASIC, 14 12 bytes

4⁻¹√(sum(Ansprod(sum(Ans)-2Ans 

Starting from a Heron's Formula routine written by Kenneth Hammond (Weregoose), I golfed off two bytes. Note that TI-BASIC is tokenized, and each token, like Ans and prod(, is one or two bytes in the calculator's memory.

Input through Ans i.e. in the form {a,b,c}:[program name].

Explained:

 sum(Ans)-2*Ans (a+b+c)-2{a,b,c}={b+c-a,c+a-b,a+b-c} Ans*prod( {a,b,c}*(b+c-a)(c+a-b)(a+b-c) sum( (a+b+c)(b+c-a)(c+a-b)(a+b-c) 4⁻¹*√( √((a+b+c)(b+c-a)(c+a-b)(a+b-c)/16) =√(s(s-a)(s-b)(s-c)) 
\$\endgroup\$
1
  • \$\begingroup\$ I've converted this to a community wiki as it isn't your own work. We don't really have a solid consensus on this, but feel free to weigh in here if you disagree with my decision. \$\endgroup\$ Commented May 21, 2015 at 10:53
2
\$\begingroup\$

C (gcc), 55 bytes

#define f(a,b,c)sqrt((a+b+c)*(a+b-c)*(a-b+c)*(b+c-a))/4 

Try it online!

Yet another implementation of Hero's formula.

\$\endgroup\$
2
\$\begingroup\$

Pip, 17 bytes

RT(_*$*:_-g$+g/2) 

Takes the three side lengths as command-line arguments. Attempt This Online!

Explanation

The same formula as everyone else, but saves a byte using a surprising program structure inspired by ATaco's ARBLE solution:

RT(_*$*:_-g$+g/2) g List of command-line args /2 Divide each by 2 $+ Sum ( ) Call this anonymous function with that value as the argument: _ The argument -g Minus the list of command-line args $*: Product of that list of differences _* Times the argument RT Square root 
\$\endgroup\$
1
\$\begingroup\$

Haskell: 51 (27) characters

readLn>>=(\l->print$sqrt$product$map(sum l/2-)$0:l) 

A very straight-forward implementation of Heron's formula. Example run:

Prelude> readLn>>=(\l->print$sqrt$product$map(sum l/2-)$0:l) [2,3,4] 2.9047375096555625 Prelude> 

Note that it accepts any numeric input, not only integers. And if the input already is in l the solution only needs to be 36 characters long, and if we are not interested in printing the answer the solution only needs to be 30 characters long. What more is that if we can allow ourself to change the input format we can remove 3 more characters. So if our input looks like [2,3,4,0.0] and is already in l we can get our answer with only:

sqrt$product$map(sum l/2-)l 

Example run:

Prelude> let l = [2,3,4,0.0] Prelude> sqrt$product$map(sum l/2-)l 2.9047375096555625 Prelude> 
\$\endgroup\$
1
\$\begingroup\$

PHP, 78 77

<?=sqrt(($s=array_sum($c=fgetcsv(STDIN))/2)*($s-$c[0])*($s-$c[1])*$s-=$c[2]); 

Useage:

php triangle.php 2,3,4 

Output: 2.9047375096556

I don't think I can make it shorter? I'm still new to golfing. Anyone let me know if I overlooked something.

Thanks Primo for saving me 1 byte, lol.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The final ($s-$c[2]) can be replaced with $s-=$c[2] for one byte, but that's all I can see. \$\endgroup\$ Commented Apr 8, 2013 at 9:24
1
\$\begingroup\$

JavaScript (84 86)

s=(eval('abc '.split('').join('=prompt()|0;'))+a+b)/2;Math.sqrt(s*(s-a)*(s-b)*(s-c)) 

Another JavaScript solution based on Heron's formula, but trying a different approach for loading variables. Needs to be run from the console. Each side is entered in a separate prompt.

EDIT: Make use of return value of eval to save 2 characters. Beats @tomsmeding, wahoo! :)

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

Excel, 42 bytes

Based on Heron's formula, high school algebra to golf.

=SQRT(((A1+B1)^2-C1^2)*(C1^2-(A1-B1)^2))/4 

Ungolfed / Unalgebra'ed

=SQRT((A1+B1+C1)/2*(((A1+B1+C1)/2)-A1)*(((A1+B1+C1)/2)-B1)*(((A1+B1+C1)/2)-C1)) 
\$\endgroup\$
1
\$\begingroup\$

ARBLE, 36 bytes

sqrt(a*(a-b)*(a-c)*(a-d))((b+c+d)/2) 

Heron's formula.

Try it online!

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

Julia 0.6.0, 48 bytes

Basically heron's formula:

f(a,b,c)=(p=(a+b+c)/2;sqrt(p*(p-a)*(p-b)*(p-c))) 
\$\endgroup\$
1
\$\begingroup\$

dc, 34 bytes

9ksssddlsld++2/d3R-rdls-rdld-***vp 

Try it online!

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

Japt, 17 16 15 13 bytes

mnU=x*½)׬*U¬ 

Try it

Saved 2 bytes thanks to ETH.

mnU=x*½)׬*U¬ :Implicit input of array U m :Map n : Subtract from U= : Reassign to U x : Reduce by addition *½ : After multiplying each by 0.5 ) :End map × :Reduce by multiplication ¬ :Square root *U¬ :Multiplied by the square root of U 
\$\endgroup\$
1
  • \$\begingroup\$ I think you can use any of these for the second line, too: NmnU ×*U q, NmnU r*U q, Np0 mnU ×q \$\endgroup\$ Commented Jul 26, 2017 at 14:42
1
\$\begingroup\$

Desmos, 42 bytes

Uses Heron's formula.

s={a+b+c}/2 f(a,b,c)=(s(s-a)(s-b)(s-c))^.5 

Try this online!

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

Perl 5 -MList::Util=sum -ap, 40 bytes

$r=$t=.5*sum@F;map$r*=$t-$_,@F;$_=sqrt$r 

Try it online!

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

Stax, 10 bytes

╝0∞♀»♦▓y╩╪ 

Run and debug it

Operates triples of floating point numbers. Uses Heron's Formula

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

APL(NARS), 16 chars, 32 bytes

{√×/(+/⍵÷2)-0,⍵} 

One has to cenvert the Erone formula if a, b, c are sides of triangle

p =(a+b+c)/2 Area=√p*(p-a)(p-b)(p-c) 

to APL language... test

 f←{√×/(+/⍵÷2)-0,⍵} f 2 3 4 2.90473751 f 3 4 5 6 
\$\endgroup\$
0
\$\begingroup\$

APL(NARS), 14 chars

{√×/∊⍵0-+/⍵÷2} 

test:

 {√×/∊⍵0-+/⍵÷2}3 4 5 6 
\$\endgroup\$
0
\$\begingroup\$

AWK, 41 bytes

$0=sqrt(4*$1^2*$2^2-($1^2+$2^2-$3^2)^2)/4 

Attempt This Online!

Had to do some research to find the tightest formula.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.