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.

  • 1
    \$\begingroup\$ Am 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? \$\endgroup\$
    – xnor
    Jul 9, 2015 at 10:07
  • \$\begingroup\$ @xnor That is correct, I believe. \$\endgroup\$
    – isaacg
    Jul 9, 2015 at 10:12
  • 3
    \$\begingroup\$ I 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. \$\endgroup\$
    – xnor
    Jul 9, 2015 at 10:15
  • \$\begingroup\$ 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? \$\endgroup\$ Jul 9, 2015 at 10:36
  • \$\begingroup\$ @PeterTaylor I disallowed zero specifically to avoid this problem \$\endgroup\$
    – anatolyg
    Jul 9, 2015 at 10:52

1 Answer 1


CJam, 62 chars


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

  • 1
    \$\begingroup\$ I have no idea how it works (magic, I guess), but it seems to include all the edge cases! \$\endgroup\$
    – anatolyg
    Oct 8, 2015 at 17:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.