Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to find an arbitrarily large number

Tags:

algorithm

Here's something I've been thinking about: suppose you have a number, x, that can be infinitely large, and you have to find out what it is. All you know is if another number, y, is larger or smaller than x. What would be the fastest/best way to find x?

An evil adversary chooses a really large number somehow ... say:

int x = 9^9^9^9^9^9^9^9^9^9^9^9^9^9^9

and provides isX, isBiggerThanX, and isSmallerThanx functions. Example code might look something like this:

int c = 2
int y = 2
while(true)
    if isX(y) return true
    if(isBiggerThanX(y)) fn()
    else y = y^c

where fn() is a function that, once a number y has been found (that's bigger than x) does something to determine x (like divide the number in half and compare that, then repeat). The thing is, since x is arbitrarily large, it seems like a bad idea to me to use a constant to increase y.

This is just something that I've been wondering about for a while now, I'd like to hear what other people think

like image 898
ahota Avatar asked Apr 02 '12 02:04

ahota


People also ask

What is arbitrarily large number?

In mathematics, the phrases arbitrarily large, arbitrarily small and arbitrarily long are used in statements to make clear of the fact that an object is large, small and long with little limitation or restraint, respectively.

What is big number arithmetic?

In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system.


2 Answers

Use a binary search as in the usual "try to guess my number" game. But since there is no finite upper end point, we do a first phase to find a suitable one:

  • Initially set the upper end point arbitrarily (e.g. 1000000, though 1 or 1^100 would also work -- given the infinite space to work in, all finite values are equally disproportionate).
  • Compare the mystery number X with the upper end point.
  • If it's not big enough, double it, and try again.
  • Once the upper end point is bigger than the mystery number, proceed with a normal binary search.

The first phase is itself similar to a binary search. The difference is that instead of halving the search space with each step, it's doubling it! The cost for each phase is O(log X). A small improvement would be to set the lower end point at each doubling step: we know X is at least as high as the previous upper end point, so we can reuse it as the lower end point. The size of the search space still doubles at each step, but in the end it will be half as large as would have been. The cost of the binary search will be reduced by only 1 step, so its overall complexity remains the same.

Some notes

A couple of notes in response to other comments:

It's an interesting question, and computer science is not just about what can be done on physical machines. As long as the question can be defined properly, it's worth asking and thinking about.

The range of numbers is infinite, but any possible mystery number is finite. So the above method will eventually find it. Eventually is defined such as that, for any possible finite input, the algorithm will terminate within a finite number of steps. However since the input is unbounded, the number of steps is also unbounded (it's just that, in every particular case, it will "eventually" terminate.)

like image 172
Edmund Avatar answered Sep 24 '22 02:09

Edmund


If I understand your question correctly (advise if I do not), you're asking about how to solve "pick a number from 1 to 10", except that instead of 10, the upper bound is infinity.

If your number space is truly infinite, the following are true:

  • The value will never be held in an int (or any other data type) on any physical hardware
  • You will NEVER find your number

If the space is immensely large but bound, I think the best you can do is a binary search. Start at the middle of the number range. If the desired number turns out to be higher or lower, divide that half of the number space, and repeat until the desired number is found.

In your suggested implementation you raise y ^ c. However, no matter how large c is chosen to be, it will not even move the needle in infinite space.

like image 40
Eric J. Avatar answered Sep 23 '22 02:09

Eric J.