| Bytes | Lang | Time | Link |
|---|---|---|---|
| 042 | Desmos | 250801T010838Z | ErikDaPa |
| 017 | Pip | 250730T204239Z | DLosc |
| 041 | AWK | 250730T170510Z | xrs |
| 014 | APLNARS | 250509T183802Z | RosLuP |
| 013 | Japt | 170726T082440Z | Shaggy |
| 007 | Jelly | 210323T064001Z | Adam |
| 018 | Mathematica | 171018T021516Z | DavidC |
| 055 | C gcc | 190624T072429Z | ceilingc |
| 032 | APLNARS | 190621T062305Z | user5898 |
| 010 | Stax | 190621T025002Z | recursiv |
| 040 | Perl 5 MListUtil=sum ap | 190621T021722Z | Xcali |
| 034 | dc | 190621T005617Z | jnfnt |
| 012 | TIBASIC | 150521T031258Z | lirtosia |
| 048 | Julia 0.6.0 | 170726T211057Z | Goysa |
| 036 | ARBLE | 171018T012425Z | ATaco |
| 074 | Tcl | 130411T163141Z | Johannes |
| 042 | Excel | 170726T094711Z | Wernisch |
| 023 | Mathematica | 130327T040621Z | chyanog |
| nan | 130411T134652Z | mellamok | |
| 077 | PHP | 130401T190703Z | jdstanko |
| 027 | Haskell 51 | 130404T043328Z | Fors |
| 043 | R | 130326T130409Z | plannapu |
| 085 | Javascript | 130327T082750Z | tomsmedi |
| 019 | J | 130326T184816Z | randomra |
| 020 | APL | 130326T144915Z | Graham |
| 020 | APL | 130326T134817Z | Howard |
| 023 | K | 130326T193905Z | tmartin |
| 053 | Python 2 | 130326T155729Z | flornqua |
| 038 | GolfScript | 130326T132746Z | Howard |
| 057 | Python | 130326T112709Z | primo |
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
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¬
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
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]
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
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
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)))
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
