The “guess the number” game for arbitrary rational numbers?

后端 未结 8 1053
既然无缘
既然无缘 2020-12-12 09:12

I once got the following as an interview question:

I\'m thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) quer

8条回答
  •  温柔的废话
    2020-12-12 09:44

    Remember that any rational number in (0, 1) can be represented as a finite sum of distinct (positive or negative) unit fractions. For example, 2/3 = 1/2 + 1/6 and 2/5 = 1/2 - 1/10. You can use this to perform a straight-forward binary search.

提交回复
热议问题