# Perimeter of Conway hexagon

## Background

Given a triangle $$\ABC\$$, extend its three sides by the opposite side length, as shown in the figure below. Then the six points surprisingly lie on a circle called the Conway circle, whose center coincides with the incenter (the center of incircle, the circle that is tangent to the three sides from the inside). ## Task

Given three side lengths $$\a,b,c\$$ of the triangle $$\ABC\$$, calculate the perimeter of the hexagon $$\A_b B_a B_c C_b C_a A_c\$$ (formed by the six points on the Conway circle).

The answer must be within 1e-6 relative error from the expected. You can assume the side lengths form a valid non-degenerate triangle.

The shortest code in bytes wins.

## Test cases

a   b   c         ans
---------------------
1   1   1    9.000000
2   2   3   20.399495
3   4   5   35.293155
6   7   12  65.799785
2.3 4.5 6.7 31.449770

• @FryAmTheEggman OK, though it isn't that relevant to the task (you don't need to compute the inradius or the coordinates of the incenter). – Bubbler Apr 24 '20 at 0:57
• That's fair, but someone might! – FryAmTheEggman Apr 24 '20 at 1:09
• Thanks for all the John Conway challenges -- they're a nice homage to him. – Mitchell Spector Apr 24 '20 at 3:57

# Python 3, 89 87 69 68 bytes

Thanks @xnor for finding a shorter expression, saving 1 byte!

f=lambda a,b,c,n=3:n and(a+b+c)*(c*c/a/b-a/b-b/a+2)**.5+f(b,c,a,n-1)


Try it online!

A recursive function, takes in 3 sides of the triangle as input.

Recursion is used to repeats the function 3 times, each time with the positions of a,b,c swapped in order to calculate each of the 3 summands in the formula below.

### How

We can see that each side of the hexagon is the base of an isosceles triangle, whose vertex angle is an angle of the original triangle. For example:

• $$\C_aC_b\$$ is the base of $$\CC_aC_b\$$ - an isosceles triangle with leg $$\c\$$ and vertex angle $$\\widehat{C}\$$.
• $$\A_bB_a\$$ is the base of $$\CA_bB_a\$$ - an isosceles triangle with leg $$\a+b\$$ and vertex angle $$\\widehat{C}\$$.

Given the leg $$\l\$$ and vertex angle $$\\theta\$$ of an isosleces triangle, the base is calculated as: $$l\sqrt{2-2\cos{\theta}}$$ Consider 2 opposites side of the hexagon, says $$\C_aC_b\$$ and $$\A_bB_a\$$. Since their corresponding triangles have the same vertex angle, their total length is: $$c\sqrt{2-2\cos{\widehat{C}}}+(a+b)\sqrt{2-2\cos{\widehat{C}}}$$$$=(a+b+c)\sqrt{2-2\cos{\widehat{C}}}$$ Then the perimeter of the hexagon is the sum of 3 opposite pairs: $$(a+b+c)\left(\sqrt{2-2\cos{\widehat{A}}}+\sqrt{2-2\cos{\widehat{B}}}+\sqrt{2-2\cos{\widehat{C}}}\right)$$ The cosine of an angle can be calculated from the sides of the triangle: $$2-2\cos{\widehat{C}}=\frac{c^2-(a-b)^2}{ab}$$ Thus, the final formula for the hexagon's perimeter is: $$(a+b+c)\left(\sqrt{\frac{a^2-(b-c)^2}{bc}}+\sqrt{\frac{b^2-(a-c)^2}{ac}}+\sqrt{\frac{c^2-(a-b)^2}{ab}}\right)$$

• Great solution, and shorter than mine! I'll post a bounty when this challenge is old enough to allow it. – xnor Apr 24 '20 at 4:40
• It looks like expanding out (c*c-(a-b)**2)/a/b -> c*c/a/b-a/b-b/a+2 works to save a byte. – xnor Apr 24 '20 at 4:43
• @xnor Nice golf, thanks! Also I appreciate the bounty! – Surculose Sputum Apr 24 '20 at 5:02

