g | x | w | all
Bytes Lang Time Link
042Desmos250801T010838ZErikDaPa
017Pip250730T204239ZDLosc
041AWK250730T170510Zxrs
014APLNARS250509T183802ZRosLuP
013Japt170726T082440ZShaggy
007Jelly210323T064001ZAdam
018Mathematica171018T021516ZDavidC
055C gcc190624T072429Zceilingc
032APLNARS190621T062305Zuser5898
010Stax190621T025002Zrecursiv
040Perl 5 MListUtil=sum ap190621T021722ZXcali
034dc190621T005617Zjnfnt
012TIBASIC150521T031258Zlirtosia
048Julia 0.6.0170726T211057ZGoysa
036ARBLE171018T012425ZATaco
074Tcl130411T163141ZJohannes
042Excel170726T094711ZWernisch
023Mathematica130327T040621Zchyanog
nan130411T134652Zmellamok
077PHP130401T190703Zjdstanko
027Haskell 51130404T043328ZFors
043R130326T130409Zplannapu
085Javascript130327T082750Ztomsmedi
019J130326T184816Zrandomra
020APL130326T144915ZGraham
020APL130326T134817ZHoward
023K130326T193905Ztmartin
053Python 2130326T155729Zflornqua
038GolfScript130326T132746ZHoward
057Python130326T112709Zprimo

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!

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

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.

APL(NARS), 14 chars

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

test:

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

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

Jelly, 7 bytes

SH;_P½ʋ

Try it online!

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

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.

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

Stax, 10 bytes

╝0∞♀»♦▓y╩╪

Run and debug it

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

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

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

Try it online!

dc, 34 bytes

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

Try it online!

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

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

ARBLE, 36 bytes

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

Heron's formula.

Try it online!

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.

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

Mathematica 23

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

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

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.

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>

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

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.

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

APL, 23 20 characters

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

Example:

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

K, 23

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

Example

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

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

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

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