जवाबों:
t=input()
s=a=sum(t)/2.
for x in t:a*=s-x
print a**.5
इनपुट: 2,3,4
आउटपुट: 2.90473750966
गणितज्ञ २३
√Times@@(+##/2-{0,##})&
Sqrt[Tr@#*Times@@(Tr@#-2#)]/4&
(Tr@#Times@@(Tr@#-2#))^.5/4&चर, एक फ़ंक्शन के रूप में , या 27 एक चर का उपयोग करते हुए
a,b,c=input()
s=(a+b+c)*.5
print(s*(s-a)*(s-b)*(s-c))**.5
बगुला के सूत्र का उपयोग करना ।
नमूना उपयोग:
$ echo 2,3,4 | python triangle-area.py
2.90473750966
$ echo 3,4,5 | python triangle-area.py
6.0
एक 58 बाइट संस्करण:
a,b,c=input()
print((a+b+c)*(b+c-a)*(a+c-b)*(a+b-c))**.5/4
*.5बजाय क्यों है /2?
a+b+cविषम हो, तो परिणाम गलत होगा। यह पायथन 3 में बदल गया, हालांकि अधिकांश गोल्फ सबमिशन को पायथन 2.7 माना जाता है जब तक कि अन्यथा निर्दिष्ट नहीं किया जाता है (जैसा कि पर्ल सबमिशन 5.10+ माना जाता है, और पर्ल 6 नहीं)।
f=function(...)prod(sum(...)/2-c(0,...))^.5
हेरोन के फार्मूले का उपयोग करना लेकिन आर के वैश्वीकरण का लाभ उठाना।
एलिप्स के विचार के लिए @flodel को धन्यवाद।
उपयोग:
f(2,3,4)
[1] 2.904738
f(3,4,5)
[1] 6
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.
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.
(a=v[0])a is longer than v[0]v[0].
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, b` and c though.
s=(-v[0]-v[1]-v[2])/2 and change the other - to +. It's an even number of terms, so it cancels out.
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
Area@*SSSTriangle
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>
<?=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.
($s-$c[2]) can be replaced with $s-=$c[2] for one byte, but that's all I can see.
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! :)
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.
proc, it extends to only 81 bytes: tio.run/##NYo7CoAwEAV7T/…
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))
#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.
#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;
}