त्रिभुज क्षेत्र साइड साइड साइड


17

एक त्रिकोण के तीन पक्षों को देखते हुए, इस त्रिकोण का प्रिंट क्षेत्र।

परीक्षण के मामलों:

में: 2,3,4

आउट: 2.90473750965556

में: 3,4,5

आउट: 6

मान लें कि तीन पक्ष a, b, c हमेशा a> 0, b> 0, c> 0, a + b> c, b + c> a, c + a> b।

जवाबों:


6

जे, 23 19 वर्ण

   (4%~2%:[:*/+/-0,+:)

   (4%~2%:[:*/+/-0,+:) 2 3 4
2.90474

   (4%~2%:[:*/+/-0,+:) 3,4,5
6

17-चार संस्करण यदि इनपुट में है i:4%~%:*/(+/,+/-+:)i

मूल 23-वर्ण संस्करण: (%:@(+/**/@(+/-+:))%4:)


7

एपीएल 21 20

Screen ← के माध्यम से स्क्रीन इनपुट लेता है

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

6

पायथन 2, 53

t=input()
s=a=sum(t)/2.
for x in t:a*=s-x
print a**.5

इनपुट: 2,3,4

आउटपुट: 2.90473750966


3
मैंने बेहतर समाधान के साथ आने में बहुत समय बर्बाद किया। मुझे यकीन है कि यह उतना ही अच्छा है जितना कि यह हो जाता है
जामिलक

2
); तुम और मैं दोनों @jamylak
Primo

6

गणितज्ञ २३

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

बस एक अलग टिप्पणी। हम (आमतौर पर) Mma कोड को यहां दिए गए कार्यों के रूप में प्रदान करने का प्रयास करते हैं। उदाहरण के लिए आपके मामले मेंSqrt[Tr@#*Times@@(Tr@#-2#)]/4&
डॉ। बेलिसरियस

2
28 (Tr@#Times@@(Tr@#-2#))^.5/4&चर, एक फ़ंक्शन के रूप में , या 27 एक चर का उपयोग करते हुए
डॉ। बेलिसरियस

@belisarius आपके सुझाव के लिए धन्यवाद।
च्यानयोग

5

पायथन 57 बाइट्स

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

मैं अजगर से बहुत परिचित नहीं हूं, लेकिन लाइन 2 के *.5बजाय क्यों है /2?
jdstankosky

@jdstankosky पायथन में डिवीजन ऑपरेटर डिफ़ॉल्ट पूर्णांक डिवीजन द्वारा होता है, ताकि यदि योग a+b+cविषम हो, तो परिणाम गलत होगा। यह पायथन 3 में बदल गया, हालांकि अधिकांश गोल्फ सबमिशन को पायथन 2.7 माना जाता है जब तक कि अन्यथा निर्दिष्ट नहीं किया जाता है (जैसा कि पर्ल सबमिशन 5.10+ माना जाता है, और पर्ल 6 नहीं)।
प्रिमो

3
आप केवल "पायथन" के बजाय "पायथन 3" कह सकते हैं।
जो जेड।

1
@JoeZ। नहीं। यह पायथन 2 है; पायथन 3 में, इनपुट () एक स्ट्रिंग लौटाता है, इस समाधान को तोड़ता है।
खुल्द्रेसथ ना'बरिया

4

GolfScript, 38 अक्षर

~].~++:d\{2*d\-*}/'"#{'\+'**0.5/4}"'+~

चूंकि प्रश्न में यह निर्दिष्ट नहीं था, अन्यथा मैंने केवल पूर्णांक लंबाई पर काम करना चुना। स्पेस द्वारा अलग किए गए STDIN पर साइड्स दिए जाने चाहिए।

उदाहरण:

> 2 3 4
2.9047375096555625

3

के, २३

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

उदाहरण

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

3

एपीएल, २३ २० अक्षर

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

उदाहरण:

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

3

आर: 48 43 वर्ण

f=function(...)prod(sum(...)/2-c(0,...))^.5

हेरोन के फार्मूले का उपयोग करना लेकिन आर के वैश्वीकरण का लाभ उठाना।
एलिप्स के विचार के लिए @flodel को धन्यवाद।

उपयोग:

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

@flodel thanks! I didn't thought of the ellipsis, that's nice.
plannapus

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.


1
You 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

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, b` and c though.
tomsmeding

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

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


Composition shaves couple of bytes Area@*SSSTriangle
swish

@swish Thanks, Much appreciated.
DavidC

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>

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.


1
The final ($s-$c[2]) can be replaced with $s-=$c[2] for one byte, but that's all I can see.
primo

@primo Thanks, man!
jdstankosky

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


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

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.


I think you can use any of these for the second line, too: NmnU ×*U q, NmnU r*U q, Np0 mnU ×q
ETHproductions


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

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

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



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;
}

Hi iam beginner of programming,if any errors plz suggest me.
sharath

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




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
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.