# Triangle area from side lengths

Output the area $$\A\$$ of a triangle given its side lengths $$\a, b, c\$$ as inputs. This can be computed using Heron's formula:

$$A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}.$$

This can be written in various ways, such as

$$A= \frac{1}{4}\sqrt{(a+b+c)(-a+b+c)(a-b+c)(a+b-c)}$$ $$A= \frac{1}{4}\sqrt{(a^2+b^2+c^2)^2-2(a^4+b^4+c^4)}$$

See Wikipedia for more. Related: What are my dimensions?

The inputs will be three positive integers that satisfy the triangle inequality and so are sides of a non-degenerate triangle. While the order of the three sides doesn't affect the output, you may not assume they're given in a particular order like sorted. You may take inputs in a list or tuple or the like. Any reasonable float output is fine.

Test cases

1 1 1 -> 0.4330
3 4 5 -> 6.0000
9 3 7 -> 8.7856

• Jul 28 at 1:32
• Similar challenge on anagol (round to int) Jul 28 at 1:43
• Can you output an exact real number, if the language natively supports such a type? Jul 29 at 17:45
• @A.Rex What do you mean, like outputting sqrt(n)?
– xnor
Jul 29 at 23:10
• @xnor: Can the Mathematica answer be shortened by removing the call to N[..], which returns a numeric approximation of an exact number? Jul 31 at 12:20

# Wolfram Language (Mathematica), 20 bytes

N@*Area@*SSSTriangle


Try it online!

Built-in.

# Wolfram Language (Mathematica), 22 bytes

