Evaluate the aspect ratio of a triangle

35

7

Given three sidelengths of a triangle, evaluate its aspect ratio AR given the following formula:

enter image description here

where

enter image description here

The closer to equilaterality a triangle is, the closer to 1 its aspect ratio is. The aspect ratio is bigger or equal to 1 for valid triangles.

Inputs

The input is three real positive numbers which can be encapsulated in a list or anything similar if need be.

Your program must output the same value no matter what the order in which the three sidelengths are inputted is.

Those three numbers will always be valid sidelengths of a triangle (degenerate triangles like one with sidelengths 1, 1 and 2 will not be given as input). You need not worry about floating point inaccuracies when values become extremely close to a degenerate triangle (e.g. it is acceptable that your program would error division by 0 for input [1, 1, 1.9999999999999999]).

The input can be given through STDIN, as a function argument, or anything similar.

Outputs

The output is a real number bigger or equal to 1 with the standard accuracy that is acceptable in your language.

The output may be printed to STDOUT, returned from a function, or anything similar.

Test cases

Inputs                   Output

  1      1      1         1
  3      4      5         1.25
 42     42   3.14         ≈ 6.9476
 14      6     12         1.575
  6     12     14         1.575
0.5    0.6    0.7         ≈ 1.09375

Scoring

This is , so the shortest answer in bytes wins.

Fatalize

Posted 2016-11-27T11:09:13.253

Reputation: 32 976

should s be (a+b+c)/3 ? – costrom – 2016-11-28T16:06:37.740

3

@costrom No, the formula is correct. s is the semiperimeter of the triangle. your formula would be undefined for an equilateral triangle.

– Fatalize – 2016-11-28T17:10:58.557

Can I just get floats for input or do I need to get integers as well? – Erik the Outgolfer – 2016-11-29T13:35:48.193

@ErikGolferエリックゴルファー It is acceptable to input 42.0 instead of 42. – Fatalize – 2016-11-29T13:37:03.073

@Fatalize Thanks. Also, can the inputs all be 0? – Erik the Outgolfer – 2016-11-29T13:39:33.023

@ErikGolferエリックゴルファー I fixed the description: "The input is three real positive numbers", so no input can be 0 (this would yield a degenerate triangle). Thanks. – Fatalize – 2016-11-29T13:41:55.233

Is there a related Wikipedia or WolframAlpha page for this formula? – mbomb007 – 2016-11-29T15:00:15.767

@mbomb007 This formula is described in this book.

– Fatalize – 2016-11-29T15:13:25.100

@Fatalize Yeah, I saw, but I was expecting to find more than that when searching. – mbomb007 – 2016-11-29T15:19:36.260

Input given through STDOUT? Do you mean STDIN, or do you actually want us to retrieve input through STDOUT? – Zacharý – 2016-11-29T22:19:14.880

@ZacharyT That was a typo, thanks. – Fatalize – 2016-11-30T07:34:41.860

@mbomb007 Look here.

– Adám – 2016-11-30T15:56:47.310

Ehm. An answer in... MINECRAFT!?!?!. Ehm.

– RudolfJelin – 2016-12-02T18:58:08.417

Answers

19

Jelly, 6 bytes

This answer is based on Emigna's 05AB1E answer. Many thanks to Dennis and Lynn for their help in figuring this answer out. Golfing suggestions welcome! Try it online!

S_Ḥ⁸÷P

Ungolfing

           Implicit argument [a, b, c].
S          Take the sum, a+b+c or 2*s
  Ḥ        Take the double, [2*a, 2*b, 2*c].
 _         Vectorized subtract, giving us [2*(s-a), 2*(s-b), 2*(s-c)].
   ⁸÷      Vectorized divide the initial left argument, the input [a, b, c],
             by [2*(s-a), 2*(s-b), 2*(s-c)].
     P     Take the product giving us the aspect ratio, abc/8(s-a)(s-b)(s-c).

Sherlock9

Posted 2016-11-27T11:09:13.253

Reputation: 11 664

