猜测无界整数整数、无界

2023-09-11 04:12:45 作者:落ㄖ旳余晖

如果我对你说:

我是0到n之间思维的一个号码,我会告诉你,如果你的猜测是高还是低,那么你会立刻伸手二进制搜索。

"I am thinking of a number between 0 and n, and I will tell you if your guess is high or low", then you will immediately reach for binary search.

如果我删除上限?即我想到一个正整数,你需要去猜测吧。

What if I remove the upper bound? i.e. I am thinking of a positive integer, and you need to guess it.

一种可能的方法是让你猜2,4,8,...,直到你猜2 **亩一些k和我说:低。然后你就可以申请二进制搜索。

One possible method would be for you to guess 2, 4, 8, ..., until you guess 2**k for some k and I say "lower". Then you can apply binary search.

有没有更快的方法?

编辑:

显然,任何解决方案是要花费时间正比于目标数的大小。如果我通过阿克曼函数扔下格雷厄姆的数量,我们会等着什么策略,你追求了一会儿。

Clearly, any solution is going to take time proportional to the size of the target number. If I chuck Graham's number through the Ackermann function, we'll be waiting a while whatever strategy you pursue.

我可以提供这个算法太:猜猜依次在每个整数,从1开始

I could offer this algorithm too: Guess each integer in turn, starting from 1.

它保证在有限时间内完成,但但它显然不是战略我的2的幂差多了。如果我能找到一个更糟糕的算法(并且知道它是坏),那么也许我能找到一个更好的?

It's guaranteed to finish in a finite amount of time, but yet it's clearly much worse than my "powers of 2" strategy. If I can find a worse algorithm (and know that it is worse), then maybe I could find a better one?

例如,而不是2的幂,也许我可以用10的幂然后我找到了上界 log_10(N)的步骤,而不是 log_2(N)步骤。但我必须再寻找一个更大的空间。说 K = CEIL(log_10(N))。然后,我需要 log_2(10 **亩 - 10 **(K-1))我的二进制搜索,我的猜测是对的步骤 10 + log_2(K)。对于2的幂,我有大约 log_2(log_2(N))步骤,我的搜索​​阶段。哪个赢了?

For example, instead of powers of 2, maybe I can use powers of 10. Then I find the upper bound in log_10(n) steps, instead of log_2(n) steps. But I have to then search a bigger space. Say k = ceil(log_10(n)). Then I need log_2(10**k - 10**(k-1)) steps for my binary search, which I guess is about 10+log_2(k). For powers of 2, I have roughly log_2(log_2(n)) steps for my search phase. Which wins?

如果我寻找向上使用ñ** N ?或者一些其他的序列?请问该奖项去谁就可以发现,增长最快的顺序?这是一个答案的问题吗?

What if I search upwards using n**n? Or some other sequence? Does the prize go to whoever can find the sequence that grows the fastest? Is this a problem with an answer?

感谢您的想法。而我对不住那些你建议我开始MAX_INT或2 ** 32-1,因为我清楚地从实用性这里的界限渐行渐远。

Thank you for your thoughts. And my apologies to those of you suggesting I start at MAX_INT or 2**32-1, since I'm clearly drifting away from the bounds of practicality here.

最后编辑:

大家好,

感谢您的答复。我接受了诺曼·拉姆齐(及评议onebyone)的答案是我理解为以下几种说法:一个目标数n,任何战略必须能够区分(至少),从0到n,这意味着数你需要(至少)O(日志(n))的比较。

Thank you for your responses. I accepted the answer by Norman Ramsey (and commenter onebyone) for what I understood to be the following argument: for a target number n, any strategy must be capable of distinguishing between (at least) the numbers from 0..n, which means you need (at least) O(log(n)) comparisons.

不过你seveal也指出,这个问题是不是在第一时间明确定义,因为这是不可能选择一个随机正整数下的均匀概率分布(或者更确切地说,一个统一的概率分布不能存在过一个无限集)。有一次我给你一个不均匀分布,可以在半分割并应用二进制搜索是正常的。

However seveal of you also pointed out that the problem is not well-defined in the first place, because it's not possible to pick a "random positive integer" under the uniform probability distribution (or, rather, a uniform probability distribution cannot exist over an infinite set). And once I give you a nonuniform distribution, you can split it in half and apply binary search as normal.

这是我经常思考,我四处走动的一个问题,所以我很高兴有两个决定性的答案吧。

This is a problem that I've often pondered as I walk around, so I'm pleased to have two conclusive answers for it.

推荐答案

最坏的情况下,你可以找到它在时间对数的使用完全相同您所描述的方法,答案的大小。可能使用阿克曼的函数查找的上限比对数时间更快,但随后猜数和previous猜测之间的二分搜索将需要时间对数的区间中,其尺寸(如果猜测非常迅速增长)是接近答案的大小对数。

Worst case, you can find it in time logarithmic in the size of the answer using exactly the methods you describe. You might use Ackermann's function to find an upper bound faster than logarithmic time, but then the binary search between the number guessed and the previous guess will require time logarithmic in the size of the interval, which (if guesses grow very quickly) is close to logarithmic in the size of the answer.

这将是有趣的尝试的证明的,有没有更快的算法(例如,为O(log log n)的的),但我不知道该怎么办吧。

It would be interesting to try to prove that there is no faster algorithm (e.g., O(log log n)), but I have no idea how to do it.

 
精彩推荐
图片推荐