Go south, east and north, come to the same place

7

There is that famous riddle:

A bear walks 1 km south, 1 km east and 1 km north, and comes to the place it started from. What color is the bear?

This one is very old, so I'll generalize it:

A bear walks x km south, y km east and x km north, and comes to the place it started from. This bear thinks that the North Pole is boring, so it never goes there. Which latitude did it start and end at?

Assume that the bear is an idealized point, and the Earth is a perfect sphere with 10000 km distance from the pole to the equator (however, you can use a more accurate model if it's easier for some reason).

Write a program or a subroutine that receives the two numbers x and y, and prints or returns the latitude.

Fine points:

  • Input with at least 3 decimal places of precision should be supported
  • Input is in the range (0...99999), bounds not inclusive
  • If the situation is physically impossible, print an empty string or a message, or return an impossible value (like 91 or NaN - or, in fact, 90 - since 90 is an impossible value)
  • You should print the most northern (maximal) latitude that satisfies the conditions, except the North Pole. If the North Pole is the only solution, see above.
  • Output precision should be like IEEE single precison or better (24 bits or 7 decimal digits of precision).
  • If the output is printed, use decimal notation (e.g. -89.99 for a point near the South Pole) . If the output is returned, use floating-point or fixed-point notation.

Shortest code in bytes wins.

anatolyg

Posted 2015-07-09T09:35:50.587

Reputation: 10 719

1Am I missing something, or is this only possible if going y km east makes the bear return to the same place by doing an integer number of laps around? – xnor – 2015-07-09T10:07:08.660

@xnor That is correct, I believe. – isaacg – 2015-07-09T10:12:36.567

3I feel like this is really an algebra/geometry problem, and once you solve it, it will be straightforward to code it in a mathematical language. – xnor – 2015-07-09T10:15:42.043

So if y = 0 and x < 20000 are you expecting the largest representable number smaller than 90? What if that then gets formatted to 90 by the printf or equivalent? And what intermediate precision and accuracy is required? – Peter Taylor – 2015-07-09T10:36:10.327

@PeterTaylor I disallowed zero specifically to avoid this problem – anatolyg – 2015-07-09T10:52:33.637

@xnor I feel there should be some cases (like when solving a cubic equation - is the discriminant negative? what about reusing the code that calculates it in the code that calculates the roots?) and opportunities for golfing; didn't solve it so cannot say for sure. – anatolyg – 2015-07-09T10:58:18.417

@PeterTaylor Regarding intermediate calculations - ideally, I'd say "use whatever is necessary to make the output accurate enough". However, it's difficult to verify floating-point precision in all situations, and this question is not about FP accuracy. So just use whatever feels right. – anatolyg – 2015-07-09T12:33:12.800

Answers

5

CJam, 62 chars

{4e4/:Y\9e-3*:X90-P180/:Q*mc/XY2$m](/mCQ/+_90<\@XY@m[)/mCQ/-?}

This is a block (subroutine) which takes x y on the stack and leaves the solution on the stack.

Online demo

I'm interpreting the spec to allow returning 90 as an "impossible value" if there is no other solution. If that interpretation is disallowed, I think the best fix would be

{4e4/:Y\9e-3*:X90-P180/:Q*mc/[XY2$m](/mCQ/+\XY@m[)/mCQ/--91]{90<}=}

Peter Taylor

Posted 2015-07-09T09:35:50.587

Reputation: 41 901

1I have no idea how it works (magic, I guess), but it seems to include all the edge cases! – anatolyg – 2015-10-08T17:15:25.850