35
\$\begingroup\$

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.

\$\endgroup\$
13
  • \$\begingroup\$ should s be (a+b+c)/3 ? \$\endgroup\$ – costrom Nov 28 '16 at 16:06
  • 3
    \$\begingroup\$ @costrom No, the formula is correct. s is the semiperimeter of the triangle. your formula would be undefined for an equilateral triangle. \$\endgroup\$ – Fatalize Nov 28 '16 at 17:10
  • \$\begingroup\$ Can I just get floats for input or do I need to get integers as well? \$\endgroup\$ – Erik the Outgolfer Nov 29 '16 at 13:35
  • \$\begingroup\$ @ErikGolferエリックゴルファー It is acceptable to input 42.0 instead of 42. \$\endgroup\$ – Fatalize Nov 29 '16 at 13:37
  • \$\begingroup\$ @Fatalize Thanks. Also, can the inputs all be 0? \$\endgroup\$ – Erik the Outgolfer Nov 29 '16 at 13:39

41 Answers 41

1
2
1
\$\begingroup\$

Common Lisp, 70 bytes

(lambda(a b c &aux(s(/(+ a b c)2)))(/(* a b c)8(- s a)(- s b)(- s c)))
\$\endgroup\$
1
\$\begingroup\$

Math++, 51 41 bytes

?>a
?>b
?>c
a*b*c/(b+c-a)/(a-b+c)/(a+b-c)
\$\endgroup\$
0
1
\$\begingroup\$

05AB1E, 6 bytes

O¹/ÍPz

Try it online!

Floating point inaccuracies.

Ported.

Looks an awful lot like this answer. Gawd.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.