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
-
\$\begingroup\$ @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). \$\endgroup\$Bubbler– Bubbler2020年04月24日 00:57:15 +00:00Commented Apr 24, 2020 at 0:57
-
\$\begingroup\$ That's fair, but someone might! \$\endgroup\$FryAmTheEggman– FryAmTheEggman2020年04月24日 01:09:00 +00:00Commented Apr 24, 2020 at 1:09
-
10\$\begingroup\$ Thanks for all the John Conway challenges -- they're a nice homage to him. \$\endgroup\$Mitchell Spector– Mitchell Spector2020年04月24日 03:57:03 +00:00Commented Apr 24, 2020 at 3:57
11 Answers 11
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)
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)$$
-
1\$\begingroup\$ Great solution, and shorter than mine! I'll post a bounty when this challenge is old enough to allow it. \$\endgroup\$xnor– xnor2020年04月24日 04:40:38 +00:00Commented Apr 24, 2020 at 4:40
-
1\$\begingroup\$ It looks like expanding out
(c*c-(a-b)**2)/a/b->c*c/a/b-a/b-b/a+2works to save a byte. \$\endgroup\$xnor– xnor2020年04月24日 04:43:32 +00:00Commented Apr 24, 2020 at 4:43 -
\$\begingroup\$ @xnor Nice golf, thanks! Also I appreciate the bounty! \$\endgroup\$Surculose Sputum– Surculose Sputum2020年04月24日 05:02:22 +00:00Commented Apr 24, 2020 at 5:02
Python 3, 64 bytes
lambda*t:eval("+((-(%s-%s)**2+%s**2)/%s/%s)**.5"*3%(t*5))*sum(t)
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])
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
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).
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))
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])&
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.
-
\$\begingroup\$
(#1+#2+#3)can currently be replaced byPlus@##. It might or might not be a good idea to store the parameters in short-named variables or even use a proper function declaration. \$\endgroup\$the default.– the default.2020年05月02日 02:59:58 +00:00Commented May 2, 2020 at 2:59
APL (Dyalog Extended), (削除) 45 (削除ここまで) 44 bytes
×ばつ{+/{a b c←⍵⋄√(a×ばつa×ばつc)+2-(⊢+÷)b÷c}⌽∘⍵ ̈⍳3}
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!
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:
Eθ Map over sides
ΣXθ2 Sum of sides squared
− Subtract
×ばつιι Square of current side
⊗ Doubled
×ばつι Multiply by current side
∕ Πθ Divide by product of sides
−2 Subtract from 2
2 Square root
Σ Take the sum
×ばつ Multiplied by
Σθ Sum of sides
I 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.
-
\$\begingroup\$ 22 bytes using the newer version of Charcoal on ATO:
I×ΣθΣ2−²∕×θ−ΣXθ²⊗Xθ²ΠθAttempt This Online Link is to verbose version of code. \$\endgroup\$Neil– Neil2022年09月25日 21:20:21 +00:00Commented Sep 25, 2022 at 21:20
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)