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.
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.
6
(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:)
7
Takes screen input via ←⎕
(×/(+/t÷2)-0,t←⎕)*.5
7
Mathematica 23
√Times@@(+##/2-{0,##})&
6
t=input()
s=a=sum(t)/2.
for x in t:a*=s-x
print a**.5
Input: 2,3,4
Output: 2.90473750966
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
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
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
~].~++: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
3
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
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
{(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4
Example:
> {(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4
2.90474
3
{sqrt s**/(s:.5*+/x)-x}
Example
k){sqrt s**/(s:.5*+/x)-x} 2 3 4
2.904738
2
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
.
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 without
a,
band
c` 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
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
<?=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.
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
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>
1
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! :)
1
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.
As a proc
, it extends to only 81 bytes:
https://tio.run/##NYo7CoAwEAV7T/GKLYyigp@L2EqKZEknGLMRBPHsMaAWwzAwkdeUfNgYMy4DC75xiYsQlGRqsjWx6vq28EcULO70AbKHWJJUmYaMem0/s1L6Tu89o8eAURd/5sCk0wM
1
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))
1
Basically heron's formula:
f(a,b,c)=(p=(a+b+c)/2;sqrt(p*(p-a)*(p-b)*(p-c)))
1
1
#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.
1
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))
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.7700
#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;
}
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
0
0
0
{√×/(+/⍵÷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
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.550228 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