| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | AWK | 250910T142308Z | xrs |
| 043 | JavaScript | 230305T003226Z | EzioMerc |
| 014 | TIBasic | 230304T151850Z | Youserna |
| 018 | Pip | 230304T154759Z | The Thon |
| nan | 230304T154026Z | The Thon | |
| 010 | Pyt | 230304T153045Z | Kip the |
| 038 | Arturo | 230304T031914Z | chunes |
| 027 | Raku | 220916T191201Z | Sean |
| nan | Fig | 220915T210346Z | Seggan |
| 051 | C clang | 220728T090414Z | Noodle9 |
| 050 | Knight | 220803T202418Z | 97.100.9 |
| 033 | x86 32bit machine code | 220731T095645Z | m90 |
| 035 | Factor + math.unicode | 220728T024152Z | chunes |
| 043 | JavaScript | 220730T214305Z | noodle p |
| 052 | C# Visual C# Interactive Compiler | 220730T010913Z | Gymhgy |
| 1716 | J | 220728T020507Z | Jonah |
| 013 | Pyth | 220728T235006Z | math jun |
| 037 | Perl 5 MListUtil=product | 220728T221749Z | Xcali |
| 044 | Ruby | 220728T212651Z | Level Ri |
| 009 | MathGolf | 220728T090908Z | Kevin Cr |
| 013 | K ngn/k | 220728T011656Z | Bubbler |
| 014 | BQN | 220728T085828Z | Dominic |
| 036 | Desmos | 220728T084956Z | Aiden Ch |
| 015 | Burlesque | 220728T083306Z | DeathInc |
| 038 | Haskell | 220728T075737Z | alephalp |
| 028 | R | 220728T062547Z | pajonk |
| 008 | 05AB1E | 220728T065004Z | Kevin Cr |
| 011 | Charcoal | 220728T060943Z | Neil |
| 025 | lin | 220728T054709Z | Mama Fun |
| 020 | Wolfram Language Mathematica | 220728T012858Z | alephalp |
| 015 | Dyalog APL | 220728T032401Z | rabbitgr |
| 032 | Python NumPy | 220728T031059Z | loopy wa |
| 006 | Jelly | 220728T022615Z | emanresu |
| 008 | Vyxal | 220728T004413Z | lyxal |
| 047 | Python | 220728T010648Z | naffetS |
JavaScript, 43 bytes
Tried to beat 43 bytes but got a different form :)
$$ \sqrt{s(s-a)(s-b)(s-c)} $$
where \$s=\frac{a + b + c}{2}\$ is equal to:
$$ \frac{1}{4}\sqrt{4abd - d^2} $$
where \$d=(a + b)^2 - c^2\$
(a,b,c)=>(4*a*b*(d=(a+b)**2-c*c)-d*d)**.5/4
Try it:
f=(a,b,c)=>(4*a*b*(d=(a+b)**2-c*c)-d*d)**.5/4
;[
[1, 1, 1], // 0.4330
[3, 4, 5], // 6.0000
[9, 3, 7], // 8.7856
].forEach(([a,b,c])=>console.log(f(a,b,c)))
TI-Basic, 14 bytes
√(sum(Ans)/2prod(sum(Ans)/2-Ans
or
.25√(sum(Ans)prod(sum(Ans)-2Ans
Takes input in Ans as a list.
Pip, 18 bytes
RT$*Y($+YgPE0)/2-y
Port of Kevin Cruijssen's 05AB1E answer.
Explanation
RT$*Y($+YgPE0)/2-y # Input on command line
gPE0 # Prepend 0 to the input
$+Y # Sum and store in y
( )/2 # Halve
-y # And subtract y
$*Y # Get the product
RT # And square root
# Implicit output
Thunno, \$ 10 \log_{256}(96) \approx \$ 8.23 bytes
0ZNDS2/_Pt
Port of Kevin Cruijssen's 05AB1E answer.
Explanation
0ZNDS2/_Pt # Implicit input
0ZN # Prepend a 0
DS # Duplicate and sum
2/ # Halve
_ # Subtract the list
P # Product
t # Square root
# Implicit output
Pyt, 10 bytes
ĐƩᵮ₂Đ↔-Π*√
Straightforward implementation of Heron's formula
Đ implicit input; Đuplicate
Ʃ Ʃum
ᵮ₂ cast to ᵮloat; halve
Đ Đuplicate
↔ flip stack
- subtract
Π take product of list
* multiply by s
√ square root; implicit print
Raku, 27 bytes
{sqrt [*] @_.sum/2 X-0,|@_}
@_ is the array of arguments to the function: a, b, and c. @_.sum / 2 is half the sum of the arguments, what the problem statement calls s. X- subtracts the numbers 0 and |@_, the flattened arguments, from that number, producing the numbers s, s - a, s - b, and s - c. Then [*] multiplies them together and sqrt takes the square root.
Fig, \$9\log_{256}(96)\approx\$ 7.408 bytes
mqr-J0xHS
Jelly beats me... again. Takes input as a list of the side lengths.
mqr-J0xHS
S # Sum the lengths
H # Halve it
- # That ^ minus...
x # The input
J0 # With a 0 prepended
r # Product
mq # Square root
C (clang), 59 51 bytes
#define f(a,b,c)sqrt(4*a*a*b*b-(c=a*a+b*b-c*c)*c)/4
Saved 8 bytes thanks to jdt!!!
Knight, 50 bytes
;=x=s-**4=a^P2=b^P2^+a-b^P2 2;W>x=x/+x/s x 2xO/x 4
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
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
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
vhaddps xmm1, xmm0, xmm0
vhaddps xmm1, xmm1, xmm1
movss [ecx], xmm1
fld DWORD PTR [ecx]
cmc
jc r
fadd st(0), st(0)
fld st(1)
fmulp st(2), st(0)
fsubp
fsqrt
pop eax
push 4
fidiv DWORD PTR [ecx]
pop eax
jmp edx
Factor + math.unicode, 36 35 bytes
[ dup Σ 2 / dup rot n-v Π * √ ]
-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
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)
C# (Visual C# Interactive Compiler), 52 bytes
(a,b,c)=>Math.Sqrt((a-(a=(a+b+c)/2))*(b-a)*(c-a)*-a)
J, 17 16 bytes
2%:[:*/0&,-+/%2:
-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,+:
0,+:Double each input and prepend 0+/...-Subtract each of those from sum of input-:@And halve each result[:*/Product2%:Root
Ruby, 44 bytes
->a,b,c{(4*a*a*b*b-(a*a+b*b-c*c)**2)**0.5/4}
Ruby, 45 bytes
->a{a.inject(s=a.sum/2.0){|p,i|p*(s-i)}**0.5}
MathGolf, 9 bytes
0Γ_Σ½-ε*√
I/O as decimals.
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)
K (ngn/k), 13 bytes
{%*/x-/x%2}0,
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}
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
BQN, 14 bytes
√·×´+´∘÷⟜2-0⊸∾
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
Desmos, 36 bytes
f(l)=(total(ll)^2-2l^4.total)^{.5}/4
Input is a list of the three side lengths.
Port of Steffan's Python answer also gives 36 bytes:
f(a,b,c)=(4aabb-(aa+bb-cc)^2)^{.5}/4
Burlesque, 15 bytes
0+]J++2./?-pdr@
A port of Kevin Cruijssen's 05AB1E answer
0+] # Prepend 0
J # Duplicate
++ # Sum
2./ # Halve
?- # Difference with original
pd # Product
r@ # Square root
Haskell, 38 bytes
sqrt.product.(map=<<(-).(/2).sum).(0:)
Haskell, 38 bytes
sqrt.product.(map.(-).(/2).sum<*>(0:))
With the help of pointfree.io.
R, 30 28 bytes
\(x)prod(sum(x)/2-c(0,x))^.5
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
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)}$$
05AB1E, 8 bytes
0šDO;αPt
Try it online or verify all test cases.
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)
Charcoal, 11 bytes
I₂Π⁻⊘Σθ⊞Oθ⁰
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`
⊞O Appended to
θ Input array
Π Take the product
₂ Take the square root
I Cast to string
Implicitly print
lin, 25 bytes
.+ \+ `/2/.~0' - \* `/.5^
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
Wolfram Language (Mathematica), 20 bytes
N@*Area@*SSSTriangle
Built-in.
Wolfram Language (Mathematica), 22 bytes
((#.#)^2-2#.#^3)^.5/4&
-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&
Based on chyanog's answer to another challenge.
Using the formlula \$A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}\$.
Dyalog APL, 16 15 bytes
-1 thanks to @Bubbler.
.5*⍨0∘,×.-2÷⍨+/
┌──┼───────┐
.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.
Python NumPy, 32 bytes
lambda v:((v@v-2*v*v)*v@v)**.5/4
Previous Python NumPy, 33 bytes
lambda v:(v@v*v@v-2*v**3@v)**.5/4
Previous Python NumPy, 36 bytes (@alephalpha)
lambda v:(v@v*v@v-2*v**2@v**2)**.5/4
Previous Python NumPy, 37 bytes
lambda v:((v@v)**2-2*v**2@v**2)**.5/4
Expects a numpy vector containing the three side lenghts.
Jelly, 6 bytes
HSạŻP½
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
Vyxal, 9 8 bytes
∑½₌N-Π*√
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
Python, 47 bytes
lambda a,b,c:(4*a*a*b*b-(a*a+b*b-c*c)**2)**.5/4
Classic boring solution. There might be a shorter way to express this formula, but I can't find it.