10
\$\begingroup\$

I'm surprised we don't have the crossed ladders problem as a task here yet.
Setup

Two ladders of lengths a and b lie oppositely across an alley, as shown in the figure. The ladders cross at a height of h above the alley floor. What is the width of the alley?

Without loss of generality we can set h = 1. In this MSE answer I show that with this assumption the width w satisfies the following quartic in \$w^2\$: $$w^8-2(a^2+b^2-2)w^6+(a^4+4a^2b^2+b^4-6a^2-6b^2)w^4-2(a^4b^2+a^2b^4-a^4-4a^2b^2-b^4)w^2+(ab+a+b)(ab+a-b)(ab-a+b)(ab-a-b)=0$$ Obviously we must have \$a,b>1\$ to be able to cross the ladders at height 1. In this range of the input variables the quartic in \$w^2\$ will have two real roots. The smaller real root of the quartic is negative if \$ab<a+b\$, and in this case the configuration cannot be physically realised; otherwise that root is the correct value of \$w^2\$.

For this task, however, you are free to use any method to obtain \$w\$.

Task

Given ladder lengths \$a,b>1\$ with \$ab\ge a+b\$ and assuming the crossing height h = 1, calculate the alley width w to at least 4 decimal places.

Test cases

a, b, w
2, 2, 0.0
2, 3, 1.23118572377867
3, 3, 2.23606797749979
8/3, 10/3, 2.10872747840039
1.5, 4.5, 0.763513778701349
1.125, 9, 0.0
6, 9, 5.88439718860116
10, 10, 9.79795897113271
\$\endgroup\$
2
  • \$\begingroup\$ Are you sure? If the known are only a and b, the ipotenusis, One can Imagine some other solution, there are infinite solutions for me \$\endgroup\$
    – Rosario
    Commented 17 hours ago
  • 1
    \$\begingroup\$ @Rosario You forgot that I set h = 1 here, which makes the solution unique. \$\endgroup\$ Commented 15 hours ago

4 Answers 4

6
\$\begingroup\$

JavaScript (ES7), 87 bytes

Expects (a)(b).

a=>b=>(i=99,h=k=>(b*b-k*k)**.5)((g=x=>i--?g((1+c*x**3)**.25):2*x/c+2/c/x||2)(c=4/h(a)))

Try it online!

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 solve \$x^3(x-c)-1=0\$ using the 4-th root iteration 99 times:

$$x\gets (1+cx^3)^{1/4}$$

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 last moment if we get NaN via the main method.

\$\endgroup\$
3
\$\begingroup\$

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                 

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
3
\$\begingroup\$

Pyth, 22 bytes

.Ism.x@a^d2^G2_ygdGTQ1

Try it online!

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

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^4-2B^3+(a^2-b^2)(B^2-2B+1)

for to find one of its real roots B', and than calculate W using Pythagorean theorem

W=√a^2-B'^2

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 choosed 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 ok) 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 using one counter for limit the loop cicles, but it would be more long.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.