46 bytes and you still want golfing suggestions? :-D – Luis Mendo – 2016-11-27T16:58:09.790

1@LuisMendo If it's at all possible, sure :D – Sherlock9 – 2016-11-27T17:01:31.543

1“Push” isn’t quite correct terminology; Jelly isn’t stack-based. Rather, ⁸÷ gets muched off the chain as a unit, and should be read as divide the initial left argument by this, or something. – Lynn – 2016-11-27T17:28:29.363

1@Lynn I'm been golfing Actually for several months now. Stack-based terminology is firmly wedged in my brain :D Should be fixed now. – Sherlock9 – 2016-11-27T17:32:14.117

56

Jelly, 7 bytes

SH_÷@HP

Try it online!

Explanation

enter image description here

Let’s read this chain:

  • The implicit argument is a list [a, b, c].

  • First we read S. This takes the sum: a + b + c.

  • Then, we read H. This halves it: (a + b + c)/2. (This is s.)

  • Then, we read a dyad _ (subtract), followed by another dyad. This is a hook: it lacks a right argument, so it receives the argument to this chain, [a, b, c], giving us [s-a, s-b, s-c]. (This is the fifth chain pattern in the table here.)

  • Then, we read the dyad-monad pair ÷@H. This is a fork: ÷@ is division with the arguments flipped, and H is halve, so our working value gets Half the argument to this chain ÷’d by it. This vectorizes; we’re left with [(a/2)/(s-a), (b/2)/(s-b), (c/2)/(s-c)]. (This is the second chain pattern in the table here.)

  • Finally, we take the product with P, getting us abc/(8(s-a)(s-b)(s-c)).

View a tree-like graph of how the links fit together.

Lynn

Posted 2016-11-27T11:09:13.253

Reputation: 55 648

8The images look great! Nice touch! – DavidC – 2016-11-27T13:10:14.117

2I made the top image smaller myself, and turned the second one into a link (no pun intended). – Lynn – 2016-11-27T14:06:14.153

I saw the image and immediately thought, "Nice one, Lynn!" before looking at who posted it ;-) – ETHproductions – 2016-11-28T00:24:13.420

7Best explanation I've seen of a Jelly program. I still don't understand, but closer! – Sparr – 2016-11-30T07:45:11.287

I ran the sample against the test-case ´6.0,12.0,14.0and it gave-0.8888888888888888´ instead of 1.575 as shown in the test-cases. Is it a problem with the test-cases or your code? – MBaas – 2016-12-01T04:18:42.140

@MBaas It returns 1.575 for me. Could you double-check? – Martin Ender – 2016-12-01T07:54:29.110

13

Jelly, 6 bytes

S÷_2Pİ

Try it online!

How it works

S÷_2Pİ  Main link. Argument: [a, b, c]

S       Sum; compute 2s := a + b + c.
 ÷      Divide; yield [2s ÷ a, 2s ÷ b, 2s ÷ c].
  _2    Subtract 2; yield [2s ÷ a - 2, 2s ÷ b - 2, 2s ÷ c - 2].
    P   Product; yield (2s ÷ a - 2)(2s ÷ b - 2)(2s ÷ c - 2).
     İ  Invert; yield 1 ÷ (2s ÷ a - 2)(2s ÷ b - 2)(2s ÷ c - 2).

Dennis

Posted 2016-11-27T11:09:13.253

Reputation: 196 637

Ahh, and I tried to use ³⁴⁵ as arguments... – Erik the Outgolfer – 2016-11-29T13:10:14.793

11

JavaScript, 38 bytes

This is a (curried) lambda:

a=>b=>c=>a*b*c/(b+c-a)/(a+c-b)/(a+b-c)

(If you assign it to a variable f you'd need to call it like f(3)(4)(5))

flawr

Posted 2016-11-27T11:09:13.253

Reputation: 40 560

Beat me to it by a few seconds :) Mind explaining how the formula works similarly to the one provided by the question? – user41805 – 2016-11-27T11:21:57.370