((#.#)^2-2#.#^3)^.5/4&


Try it online!

-2 bytes by porting loopy walt's Python answer.

Takes input as a list. Using the formula $$\A= \frac{1}{4}\sqrt{(a^2+b^2+c^2)^2-2(a^4+b^4+c^4)}\$$.

# Wolfram Language (Mathematica), 24 bytes

1##&@@(+##/2-{0,##})^.5&


Try it online!

Using the formlula $$\A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}\$$.

• What an expressive name of a builtin. Jul 28 at 2:07
• As it is named as SSSTriangle... Is there something like SASTriangle, ASATriangle, AASTriangle, or even HLTriangle?
– tsh
Jul 29 at 6:56
• @tsh Try it online! Jul 29 at 7:01
• Jul 29 at 7:08

# Dyalog APL, 16 15 bytes

-1 thanks to @Bubbler.

.5*⍨0∘,×.-2÷⍨+/


Attempt This Online!

┌──┼───────┐
.5 *⍨  ┌───┼────┐
0∘, ×.- ┌─┼──┐
2 ÷⍨ +/

0∘,          left  argument: input with 0 prepended     0 a b c
2÷⍨+/  right argument: sum of input divided by 2  (a+b+c)/2 = s
×.-       left minus right, then take the product    (0-s)(a-s)(b-s)(c-s)
.5*⍨             square root


(0-s)(a-s)(b-s)(c-s) is equivalent to (s-0)(s-a)(s-b)(s-c) as there is an even number of items being multiplied.

• You don't need ⍨ after - since there are 4 items to be multiplied. Jul 28 at 3:29

# K (ngn/k), 13 bytes

{%*/x-/x%2}0,


Try it online!

There is! -2 bytes thanks to @ovs.

{%*/x-/x%2}0,  x: a length-3 array containing the three sides
0,  prepend a 0
x%2     (0; a/2; b/2; c/2)
x-/        (0 a b c) - a/2 - b/2 - c/2
%*/           sqrt(product of the above)


# K (ngn/k), 16 bytes

{%-s*/x-s:+/x%2}


Try it online!

There must be a shorter way..?

{%-s*/x-s:+/x%2}  x: a length-3 array containing the three sides
s:+/x%2   s: half sum of x
x-          (a-s; b-s; c-s)
-s*/            -s * (a-s) * (b-s) * (c-s)
%                sqrt


# Python, 47 bytes

lambda a,b,c:(4*a*a*b*b-(a*a+b*b-c*c)**2)**.5/4

Attempt This Online!

Classic boring solution. There might be a shorter way to express this formula, but I can't find it.

# Jelly, 6 bytes

HSạŻP½


Try it online!

Don't ask me why the chaining works...

H      # Half of
S     # The sum of the input
ạ    # Absolute difference with
Ż   # The input, with a 0 prepended
P½ # Take the square root of the product

• github.com/DennisMitchell/jellylanguage/wiki/Tutorial 4.2 "Then the following patterns are matched against, from top to bottom:" Jul 28 at 18:31
• Yeah, I don't see anything strange about the chaining. Jul 28 at 20:17

# J, 17 16 bytes

2%:[:*/0&,-+/%2:


Try it online!

-1 thanks to Bubbler's K approach

The interesting insight (obvious when spelled out but not always when searching for a golf) is: When taking the product of an even number of elements, it is equivalent to taking the product of their negatives.

## original, 17 bytes

2%:[:*/+/-:@-0,+:


Try it online!

• 0,+: Double each input and prepend 0
• +/...- Subtract each of those from sum of input
• -:@ And halve each result
• [:*/ Product
• 2%: Root

# Python NumPy, 32 bytes

lambda v:((v@v-2*v*v)*v@v)**.5/4

Attempt This Online!

### Previous Python NumPy, 33 bytes

lambda v:(v@v*v@v-2*v**3@v)**.5/4

Attempt This Online!

### Previous Python NumPy, 36 bytes (@alephalpha)

lambda v:(v@v*v@v-2*v**2@v**2)**.5/4

Attempt This Online!

### Previous Python NumPy, 37 bytes

lambda v:((v@v)**2-2*v**2@v**2)**.5/4

Attempt This Online!

Expects a numpy vector containing the three side lenghts.

• (v@v)**2 -> v@v*v@v. Jul 28 at 3:16
• Are you allowed to leave out the import numpy statement, per meta rules? Jul 28 at 7:27
• Yes @solid.py Jul 28 at 7:32
• @solid.py If you could use them without directly referencing them which looks tricky to me. Jul 28 at 7:48
• @solid.py Sorry, I didn't mean to be rude. Arguably, any module that defines new types that have useful functionality accessible through methods or overloaded operators would qualify. Jul 28 at 13:54

# 05AB1E, 8 bytes

0šDO;αPt


Explanation:

Uses the default formula:

$$s=\frac{0+a+b+c}{2}$$ $$A=\sqrt{abs(0-s)\times abs(a-s)\times abs(b-s)\times abs(c-s)}$$

0š        # Prepend a 0 in front of the (implicit) input-triplet
D       # Duplicate the list
O      # Sum the copy
;     # Halve the sum
α    # Get the absolute difference between this sum and the values in the list
P   # Take the product
t  # Square root
# (which is output implicitly as result)

• The signed difference will work just as well as the average difference (it doesn't matter if you have (a-s) instead of (s-a) because over the 4 terms the overall result will be positive.) I'm guessing that won't make it any shorter though? Jul 28 at 21:43
• @LevelRiverSt I know. I've indeed just used subtracts instead of absolute difference in my ported MathGolf answer for that reason. But changing the α to - indeed won't make a difference in byte-count. Jul 29 at 7:08

# R, 30 28 bytes

\(x)prod(sum(x)/2-c(0,x))^.5

Attempt This Online!

Inspired by @Kevin Cruijssen's 05AB1E answer.

### R, 32 30 bytes

Edit: -2 bytes by looking at @loopywalt's answer.

\(x)((x%*%x)^2-2*x^3%*%x)^.5/4

Attempt This Online!

Takes input as a vector of sides.

Uses the formula $$A= \frac{1}{4}\sqrt{(a^2+b^2+c^2)^2-2(a^4+b^4+c^4)}$$

with observation that sum of squares of elements from x is a dot product of x with itself. So with $$\x=[a,b,c]\$$ and $$\y=[a^3,b^3,c^3]\$$:

$$A= \frac{1}{4}\sqrt{(x \cdot x)^2-2(y\cdot x)}$$

• Note that the 28-byte answer is also essentially identical to plannapus's answer to the previous challenge from 9 years ago... Jul 28 at 8:35
• @DominicvanEssen indeed, thanks for that reference. Jul 28 at 9:13

# Vyxal, 9 8 bytes

∑½₌N-Π*√


Try it Online!

A 7 byter with -r

Outputs as a fraction representation. Try it Online! if you want decimals as output.

Takes the 3 side lengths as a list of numbers.

Quite literal implementation of Heron's formula, with some extra insight from Bubbler's answer.

## Explained

∑½₌N-Π*√
∑½       # Half sum of the side lengths
₌N-    # Push the half sum negated, as well as each side length minus the half sum
Π*  # Take the product of the subtracted side lengths and multiply it by the negated half sum
√ # Take the square root of that


sqrt.product.(map=<<(-).(/2).sum).(0:)

Attempt This Online!

sqrt.product.(map.(-).(/2).sum<*>(0:))

Attempt This Online!

With the help of pointfree.io.

• I tried to write a SML answer, but it's very annoying as I don't know of ways of doing the 0 append pointfree, or doing sum and product without defining my own functions (i don't know of builtins that do it).
– qwr
Jul 30 at 2:10

# MathGolf, 9 bytes

0Γ_Σ½-ε*√


I/O as decimals.

Try it online.

Explanation:

Similar approach as my 05AB1E answer, except with subtract instead of absolute difference, since with four values the negatives balance themselves out anyway.

0          # Push a 0
Γ         # Wrap the top four items into a list (using the three implicit inputs)
_        # Duplicate this list
Σ       # Sum
½      # Halve
-     # Subtract it from each value in the list
ε*   # Product: reduce by multiplication
√  # Square root
# (after which the entire stack is output implicitly as result)


# Factor + math.unicode, 36 35 bytes

[ dup Σ 2 / dup rot n-v Π * √ ]


Try it online!

-1 byte because I remembered √ is 1 byte shorter than sqrt.

        ! { 3 4 5 }
dup     ! { 3 4 5 } { 3 4 5 }
Σ       ! { 3 4 5 } 12
2       ! { 3 4 5 } 12 2
/       ! { 3 4 5 } 6
dup     ! { 3 4 5 } 6 6
rot     ! 6 6 { 3 4 5 }
n-v     ! 6 { 3 2 1 }
Π       ! 6 6
*       ! 36
√       ! 6.0


# Burlesque, 15 bytes

0+]J++2./?-pdr@


Try it online!

A port of Kevin Cruijssen's 05AB1E answer

0+] # Prepend 0
J   # Duplicate
++  # Sum
2./ # Halve
?-  # Difference with original
pd  # Product
r@  # Square root


# Desmos, 36 bytes

f(l)=(total(ll)^2-2l^4.total)^{.5}/4


Input is a list of the three side lengths.

Try It On Desmos!

Port of Steffan's Python answer also gives 36 bytes:

f(a,b,c)=(4aabb-(aa+bb-cc)^2)^{.5}/4


# C (clang), 59 bytes

float f(a,b,c){c=a*a+b*b-c*c;return sqrt(4*a*a*b*b-c*c)/4;}


Try it online!

# BQN, 14 bytes

√·×´+´∘÷⟜2-0⊸∾


Try it at BQN REPL

            0⊸∾     # each element of the argument, prepended by a zero
-        # subtracted from
+´∘             # the sum of
÷⟜2          # each element of the argument divided by 2
×´                # get the product (fold-multiply),
√·                  # and take the square root


## JavaScript, 43 bytes (@Steffan)

Ported from Steffan's Python answer by him:

f=
(a,b,c)=>(4*a*a*b*b-(a*a+b*b-c*c)**2)**.5/4

console.log(f(1, 1, 1), 0.4330)
console.log(f(3, 4, 5), 6.0000)
console.log(f(9, 3, 7), 8.7856)

## JavaScript, 46 bytes (me)

Boring answer :/ but couldn't think of anything better. $$\A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}\$$.

f=
(a,b,c,s=(a+b+c)/2)=>(s*(s-a)*(s-b)*(s-c))**.5

console.log(f(1, 1, 1), 0.4330)
console.log(f(3, 4, 5), 6.0000)
console.log(f(9, 3, 7), 8.7856)

• 43 bytes by porting my Python answer: (a,b,c)=>(4*a*a*b*b-(a*a+b*b-c*c)**2)**.5/4 Jul 30 at 22:18

# x86 32-bit machine code, 33 bytes

6A 03 59 D9 E8 D9 EE DC E1 DA 04 8C E2 FB D9 FD 6A 03 59 D9 C0 DA 2C 8C DE CA E2 F7 DE C9 D9 FA C3


Try it online!

Uses the cdecl calling convention, taking three 32-bit integers on the stack and returning a value on the FPU register stack.

In assembly:

f:  push 3; pop ecx     # Set ECX to 3.
fld1                # Push 1 onto the FPU register stack.
fldz                # Push 0 onto the FPU register stack.
fsubr st(1), st(0)  # Change the 1 to 0-1=-1.
l0: fiadd DWORD PTR [esp+4*ecx] # Add an argument to the top value.
loop l0                     # Loop 3 times, making the top value a+b+c.
fscale              # Multiply the top value by 2^(value below)=2^-1=1/2.
push 3; pop ecx     # Set ECX to 3 again.
l1: fld st(0)                   # Duplicate the top value, which is s.
fisubr DWORD PTR [esp+4*ecx]# Change the top value to a-s or b-s or c-s.
fmulp st(2), st(0)          # Multiply the third-from-top value
# (which was -1) by that and pop it.
loop l1                     # Loop 3 times.
# The FPU register stack is now -(a-s)(b-s)(c-s), s.
fmulp st(1), st(0)  # Multiply those values and pop, leaving the product.
fsqrt               # Take the square root.
ret                 # Return.


I had another version using SIMD instructions to do multiple calculations at once, but it was longer, at 48 bytes:

5A 6A 00 89 E1 F8 C5 F8 5B 01 0F 59 C0 C5 FB 7C C8 C5 F3 7C C9 F3 0F 11 09 D9 01 F5 72 EC D8 C0 D9 C1 DE CA DE E9 D9 FA 58 6A 04 DA 31 58 FF E2

f:  pop edx
push 0
mov ecx, esp
clc
vcvtdq2ps xmm0, [ecx]
r:  mulps xmm0, xmm0
movss [ecx], xmm1
fld DWORD PTR [ecx]
cmc
jc r
fld st(1)
fmulp st(2), st(0)
fsubp
fsqrt
pop eax
push 4
fidiv DWORD PTR [ecx]
pop eax
jmp edx


# Knight, 50 bytes

;=x=s-**4=a^P2=b^P2^+a-b^P2 2;W>x=x/+x/s x 2xO/x 4


Try it online!

This is the elusive "triple Heron" answer:

• Because of the limitations of Knight, this only works for Heronian triangles (triangles with integer sides and area)
• It uses Heron's formula for the area of the triangle
• Because Knight doesn't have square root, I implemented it using Heron's method.

(It doesn't work in the TIO because it hasn't implemented the no-op.) It turns out I just misread the spec.

Here's the expanded code:

;=a^P2
;=b^P2
;=c^P2
;=s (-(*(*4a) b)(^(+a(-b c)) 2))
;=x s
;WHILE >x (=x(/+x(/s x) 2))
x
OUTPUT /x 4

• For a no-op, you can just use 1 or something. It will work on TIO. Also, : is treated as whitespace, so it doesn't take up the whole while block. Aug 3 at 21:03
• @Steffan I can't use a number in this case because it would get parsed with the 2. Is : actually completely equivalent to whitespace?
Aug 3 at 21:05
• Yes, : is equivalent to whitespace. It's documented in the spec. You can also use one of NTF, but it will be grouped with O Aug 3 at 21:06
• The spec says that it "can (usually) be considered whitespace", not that it's always the same as whitespace.
Aug 3 at 21:12
• Actually, you're right. But this code is still invalid. : takes an argument, and it will consume O/x 4 for that argument. And it is implemented in TIO. In this code, it is equivalent to whitespace. It is only not equivalent at the end of a program. Aug 3 at 21:14

# lin, 25 bytes

.+ \+ /2/.~0' - \* /.5^


Try it here!

For testing purposes (use -i flag if running locally):

[1 1 1] ;
.+ \+ /2/.~0' - \* /.5^


## Explanation

Prettified code:

.+ \+ / 2/.~ 0' - \* / .5^

• .+ \+ / 2/ duplicate, half-sum of input
• .~ 0' swap, append 0
• - \* / .5^ vector subtract, product, square root

# Charcoal, 11 bytes

Ｉ₂Π⁻⊘Σθ⊞Ｏθ⁰


Try it online! Link is to verbose version of code. Takes input as an array. Explanation:

      θ     Input array
Σ      Summed
⊘       Halved
⁻        Vectorised subtract
⁰ Literal integer 0
⊞Ｏ   Appended to
θ  Input array
Π         Take the product
₂          Take the square root
Ｉ           Cast to string
Implicitly print


# Ruby, 44 bytes

->a,b,c{(4*a*a*b*b-(a*a+b*b-c*c)**2)**0.5/4}


Try it online!

# Ruby, 45 bytes

->a{a.inject(s=a.sum/2.0){|p,i|p*(s-i)}**0.5}


Try it online!

# Perl 5-MList::Util=product,sum -pal, 37 bytes

$_=sqrt product map{sum(@F)/2-$_}0,@F


Try it online!

# Pyth, 14 13 bytes

@*F-LcsQ2+QZ2


Try it online!

# C# (Visual C# Interactive Compiler), 52 bytes

(a,b,c)=>Math.Sqrt((a-(a=(a+b+c)/2))*(b-a)*(c-a)*-a)


Try it online!