Triangle Area Side Side Side

18

2

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

Assume the three side a,b,c always a>0,b>0,c>0,a+b>c,b+c>a,c+a>b.

chyanog

Posted 2013-03-26T11:18:09.207

Reputation: 1 078

Answers

6

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

randomra

Posted 2013-03-26T11:18:09.207

Reputation: 19 909

7

APL 21 20

Takes screen input via ←⎕

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

Graham

Posted 2013-03-26T11:18:09.207

Reputation: 3 184

7

Mathematica 23

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

chyanog

Posted 2013-03-26T11:18:09.207

Reputation: 1 078

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& – Dr. belisarius – 2013-03-27T12:49:03.550

228 chars, as a function (Tr@#Times@@(Tr@#-2#))^.5/4&, or 27 using a variable – Dr. belisarius – 2013-03-27T13:09:28.567

@belisarius Thanks for your suggestion. – chyanog – 2013-03-28T04:29:58.667

6

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

flornquake

Posted 2013-03-26T11:18:09.207

Reputation: 1 467

3I wasted a lot of time trying to come up with a better solution. I'm convinced this is as good as it gets – jamylak – 2013-03-30T11:02:13.823

2@jamylak you and me both ;) – primo – 2013-03-31T15:33:39.010

5

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

primo

Posted 2013-03-26T11:18:09.207

Reputation: 30 891

I'm not terribly familiar with python, but why is line 2 *.5 instead of /2 ? – jdstankosky – 2013-04-03T20:48:01.327

@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). – primo – 2013-04-04T03:22:08.557

3You could just say "Python 3" instead of "Python". – Joe Z. – 2013-04-05T18:51:53.903

1@JoeZ. Nope. This is Python 2; in Python 3, input() returns a string, breaking this solution. – Khuldraeseth na'Barya – 2017-10-18T02:15:11.027

4

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

Howard

Posted 2013-03-26T11:18:09.207

Reputation: 23 109

3

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

plannapus

Posted 2013-03-26T11:18:09.207

Reputation: 8 610

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. – flodel – 2013-03-30T02:45:16.367

@flodel thanks! I didn't thought of the ellipsis, that's nice. – plannapus – 2013-03-30T08:01:28.890

3

APL, 23 20 characters

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

Example:

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

Howard

Posted 2013-03-26T11:18:09.207

Reputation: 23 109

3

K, 23

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

Example

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

tmartin

Posted 2013-03-26T11:18:09.207

Reputation: 3 917

2

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.

tomsmeding

Posted 2013-03-26T11:18:09.207

Reputation: 2 034

1You 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]. – Peter Taylor – 2013-03-27T10:43:15.830

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 withouta,bandc` though. – tomsmeding – 2013-03-27T16:32:43.437

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. – Peter Taylor – 2013-03-27T16:39:28.540

2

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

DavidC

Posted 2013-03-26T11:18:09.207

Reputation: 24 524

Composition shaves couple of bytes Area@*SSSTriangle – swish – 2019-06-24T07:29:27.837

@swish Thanks, Much appreciated. – DavidC – 2019-06-24T08:23:35.620

1

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.

jdstankosky

Posted 2013-03-26T11:18:09.207

Reputation: 1 474

1The final ($s-$c[2]) can be replaced with $s-=$c[2] for one byte, but that's all I can see. – primo – 2013-04-08T09:24:04.097

@primo Thanks, man! – jdstankosky – 2013-04-11T12:55:44.670

1

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>

Fors

Posted 2013-03-26T11:18:09.207

Reputation: 3 020

1

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

mellamokb

Posted 2013-03-26T11:18:09.207

Reputation: 5 544

1

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.

Johannes Kuhn

Posted 2013-03-26T11:18:09.207

Reputation: 7 122

1

Japt, 17 16 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.

Shaggy

Posted 2013-03-26T11:18:09.207

Reputation: 24 623

I think you can use any of these for the second line, too: NmnU ×*U q, NmnU r*U q, Np0 mnU ×q – ETHproductions – 2017-07-26T14:42:54.970

1

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

Wernisch

Posted 2013-03-26T11:18:09.207

Reputation: 2 534

1

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

Goysa

Posted 2013-03-26T11:18:09.207

Reputation: 91

1

dc, 34 bytes

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

Try it online!

jnfnt

Posted 2013-03-26T11:18:09.207

Reputation: 373

1

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.

ceilingcat

Posted 2013-03-26T11:18:09.207

Reputation: 5 503

1

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

lirtosiast

Posted 2013-03-26T11:18:09.207

Reputation: 20 331

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.

– Martin Ender – 2015-05-21T10:53:08.770

0

#include<stdio.h>
#include<math.h>
main()
{
  double a,b,c,s,area;
  scanf("%d %d %d" &a,&b,&c);
  s=sqrt((a*a)+(b*b)+(c*c));
  area=[sqrt(s*(s-a)*(s-b)*(s-c))]/2;
}

sharath

Posted 2013-03-26T11:18:09.207

Reputation: 1

Hi iam beginner of programming,if any errors plz suggest me. – sharath – 2013-04-01T09:06:48.370

Welcome to CodeGolf. You should first verify that the program works and somehow outputs the result. There are some simple bugs in this one. – ugoren – 2013-04-02T07:49:35.137

0

ARBLE, 36 bytes

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

Heron's formula.

Try it online!

ATaco

Posted 2013-03-26T11:18:09.207

Reputation: 7 898

0

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

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

Try it online!

Xcali

Posted 2013-03-26T11:18:09.207

Reputation: 7 671

0

Stax, 10 bytes

╝0∞♀»♦▓y╩╪

Run and debug it

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

recursive

Posted 2013-03-26T11:18:09.207

Reputation: 8 616

0

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

RosLuP

Posted 2013-03-26T11:18:09.207

Reputation: 3 036