# Python 3, 64 bytes

lambda*t:eval("+((-(%s-%s)**2+%s**2)/%s/%s)**.5"*3%(t*5))*sum(t)


Try it online!

Uses an adaptation of a formula by Surculose Sputum, written so that the variables a,b,c repeat in a cycle when the formula is read left to right.

+((-(a-b)**2+c**2)/a/b)**.5+((-(c-a)**2+b**2)/c/a)**.5+((-(b-c)**2+a**2)/b/c)**.5


This lets us insert the input values into the formula as literals by string interpolation on the tuple (a,b,c) repeated 5 times, and then call eval to evaluate the resulting expression.

# Python 3, 76 bytes

lambda a,b,c:sum((2-(a*a+b*b+c*c-2*x*x)*x/a/b/c)**.5*(a+b+c)for x in[a,b,c])


Try it online!

I took Surculose Sputum's formula and solution and wrote the three summands in a closer-to-symmetric form like:

$$(a+b+c)\sqrt{2-\frac{a^2+b^2-c^2}{ab}}$$

The idea is that we want the summand to be as symmetric in $$\a,b,c\$$ as possible so that we can iterate a single variable $$\x\$$ over $$\a,b,c\$$ to produce each summand.

To this end, we write the core term $$\frac{a^2+b^2-c^2}{ab}$$ as the somewhat clunky

$$\frac{a^2+b^2+c^2-2c^2}{abc}\cdot c$$

so that we can write it in this symmetric form:

$$\frac{a^2+b^2+c^2-2x^2}{abc}\cdot x$$

Substituting $$\x=a\$$, $$\x=b\$$, and $$\x=c\$$ gives the three respective summands.

Perhaps there's a better way to put this fraction into a near-symmetric form than doing so for the numerator and denominator individually. We can split it up as $$a/b+b/a-c^2/(ab)$$ but I don't see where to go from there.

# dc, 68 bytes

9k?scsbsa[lad*lblc-d*-lblc*/v]dsFxlalbsasblFxlalcsasclFx++lalblc++*p


Try it online!

Or check out all the test cases.

This is a direct implementation of Surculose Sputum's answer (the original iterative one), which is the type of formula that dc ("desk calculator") can be pretty good for!

Explanation:

9k     Set precision to 9 decimal places.
?      Read input line (push a, b, and c on the stack).
scsbsa Save the input numbers in registers a, b, and c.
[      Start a macro.
This macro takes the values in registers a, b, and c,
and computes the first square root in the formula,
leaving that result on the stack, as follows:
lad*   Push a^2.
lblc-  Push b-c.
d*     Replace b-c at the top of the stack with (b-c)^2.
-      Replace the top 2 items on the stack with a^2-(b-c)^2.
lblc*  Push b*c.
/      Divide to compute the formula under the radical sign.
v      Compute the square root.
]      End of macro.
dsFx       Save macro for later use under the name F, and also run it now.
lalbsasb   Swap registers a and b.
lFx        Call macro F to compute the second square root.
lalcsasc   Swap registers a and c.
lFx        Call macro F to compute the third square root.
++         Add the three square roots.
lalblc++   Compute a+b+c.
*          Multiply a+b+c by the sum of the square roots.
p          Print the result.


# Desmos, 1455 bytes Try it online! Link is Desmos. Interactive! Click and drag triangle points.

Obviously this answer is not going for shortest, I just wanted to show off an interactive demo. The bytes score was calculated by taking the length of concatenating all of the LaTeX from each formula in the calculator used to compute the perimeter (formulas used for interactive components were not counted).

# 05AB1E, 15 bytes

Port of Surculose Seputum's Python solution

œεnÆy¦P/}ÌtO*O;


Try it online!

