| Bytes | Lang | Time | Link |
|---|---|---|---|
| 115 | Lua | 250906T002009Z | Someone |
| 086 | Maple | 250825T141128Z | dharr |
| 082 | JavaScript ES7 | 250822T181232Z | Arnauld |
| 038 | Charcoal | 250824T072536Z | Neil |
| 085 | APLNARS | 250822T210929Z | Rosario |
| 022 | Pyth | 250822T181305Z | CursorCo |
| 107 | APL+WIN | 250822T143651Z | Graham |
Lua, 116 115 bytes
Can this be golfed? Absolutely. This is my first ever Lua solution and its size could probably be decimated by someone more competent. I just wanted to try implementing Newton's Method. (addendum: the APL solution does it too, which is neat)
Addendum 2: lost a junk space thanks to @Adamátor
function(a,b)c=b^2-a^2 B=2 for i=0,9 do B=B-(B^4-2*B^3+(B-1)^2*c)/(4*B^3-6*B^2+2*c*B-2*c)end return(a^2-B^2)^.5 end
Explanation
function(a,b)c=b^2-a^2 B=2 for i=0,9 do B=B-(B^4-2*B^3+(B-1)^2*c)/(4*B^3-6*B^2+2*c*B-2*c)end return(a^2-B^2)^.5 end
function(a,b) end -- creates a function with two arguments—A and B
c=b^2-a^2 -- convenience variable to reduce redundancy
B=2 -- bad starting guess for Newton's method
for i=0,9 do end -- repeat ten times…
B=B-(B^4-2*B^3+(B-1)^2*c)/(4*B^3-6*B^2+2*c*B-2*c) -- subtract our guess B by P(B)/P'(B), where P is as given in Wikipædia (linked below)
return(a^2-B^2)^.5 -- return w from B
💎
Created with the help of Luminespire.
The polynomial I used is from here.
Maple, 88 86 bytes
(a,b)->rhs(fsolve({A*B=A+B,A^2+w^2=b^2,B^2+w^2=a^2},{A=(r:=0..infinity),B=r,w=r})[3]);
Solves the three simultaneous equations (from the Wikipedia page) for nonnegative solutions and displays the value of w. (Replacing infinity by a large but shorter constant such as 1e5 means it fails to find the solution in the cases where w=0.)
JavaScript (ES7), 82 bytes
Expects (a)(b).
a=>g=(b,x=c=4/(h=k=>(b*b-k*k)**.5)(a))=>b?g(b/2,(1+c*x**3)**.25):h(2*x/c+2/c/x||2)
Method
This is based on the 2nd method described on the Wikipedia page.
Assuming \$a<b\$, we define:
$$d=\sqrt{b^2-a^2},\:x=\dfrac{A+B}{d},\:c=\dfrac{4h}{d}=\dfrac{4}{d}$$
Then we look for a fixed-point for \$x^3(x-c)-1=0\$, starting with \$x=c\$ and processing ~1000 iterations of:
$$x\gets (1+cx^3)^{1/4}$$
(Which is more than enough. The value typically stabilizes under IEEE 754 precision in fewer than 100 iterations.)
Finally, we have:
$$A=d(x+1/x)/2$$ $$w=\sqrt{b^2-A^2}$$
If \$a=b\$, we use \$A=2h=2\$ instead. In the JS implementation, this is done at the very last moment if we get NaN via the main method.
NB: \$d\$ is defined above for readability. In the current version of the JS code, only \$c\$ is used and the expressions are rearranged accordingly.
Commented
a => // outer function taking a
g = ( // g is a recursive function taking:
b, // b (reused for the "stop" condition)
x = // the unknown x, initialized to c
c = 4 / ( // c = 4 / h(a)
h = k => // where h is a helper function taking k
(b * b - k * k) // and returning sqrt(b² - k²)
** .5 //
)(a) //
) => //
b ? // if b is not zero:
g( // do a recursive call with:
b / 2, // b / 2 (which will eventually evaluate
( // to 0 due to arithmetic underflow)
1 + c * x ** 3 // the next approximation of x
) ** .25 // i.e. (1+cx**3)**(1/4)
) // end of recursive call
: // else:
h( // apply h to:
2 * x / c + // 2x/c + 2/c/x
2 / c / x // or 2 if this evaluates to NaN
|| 2 // because x = c = +∞,
) // which happens when a = b
Charcoal, 38 bytes
F²⊞υN≔⟦⌊υ⁰⟧θFγ§≔θ›¹Σ∕¹₂⁻Xυ²X⊘Σθ²⊘ΣθI⊟θ
Attempt This Online! Link is to verbose version of code. Explanation: Binary search.
F²⊞υN
Input a and b.
≔⟦⌊υ⁰⟧θ
Assume that min(a, b) > w >= 0.
Fγ
Repeat 95 times, because I might as well. (g is actually printable ASCII. Other variables I could use are f for 1000, or a or b for the alphabet, i.e. 26, but that would reduce the accuracy.)
§≔θ›¹Σ∕¹₂⁻Xυ²X⊘Σθ²⊘Σθ
Calculate 1/h for the midpoint of the range, and update the appropriate endpoint depending on whether it is greater than 1 or not.
I⊟θ
Output the lower endpoint of the final range.
APL(NARS), 85 chars
r←f w;a;p;h
p←a←↑⌽w⋄h←{⍵⊥1,¯2,(-/w*2)×1,¯2,1}
→3×⍳p=r←p-(h p)÷h∂p⋄p←r⋄→2
r←√-/2*⍨a r
// +/ 12 34 27 12 = 85
f would use the Newton method on the polinomy h of the problem, that would be this (if b a is the input b<=a and A B as in the picture in question)
B⁴-2B³+(b²-a²)(B²-2B+1)
for to find one of its real roots B', and than calculate W using Pythagorean theorem
W=√(a²-B'²)
f has to have as input 2 positive numbers b,a with b<=a than it calculate polinomy h and the initial point for Newton method chosen in the second input "a". It would return one number in precision according the value of float precision type in use.
It is possible there is one never ending loop in Newton method (if the initial point is not okay) or it not converge to a solution, as it is possible it converge to the wrong root...
In case of infinite loop it is possible to use one counter to limit the loop cycles, but it would be longer.
Test:
f 2 2
0
f 2 3
1.231185724
f 8r3 10r3
2.108727478
f 10 10
9.797958971
f 3 3
2.236067977
f 1.5 4.5
0.7635137787
f 1.125 9
0
f 6 9
5.884397189
Pyth, 22 bytes
.Ism.x@a^d2^G2_ygdGTQ1
First time I've ever actually used .I in a pyth answer.
Explanation
# implicitly assign Q = eval(input())
.I 1 # Invert. Find the input which produces the output 1 from lambda G
s # sum of
m Q # map lambda d over Q
.x T # try to evaluate expression and on error output 10
a^d2^G2 # absolute difference between d^2 and G^2
@ _ # to the negative reciprocal power of
ygdG # 0 if G < d, otherwise 2 (when this is 0 it induces an error, triggering the .x from earlier)
APL+WIN, 107 bytes
Simple numerical solution. Prompts for a followed by b
A←b←⎕⋄a←⎕
:while 1E¯5<r←(A*4)-(2×A*3)-((1-A)*2)×((a*2)-b*2)
A←A-r×1E¯5
:end
((b*2)-A*2)*.5