@KritixiLithos Just plug in s = 1/2(a+b+c) into the formula and simplify :D (e.g. s-a = .5*b+.5*c-.5*a, and the three factors of .5 cancel with 8) – flawr – 2016-11-27T11:22:28.147

5(a,b,c)=> is the same length, and costs less bytes to call ;) – ETHproductions – 2016-11-28T00:25:55.673

4But I love curry :D – flawr – 2016-11-28T10:21:14.550

9

05AB1E, 11 7 bytes

05AB1E uses CP-1252 encoding.

O¹·-¹/P

Try it online!

Explanation

O         # sum input
 ¹        # push input again
  ·       # multiply by 2
   -      # subtract from sum
    ¹/    # divide by input
      P   # product

Emigna

Posted 2016-11-27T11:09:13.253

Reputation: 50 798

8

MATL, 8 7 bytes

tsGE-/p

Try it online!

Explanation

Let's use input [3 4 5] as an example

t    % Take input implicitly. Duplicate
     % STACK: [3 4 5], [3 4 5]
s    % Sum of array
     % STACK: [3 4 5], 12
G    % Push input again
     % STACK: [3 4 5], 12, [3 4 5]
E    % Multiply by 2, element-wise
     % STACK: [3 4 5], 12, [6 8 10]
-    % Subtract, element-wise
     % STACK: [3 4 5], [6 4 2]
/    % Divide, element-wise
     % STACK: [0.5 1 2.5]
p    % Product of array. Implicitly display
     % STACK: 1.25

Luis Mendo

Posted 2016-11-27T11:09:13.253

Reputation: 87 464

8

R, 34 29 bytes

x=scan();prod(x/(sum(x)-2*x))

Reads input from stdin and store as the R-vector x. Then make use of R's vectorization to form the denominator.

Billywob

Posted 2016-11-27T11:09:13.253

Reputation: 3 363

7

Haskell, 36 bytes

This defines the function # which takes three arguments.