# Io, 104 bytes

Port of Surculose Sputum's answer.

f :=method(a,b,c,n,if(n!=0,(a+b+c)*(c*c/a/b-a/b-b/a+2)**.5+f(b,c,a,n-1),n))
g :=method(a,b,c,f(a,b,c,3))


Try it online!

# Wolfram Mathematica, 99 bytes

(#1+#2+#3)(Sqrt[(#1^2-(#2-#3)^2)/#2/#3]+Sqrt[(#2^2-(#1-#3)^2)/#1/#3]+Sqrt[(#3^2-(#1-#2)^2)/#1/#2])&


Try it online!

I am pretty sure that this code can be improved. But I wasn't able to wrap my head around it. So I just post this in the hope of learning something new from more experienced users.

• (#1+#2+#3) can currently be replaced by Plus@##. It might or might not be a good idea to store the parameters in short-named variables or even use a proper function declaration. – the default. May 2 '20 at 2:59

# APL (Dyalog Extended), 45 44 bytes

+/×{+/{a b c←⍵⋄√(a×a÷b×c)+2-(⊢+÷)b÷c}⌽∘⍵¨⍳3}


Try it online!

Bubbler has a 27 byte solution with a complete train, but I think it deserves it's own answer.

This is a port of Surculose Suptum's formula, simplified by Kevin Cruijssen.

-1 byte from dzaima.

# Java 8, 104 bytes

(a,b,c)->{double r=0,t,n=3;for(;n-->0;t=a,a=b,b=c,c=t)r+=(a+b+c)*Math.sqrt(c*c/a/b-a/b-b/a+2);return r;}


Iterative port of @SurculoseSputum's Python answer, so make sure to upvote him!

Try it online.

Explanation:

(a,b,c)->{                  // Method with double as all three parameters and return-type
double r=0,               // Result-sum, starting at 0
t,                 // Temp-double
n=3;for(;n-->0            // Loop 3 times:
;                 //   After every iteration:
t=a,a=b,b=c,c=t) //    Rotate a,b,c to b,c,a respectively
r+=                     //  Increase the result-sum by:
(a+b+c)               //   The sum of a,b,c
*Math.sqrt(           //   Multiplied by the square-root of:
c*c                //    c squared
/a/b            //    Divided by both a and b
-a/b               //    Minus a divided by b
-b/a               //    as well as b divided by a
+2);               //    Plus 2
return r;}                //  After the loop, return the result-sum


Or as a single formula:

$$p = (a+b+c)\sqrt{c^2\div a\div b-\frac{a}{b}-\frac{b}{a}+2}$$ $$+(a+b+c)\sqrt{a^2\div b\div c-\frac{b}{c}-\frac{c}{b}+2}$$ $$+(a+b+c)\sqrt{b^2\div c\div a-\frac{c}{a}-\frac{a}{c}+2}$$

# Charcoal, 24 bytes

Ｉ×ΣθΣＥθ₂⁻²∕×ι⁻ΣＸθ²⊗×ιιΠθ


Try it online! Link is to verbose version of code. Port of @xnor's formula. Explanation:

     Ｅθ                     Map over sides
ΣＸθ²          Sum of sides squared
⁻              Subtract
×ιι      Square of current side
⊗         Doubled
×ι               Multiply by current side
∕           Πθ    Divide by product of sides
⁻²                  Subtract from 2
₂                    Square root
Σ                       Take the sum
×                          Multiplied by
Σθ                        Sum of sides
Ｉ                           Cast to string for implicit print


Unfortunately SquareRoot doesn't seem to vectorise so I have to do that inside the Map which makes it marginally less efficient.

# JavaScript (ES7), 62 bytes

Now a port of Surculose Sputum's answer.

f=(a,b,c,n)=>n^3&&(a+b+c)*(c*c/a/b-a/b-b/a+2)**.5+f(b,c,a,-~n)


Try it online!