# Triangle Area Side Side Side [closed]

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.

• What is the required precision? Can the input and outputs be integers? Rounded to the nearest hundredth? Single precision floats? Rationals? Mar 22, 2021 at 22:39

## 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:)

## APL 21 20

Takes screen input via ←⎕

(×/(+/t÷2)-0,t←⎕)*.5


Mathematica 23

√Times@@(+##/2-{0,##})&

• 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& Mar 27, 2013 at 12:49
• 28 chars, as a function (Tr@#Times@@(Tr@#-2#))^.5/4&, or 27 using a variable Mar 27, 2013 at 13:09
• @belisarius Thanks for your suggestion. Mar 28, 2013 at 4:29

# 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

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

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

• I'm not terribly familiar with python, but why is line 2 *.5 instead of /2 ? Apr 3, 2013 at 20:48
• @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). Apr 4, 2013 at 3:22
• You could just say "Python 3" instead of "Python". Apr 5, 2013 at 18:51
• @JoeZ. Nope. This is Python 2; in Python 3, input() returns a string, breaking this solution. Oct 18, 2017 at 2:15

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


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

• 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. Mar 30, 2013 at 2:45
• @flodel thanks! I didn't thought of the ellipsis, that's nice. Mar 30, 2013 at 8:01

# Mathematica 20 16 or 22 18 bytes

With 4 bytes saved by @swish.

Area@SSSTriangle@


Example

Area@SSSTriangle[2,3,4]


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

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


2.90474

• Composition shaves couple of bytes Area@*SSSTriangle Jun 24, 2019 at 7:29
• @swish Thanks, Much appreciated. Jun 24, 2019 at 8:23
• @swish, the multiplication operation, represented by an asterisk, should be dropped. (I suspect you know that.) Mar 23, 2021 at 9:19

# K, 23

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


Example

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


### APL, 23 20 characters

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


Example:

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


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

• 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]. Mar 27, 2013 at 10:43
• 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. Mar 27, 2013 at 16:32
• 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. Mar 27, 2013 at 16:39

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

# Jelly, 7 bytes

SH;_P½ʋ


Try it online!

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>


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

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

## 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! :)

# 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))


# Japt, 1716 15 bytes

½*Nx
NmnU ×*U q


Test it

Saved 2 bytes thanks to ETH pointing out a redundant newline and some alternative ways to reduce the array.

• I think you can use any of these for the second line, too: NmnU ×*U q, NmnU r*U q, Np0 mnU ×q Jul 26, 2017 at 14:42

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

• As a proc, it extends to only 81 bytes: tio.run/##NYo7CoAwEAV7T/… Oct 16, 2017 at 13:47

## 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)))


# 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))

• 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. May 21, 2015 at 10:53

# dc, 34 bytes

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


Try it online!

# ARBLE, 36 bytes

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


Heron's formula.

Try it online!

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

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


Try it online!

# Stax, 10 bytes

╝0∞♀»♦▓y╩╪


Run and debug it

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

# 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
`