(a#b)c=a*b*c/(b+c-a)/(a+c-b)/(a+b-c)

You have to call it as follows: (3#4)5

A little bit longer but perhaps more golfable:

p=product
f v=p v/p((sum v-).(2*)<$>v)

flawr

Posted 2016-11-27T11:09:13.253

Reputation: 40 560

6

MATLAB, 64 38 25 bytes

This is an anyonmous function that implements the formula as provided:

@(v)prod(v./(sum(v)-2*v))

It assumes the input to be a list of three values e.g. [3,4,5]. This example is used in following explanation:

             sum(v)        = 3+4+5 = 12
             sum(v)-2*v    = 12 - 2*[3,4,5] = 12 - [6,8,10] = [6,4,2]
         v./(sum(v)-2*v))  = [3,4,5] ./ [6,4,2] = [0.5,1,2.5]
    prod(v./(sum(v)-2*v))  = 0.5 * 1 * 2.5 = 1.25

flawr

Posted 2016-11-27T11:09:13.253

Reputation: 40 560

6

Mathematica, 20 bytes

1##&@@(#/(Tr@#-2#))&

Takes input as a list of three values, which is referred to as # inside the function. Tr@ is the shortest way to sum a list (to get 2s) and 1##&@@(...) multiplies the three factors i/(2s-2i) for i in a, b, c.

If the inputs are integers or rational numbers, you'll get an exact result.

Martin Ender

Posted 2016-11-27T11:09:13.253

Reputation: 184 808

6

Python 3, 42 bytes

lambda a,b,c:a*b*c/(b+c-a)/(a+c-b)/(a+b-c)

Try It Online!

Dennis

Posted 2016-11-27T11:09:13.253

Reputation: 196 637

5

OCaml, 51 bytes

fun a b c->a*.b*.c/.(b+.c-.a)/.(a+.c-.b)/.(a+.b-.c)

Yay, separate operators for floats...

shooqie

Posted 2016-11-27T11:09:13.253

Reputation: 5 032

5

Actually, 10 8 bytes

This answer is based on Dennis's excellent Jelly answer. Golfing suggestions welcome! Try it online!

;Σ♀/♂¬πì

Ungolfing

     Implicit input [a, b, c].
;    Duplicate [a, b, c].
Σ    sum() to get twice the semiperimeter, 2*s.
♀/   Vectorized divide 2*s by [a, b, c] to get [2*s/a, 2*s/b, 2*s/c].
♂¬   Vectorized subtract 2 to get [2*s/a-2, 2*s/b-2, 2*s/c-2].
π    Get the product of the above to get 8*(s/a-1)*(s/b-1)*(s/c-1).
     This is the same as 8(s-a)(s-b)(s-c)/abc.
ì    Invert to get the aspect ratio, abc/8(s-a)(s-b)(s-c).
     Implicit return.

Sherlock9

Posted 2016-11-27T11:09:13.253

Reputation: 11 664

5

Wonder, 48 bytes

@@@prod[#0#1#2/1- +#1#0#2/1- +#2#0#1/1- +#2#1#0]

RIP

Usage:

(((@@@prod[#0#1#2/1* * - +#1#0#2- +#2#0#1- +#2#1#0])3)4)5

Explanation

Function calls are costly in Wonder when compared to infix operators in other languages. Because of this, I contained all the terms in an array and got the product of the result instead of multiplying every single term. The code would be equivalent to something like:

(a,b,c)=>product([a,b,c,1/(b+c-a),1/(a+c-b),1/(a+b-c)])

Mama Fun Roll

Posted 2016-11-27T11:09:13.253

Reputation: 7 234

1Hmm, why "RIP"? – Luis Mendo – 2016-11-27T17:04:27.550

It's much longer than necessary/expected – Mama Fun Roll – 2016-11-27T20:16:42.963

5

Minecraft 1.8, 1607 bytes + 85 blocks = 1692 blytes

Warning: Not golfed. Golfed will take up to 1/3 less blytes.

Here is a commented screenshot:

enter image description here

  • The inputs are a, b, and c,and the output is fin

  • fin, and all other variables in Minecraft are integers, so the standard Minecraft accuracy is 0 decimal points

  • The green border: the command blocks on the left will activate after the ones on the right, which are just variable initializations.

  • The lever (grey-brown rectangle in the down right) is the contraption trigger

  • It takes up so much because of the way Minecraft handles variables. A very simplified overview:

    • /scoreboard objectives add name dummy creates a new variable named "name"

    • /scoreboard players set @p name number sets the variable name to number. Number must be a real number, not a variable.

    • /scoreboard players operation @p name += @p name2 increments name by name2. name2 must be a variable, not a number.

      • -=, /=, *=, = and more can be used instead += to decrement, multiply, divide, etc.
  • I'm not going to post all the 43 commands here. It would help golfing this, but would also help drive me crazy copypasting

  • If 1.9 command blocks would be used, the solution would (at least) use 42 blocks less. If one-letter variables would be used, almost 200 bytes would be saved.

RudolfJelin

Posted 2016-11-27T11:09:13.253

Reputation: 853

4

Java, 38 bytes

(a,b,c)->a*b*c/(b+c-a)/(a-b+c)/(a+b-c)

Testing and ungolfed

public class Pcg101234 {
  interface F {
    double f(double a, double b, double c);
  }
  public static void main(String[] args) {
    F f = (a,b,c)->a*b*c/(b+c-a)/(a-b+c)/(a+b-c);

    System.out.println(f.f(1,1,1));
    System.out.println(f.f(3,4,5));
    System.out.println(f.f(42,42,3.14));
    System.out.println(f.f(14,6,12));
    System.out.println(f.f(6,12,14));
    System.out.println(f.f(0.5,0.6,0.7));
  }
}

Test it!

Output

1.0
1.25
6.947606226693615
1.575
1.575
1.09375

Olivier Grégoire

Posted 2016-11-27T11:09:13.253

Reputation: 10 647

I feel like (a,b,c) is kinda cheating here, because it contains no type information. IMO the implicit lambda interface (in your case F) should count in the total byte sum. – F. George – 2016-11-27T18:51:49.913

4

@mEQ5aNLrK3lqs3kfSa5HbvsTWe0nIu Lambdas are heavily recommended. Also, most of the Java 8 entries work like that without any remarks. If you disagree and consider I cheated, I invite you to ask formally in meta if this notation is accepted or not. Meanwhile, I align on previous Java 8 answers.

– Olivier Grégoire – 2016-11-27T19:01:34.423

4

Jellyfish, 17 16 bytes

Thanks to Zgarb for saving 1 byte.

p%/*-)/+i
    2%

Try it online!

Explanation

This is based on the same reciprocal formula as Dennis's answer.

In more traditional functional notation, the above program reads as follows:

print(
  1 / fold(
    multiply,
    fold(add, i) / i - 2
  )
)

Where i is the input list. Note that fold(multiply, ...) just computes the product and fold(add, ...) the sum, so we can further simplify this to:

print(1 / product(sum(i) / i - 2))

The sum(i) / i is implemented via the hook )/+ which defines a new unary function to do both steps at once.

Martin Ender

Posted 2016-11-27T11:09:13.253

Reputation: 184 808

Save a byte with a hook.

– Zgarb – 2016-11-29T14:02:51.533

4

Dyalog APL, 10 9 bytes

×/⊢÷+/-+⍨

This is an anonymous function train (an atop of a fork of a fork of a fork), meaning that every sub-function is applied to the argument, inside the following structure:

 ┌─┴─┐          
×/ ┌─┼───┐      
   ⊢ ÷ ┌─┼──┐  
      +/ - +⍨

TryAPL online!

×/ the product of

the arguments

÷ divided by

+/ the sum of the arguments

- minus

+⍨ the arguments doubled (lit. added to themselves)

Mathematical background.

ngn shaved a byte.

Adám

Posted 2016-11-27T11:09:13.253

Reputation: 37 779

Hi Adám, pls. remind me to ask you about ´⊢´ next week if I forget it :-) – MBaas – 2016-12-01T04:11:22.227

I dunno how the "background" link supposedly pertains to this answer because I see no algorithm there at all. Also, can you add some info about order of operations? I've tried to reproduce this answer in a few different languages with varying order of operations but I always get an answer different from that in the question. – cat – 2016-12-03T13:02:01.493

@cat Well, it wasn't intended to give the algorithm, only to explain what the aspect ratio is, as there is no such page on Wikipedia. APL is right-to-left, meaning that every function takes whatever is on its right as argument. Therefore, it can be read from left to right as in the explanation. – Adám – 2016-12-04T09:34:41.087

3

2sable, 6 bytes

A port of Dennis' Jelly answer.

Os/ÍPz

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2016-11-27T11:09:13.253

Reputation: 41 965

3

dc, 49 bytes

5k?dsa?dsb?dsc++2/sslalblc**lsla-lslb-lslc-8***/p

A direct implementation of the formula given. Prompts for the three inputs upon invocation on three separate lines and outputs a floating-point value with 5 digits after the decimal point to the next line.

Explanation

5k                                                # Set the output precision to 5 digits after the decimal
  ?dsa                                            # Prompt for first input value on first line, duplicate it, and then store it in register `a`
      ?dsb                                        # Prompt for second input, duplicate it, and store it in register `b`
          ?dsc                                    # Prompt for third input, duplicate it, and store it in register `c`
              ++2/ss                              # Sum up the 3 values on the main stack, then divide sum by 2 and store the result in register `s`
                    lalblc**                      # Copy all three values from registers `a`,`b`,`c` onto the main stack, find their product, and push result to top of main stack
                            lsla-                 # Copy value from register `s` onto main stack, subtract register `a`'s value from it, and push result to main stack
                                 lslb-            # Copy value from register `s` onto main stack, subtract register `b`'s value from it, and push result to main stack
                                      lslc-       # Copy value from register `s` onto main stack, subtract register `c`'s value from it, and push result to main stack
                                           8***   # Find the product of the top three values and 8 and then push the result to main stack
                                               /p # Divide the second to top value (a*b*c) by the top of stack value (8*(s-a)*(s-b)*(s-c)), push the result to the main stack, and then output the result to STDOUT

R. Kap

Posted 2016-11-27T11:09:13.253

Reputation: 4 730

3

TI-Basic, 11 bytes

Input should be in the form of a list, like {A B C}.

prod(Ans)/prod(sum(Ans)-2Ans

Maybe this visual will help (remember that 2s = a+b+c):

      abc                    abc                   abc                prod(Ans)
---------------- = --------------------- = ------------------- = -------------------
8(s-a)(s-b)(s-c)   (2s-2a)(2s-2b)(2s-2c)   (a+b+c)(1-2{a,b,c})   prod(sum(Ans)-2Ans)

Timtech

Posted 2016-11-27T11:09:13.253

Reputation: 12 038

2

Perl 6, 44 bytes

->\a,\b,\c{a*b*c/(b+c -a)/(a+c -b)/(a+b -c)}

Brad Gilbert b2gills

Posted 2016-11-27T11:09:13.253

Reputation: 12 713

2

Python, 55 bytes

def f(x,y,z):s=x+y+z;return 1/((s/x-2)*(s/y-2)*(s/z-2))

Credit to Dennis. I just ported. In Python, a much-neglected language.

Erik the Outgolfer

Posted 2016-11-27T11:09:13.253

Reputation: 38 134

2

Forth, 83 bytes

Assumes the floating point parameters start on the floating point stack. Leaves the result on the floating point stack. Using the stack for params/returning is the standard for Forth.

: p 3 fpick ;
: T p p p ;
: f 0 s>f T f- f+ f- T f+ f- f* T f- f- f* 1/f f* f* f* ;

Try it online - contains all test cases

Uses the formula a*b*c * 1/ ( -(a+b-c) * -(b+c-a) * (a+c-b) ). Pretty much the entire program is using only the floating point stack. The exception is the 3 in 3 fpick. This program requires an interpreter that supports fpick (Ideone works, repl.it doesn't).

Explanation: slightly less golfed

\ 3 fpick takes the 3rd element (0-indexed) and pushes a copy
\ Used to copy parameters on the stack over another number 'x' ( a b c x -> a b c x a b c )
: f3p 3 fpick 3 fpick 3 fpick ;

: f                     \ define a function f
0 s>f f3p f- f+ f-      \ push a zero, copy params, compute 0-(a+b-c)
                        \ the zero allows me to copy (I need an 'x' to jump over)
f3p f+ f- f*            \ copy params and compute -(b+c-a), multiply by previous result
                        \ the negatives miraculously cancel
f3p f- f- f*            \ copy and compute (a+c-b), multiply by previous result
1/f f* f* f* ;          \ take the reciprocal and multiply by a*b*c
                        \ the result is left on the floating point stack

mbomb007

Posted 2016-11-27T11:09:13.253

Reputation: 21 944

2

ised: 19 bytes

@*$1/@*{@+$1-2.*$1}

Call it as ised --l 'inputfile.txt' '@*$1/@*{@+$1-2.*$1}' where inputfile.txt can be a file with space separated array, or - to receive from pipe/stdin.

Unicode version (same bytecount but 3 chars less):

Π$1/Π{Σ$1-2.*$1}

Unfortunately, ised wastes a lot of chars for its input argument syntax.

orion

Posted 2016-11-27T11:09:13.253

Reputation: 3 095

2

vba, 76

Function r(a,b,c)
s=(a+b+c)/2:r=(a*b*c)/(8*(s-a)*(s-b)*(s-c))
End Function

Call with

?r(3,4,5)

or in excel with

=r(5,12,13)

SeanC

Posted 2016-11-27T11:09:13.253

Reputation: 1 117

You'd save 6 bytes with @SuperJedi224 's algorithm: Public Function r(a,b,c):r=a*b*c/(b+c-a)/(a-b+c)/(a+b-c):End Function – steenbergh – 2016-11-30T15:26:44.197

2

C#, 82 bytes

void ar(double a,double b,double c)=>Console.Write(a*b*c/(b+c-a)/(a+c-b)/(a+b-c));

Usage:

ar(42, 42, 3.14);

WeskerTyrant

Posted 2016-11-27T11:09:13.253

Reputation: 121

2

Pyke, 12 bytes

1QFQsR/tt)B/

Try it here!

Well, BlueEyedBeast, you had your chance. I used a good algorithm here.

Erik the Outgolfer

Posted 2016-11-27T11:09:13.253

Reputation: 38 134

2

k, 19 bytes

{(*/x)%8*/-x-+/x%2}

Evaluates right to left - Divide the list x by 2, sum the result and subtract it from original x. Neg the answer and get the product of the result and 8. The result is the denominator, the numerator is the product of the list.

Paul Kerrigan

Posted 2016-11-27T11:09:13.253

Reputation: 189

1

Lua, 45 bytes

a,b,c=...print(a*b*c/(b+c-a)/(a+c-b)/(a+b-c))

Heavily based on the JavaScript answer.

IDid

Posted 2016-11-27T11:09:13.253

Reputation: 101

1

Common Lisp, 70 bytes

(lambda(a b c &aux(s(/(+ a b c)2)))(/(* a b c)8(- s a)(- s b)(- s c)))

coredump

Posted 2016-11-27T11:09:13.253

Reputation: 6 292

1

Math++, 51 41 bytes

?>a
?>b
?>c
a*b*c/(b+c-a)/(a-b+c)/(a+b-c)

SuperJedi224

Posted 2016-11-27T11:09:13.253

Reputation: 11 342

1

05AB1E, 6 bytes

O¹/ÍPz

Try it online!

Floating point inaccuracies.

Ported.

Looks an awful lot like this answer. Gawd.

Erik the Outgolfer

Posted 2016-11-27T11:09:13.253

Reputation: 38 134

1

CJam, 21 15 bytes

l~_:+\f/2f-:*W#

[.5 .6 .7]

Dennis's post that I ported.

Thanks to 8478 (Martin Ender) for saving me 6 bytes.

Erik the Outgolfer

Posted 2016-11-27T11:09:13.253

Reputation: 38 134

1

Minkolang v0.15, 34 bytes

$n3$D++2$:3[d5i-g-$r]x**8*r**r$:N.

Try it online!

Explanation

$n                                     takes all numbers from input
                                       STACK: [a,b,c]
  3$D                                  duplicates stack 3 times
                                       STACK: [a,b,c,a,b,c,a,b,c]
     ++                                adds top two numbers in stack
                                       STACK: [a,b,c,a,b,c,a+b+c]
       2$:                             divides it by 2 (float division)
                                       STACK: [a,b,c,a,b,c,(a+b+c)/2]
                                        or can be restated as
                                       STACK: [a,b,c,a,b,c,s]
          3[        ]                  starts a for-loop (for 3 iterations)
            d                          duplicates top of stack
                                       STACK: [a,b,c,a,b,c,s,s]
             5i-g                      pushes 5 minus the index (0-indexed) and gets the
                                       value in the stack at that index
                                       STACK: [a,b,c,a,b,s,s,c]
                 -                     subtracts them
                                       STACK: [a,b,c,a,b,s,s-c]
                  $r                   swaps the top 2 stack values
                                       STACK: [a,b,c,a,b,s-c,s]
                                       Does this two more times
                                       STACK: [a,b,c,s-c,s-b,s-a,s]
                    x                  removes top of stack
                                       STACK: [a,b,c,s-c,s-b,s-a]
                     **8*              multiplies the top 3 values with 8 and each other
                                       STACK: [a,b,c,8(s-c)(s-b)(s-a)]
                         r             reverses stack
                                       STACK: [8(s-c)(s-b)(s-a),a,b,c]
                          **           multiplies top 3 values with each other
                                       STACK: [8(s-c)(s-b)(s-a),abc]
                            r$:        reverse stack and divide the values
                                       STACK: [abc/8(s-c)(s-b)(s-a)]
                               N.      outputs value as number and exit program

user41805

Posted 2016-11-27T11:09:13.253

Reputation: 16 320

1

PHP, 109 97 bytes

<?$n=explode(',',$argv[1]);echo($a=$n[0])*($b=$n[1])*($c=$n[2])/($a+$b-$c)/($a+$c-$b)/($b+$c-$a);

Output:

php triangle-aspect.php "1,1,1"
1
php triangle-aspect.php "3,4,5"
1.25
php triangle-aspect.php "42,42,3.14"
6.9476062266936
php triangle-aspect.php "14,6,12"
1.575
php triangle-aspect.php "6,12,14"
1.575
php triangle-aspect.php ".5,.6,.7"
1.09375

Mario

Posted 2016-11-27T11:09:13.253

Reputation: 3 043

1

QBIC, 35 bytes

:::?(a*b*c)/(a+b-c)/(a+c-b)/(b+c-a)

Wrapped the shortest algorithm in a QBIC boilerplate. ::: gets three command line parameters and makes the ints a, b, c out of them.

steenbergh

Posted 2016-11-27T11:09:13.253

Reputation: 7 772

1

Lithp, 71 bytes

#A,B,C::((/ (* A B C) (+ B C (- 0 A)) (+ A C (- 0 B)) (+ A B (- 0 C))))

At least it's not the longest solution. But it's not all that short either.

In Lithp, arithmatic functions such as / (divide), * (multiply), + (plus) and - (subtract) take multiple arguments, and continually applies the operation to each successive argument. Therefore we only need one divide call total, one multiply and several discrete add and subtract operations. This saves quite a few bytes.

Automatic arithmatic like -A does not work, so instead we subtract A from 0 to make it negative and save on more complex operations.

Usage and ungolfed:

(
    (import "lists")
    (def f #A,B,C::(
        (/ (* A B C)
           (+ B C (- 0 A))
           (+ A C (- 0 B))
           (+ A B (- 0 C))
        )
    ))
    (print (f 1   1   1))    % Output: 1
    (print (f 3   4   5))    % Output: 1.25
    (print (f 42  42  3.14)) % Output: 6.9476062266936145
    (print (f 14  6   12))   % Output: 1.575
    (print (f 6   12  14))   % Output: 1.575
    (print (f 0.5 0.6 0.7))  % Output: 1.09375
)

Andrakis

Posted 2016-11-27T11:09:13.253

Reputation: 361

1

J, 10 bytes

*/ .%+/-+:

Try it online!

Explanation

*/ .%+/-+:  Input: array [a b c]
        +:  Double, gets [2a, 2b, 2c]
     +/     Reduce by addition, gets the sum a+b+c
       -    Subtract, gets [-a+b+c, a-b+c, a+b-c]
   .        Inner product between [a b c] and [-a+b+c, a-b+c, a+b-c]
    %         Divide elementwise
*/            Reduce by multiplication

miles

Posted 2016-11-27T11:09:13.253

Reputation: 15 654

1

Clojure, 53 bytes

#(apply /(apply * %)8(for[i %](-(*(apply + %)0.5)i)))

Takes numbers as a list or a vector and applies them to various basic mathematical operations. Many bytes from repeated apply.

NikoNyrh

Posted 2016-11-27T11:09:13.253

Reputation: 2 361

0

Racket 57 bytes

(define s(/(+ a b c)2))(/(* a b c)8(- s a)(- s b)(- s c))

Ungolfed:

(define (f a b c)
  (define s (/ (+ a b c) 2))
  (/ (* a b c)
     8
     (- s a)
     (- s b)
     (- s c)))

Testing:

(f 1 1 1)
(f 3 4 5)
(f 42 42 3.14)
(f 14 6 12)
(f 6 12 14)
(f 0.5 0.6 0.7)

Output:

1
1 1/4
6.947606226693614
1 23/40
1 23/40
1.0937499999999993

rnso

Posted 2016-11-27T11:09:13.253

Reputation: 1 635