5

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

Matt McHenry
  • 20,009
  • 8
  • 65
  • 64
Austin Gayler
  • 4,038
  • 8
  • 37
  • 60
  • 1
    O(1) for finding `y` equal to `x`: `y = x`. It's not really clear what you mean by "determine x". Where is its value coming from? Why do you need `y` to be greater than `x` before you start dividing `x`? – jscs Apr 02 '12 at 02:52
  • 1
    Didn't really mean for my attempted pseudocode to be essential to the question, but I'll try to explain it anyways. I don't know where x comes from, it's just a number. And I made wanted to find a range of numbers that contained x first, then somehow determine x based on that (although I don't really know what would happen from that point onward). – Austin Gayler Apr 02 '12 at 03:01
  • And thanks Matt for editing/clarifying my question. – Austin Gayler Apr 02 '12 at 03:02
  • How can you reliably determine a range of numbers that contain `x` if 1) you do not know what `x` is and 2) `x` may be infinitely large? – aroth Apr 02 '12 at 03:03
  • You should google `binary search algorithm`. Also, SO is not about infinitely large numbers, it's about programming. There are no infinitely large numbers in programming. – iehrlich Apr 02 '12 at 02:53
  • 2
    This question is badly phrased: "infinitely large" should be "arbitrarily large". – tskuzzy Apr 02 '12 at 04:44
  • 1
    suddnely_me, so there is no programming involved in creating a CAS? They work by magic! Cool. – Joey Apr 02 '12 at 06:50

7 Answers7

8

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.)

Edmund
  • 10,533
  • 3
  • 39
  • 57
  • If the "range of numbers" is infinite, it's not true that any possible mystery number is finite. No matter how large your initial value, and no matter whether you double or even raise to the 100th power, there is absolutely no guarantee that you will ever find a "random" number in infinite space. In fact, it is infinitely improbable that you will ever find your number using this method. – Eric J. Apr 02 '12 at 03:30
  • 2
    @EricJ. No, he's right. That there are infinitely many integers doesn't mean that there's an infinite integer; every integer is finite, and so can be found by this method in finite time. You seem to be thinking in terms of the expectation value of the time-to-find X given that X was randomly drawn in some sense, but that's a separate question (and not necessarily a well-defined one.) – DSM Apr 02 '12 at 03:34
  • 1
    @DSM: There is not "an" infinite anything. Infinity is a concept, not a specific number. When you apply it to the positive integers, one implication is that if you draw a dot on the number line representing ANY finite number, your dot approaches the zero point of the number line. There is an endless supply of "finite" integers to the right of your dot, and a bounded number of integers to the left of your dot. Since the amount of numbers to the left of your dot is vastly less than the (unbounded) amount of numbers to the right, your finite number is infinitely (as a ratio) close to zero. – Eric J. Apr 02 '12 at 03:43
  • 1
    @EricJ: there certainly *are* objects fairly described as infinite numbers, in that they're not finite but satisfy arithmetic rules, but that's another issue. You claimed "it's not true that any possible mystery number is finite", and this is manifestly false; every such number is finite. You claimed that it is "infinitely improbable that you will ever find your number using this method", and that is also false; Edmund's method will always find the mystery number, whatever it is. – DSM Apr 02 '12 at 03:49
  • 1
    @DSM: Rebut my argument about the number line successfully and I'll agree and upvote this answer. This method will never move the dot significantly from the zero point of the number line, because there are always "vastly more" numbers to the right of the dot than there are to the left so the chance that you have yet reached the finite number in infinite space, for any finite iteration of the algorithm, is vanishingly small. – Eric J. Apr 02 '12 at 03:56
  • 2
    @EricJ. This algorithm is sound and will always terminate for any given input. The running time is bounded by the size of the answer, which is surely finite for any instance of the problem. – tskuzzy Apr 02 '12 at 04:46
  • 4
    As I have mentioned in a comment in the question itself, I believe that the misunderstanding lies in the fact that the mystery number itself is not infinite. It is **not** "infinitely large", but rather finite albeit arbitrarily large. This is similar to the definition of a limit to infinity where we let `N` be an arbitrarily large number. – tskuzzy Apr 02 '12 at 04:48
  • @tskuzzy: It's irrelevant that the number is not "infinity". Please, try and directly counter argue the number line. You will see why if you do that. – Eric J. Apr 02 '12 at 05:18
  • 2
    @EricJ. I'm afraid I don't understand your point with the number line example and how that's relevant to this problem. Yes, there are infinitely many more numbers to the "right", that much is obvious. But that doesn't mean that the algorithm won't terminate for that dot. – tskuzzy Apr 02 '12 at 05:39
  • 2
    @EricJ: I'm not sure what your number line argument is, though. It sounds a lot like "there are infinitely more numbers above X than below X, so if someone chooses some X, it must take forever to count to X". After all, I can always simply ask "is it 1? is it 2? is it 3?", and I'll get there in finite time, because **the number is finite**, despite your "not true that any possible mystery number is finite" claim. – DSM Apr 02 '12 at 06:01
2

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.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 3
    Such is the nature of infinity :-) – Eric J. Apr 02 '12 at 02:59
  • What if I have a piece of hardware with infinite memory? – Austin Gayler Apr 02 '12 at 03:13
  • 1
    @A.J. Then you don't live in the physical universe. If you use the quantum spin of every single particle in the universe as the 1's and 0's of your memory, you will still have finite memory. Dealing with infinity is always strange. – Eric J. Apr 02 '12 at 03:26
1

Infinity isn't a number. Thus you can't find it, even with a computer.

Larry Battle
  • 9,008
  • 4
  • 41
  • 55
1

That's funny. I've wondered the same thing for years, though I've never heard anyone else ask the question.

As simple as your scenario is, it still seems to provide insufficient information to allow the choice of an optimal strategy. All one can choose is a suitable heuristic. My heuristic had been to double y, but I think that I like yours better. Yours doubles log(y).

The beauty of your heuristic is that, so long as the integer fits in the computer's memory, it finds a suitable y in logarithmic time.

Counter-question. Once you find y, how do you proceed?

thb
  • 13,796
  • 3
  • 40
  • 68
  • If you are dealing with integers that fit into (any) computer's memory, the optimal algorithm is still a binary search. If you have a known computing architecture, you also know the bounds of an integer it can represent. Start in the middle of that range and perform a binary search. – Eric J. Apr 02 '12 at 04:16
1

I agree with using binary search, though I believe that a ONE-SIDED binary search would be more suitable, since here the complexity would NOT be O( log n ) [ Where n is the range of allowable numbers ], but O( log k ) - where k is the number selected by your adversary.

This would work as follows : ( Pseudocode )

k = 1;

while( isSmallerThanX( k ) )
{
   k = k*2;
}

// At this point, once the loop is exited, k is bigger than x
// Now do normal binary search for the range [ k/2, k ] to  find your number :)

So even if the allowable range is infinity, as long as your number is finite, you should be able to find it :)

arya
  • 565
  • 1
  • 6
  • 17
  • No you will (almost certainly) not find it. See comments under Edmund's answer. – Eric J. Apr 02 '12 at 04:14
  • I am sorry, but I disagree. The very fact that an adversary "chooses" a number, ensures that such a number would be finite. It makes sense to assume that he does not say "I choose positive infinity". So if he selected a particular number ( say x ), although it may be arbitrarily large, if we choose x+1, we find that x+1 > x. So x obviously cannot be infinite. – arya Apr 02 '12 at 04:19
  • Yes, but... Draw a number line with a zero point. Now put a dot at the number you choose. There are a bounded number of integers to the left of your dot. There are an unbounded number of integers to the right of your dot. No matter what number you choose, the odds are vastly against you having selected a number larger than the adversary's selected number. Almost all numbers are to the right of your chosen number on the number line. – Eric J. Apr 02 '12 at 04:27
  • I am not randomly selecting an upper bound you see ?? You state - "There are a bounded number of integers to the left of your dot". well I go through these bounded number of values systematically in logarithm of the bound size, which is also bounded. So at some point I will have crossed out all these numbers ( in O( log k ) time, actually, where k is the adversary's number ) and found the first number that is larger than k. – arya Apr 02 '12 at 04:33
  • If the adversary selects a truly random (finite) number, it is almost certainly to the RIGHT of your chosen number, not to the left. On the other hand, if you KNOW the upper bound of numbers the adversary might choose from, the best you can do is start in the middle of that range and perform a binary search. – Eric J. Apr 02 '12 at 04:42
  • 1
    Which is why I use one-sided binary search :) Here, the upper bound, can be viewed, not as infinity, but as 2*k. This is a technique frequently used to cut down the search space before using normal binary search :) – arya Apr 02 '12 at 04:44
  • Irrelevant. If the adversary selects a finite number in infinite space, you will (statistically) never reach it for any value of k. – Eric J. Apr 02 '12 at 05:20
  • 1
    There is no mention of statistics. Given a finite number `n`, the algorithm runs in `O(log n)` time. The fact that the number can be arbitrarily large does not change this fact. No matter which number your "adversary" picks, it will always be finite. You cannot not choose an arbitrarily large number, you can only choose a finite number, albeit the upper bound is arbitrary. Also, the "you will never reach it" statement makes no sense. In all computer science, you assume the computer will work forever or until it finds a solution/value to return, whichever comes first. – Undreren Apr 02 '12 at 09:40
  • @EricJ. This has nothing to do with chance, odds, randomness, almost certainty etc. (And do you know that there's no uniform probability distribution on the natural numbers? But that's **not** the point.) – Reinstate Monica Apr 02 '12 at 11:20
1

Your method of tetration is guaranteed to take longer than the age of the universe to find an answer, if the opponent merely uses a paradigm which is better (for example, pentation). This is how you should do it:

  1. You can only do this with symbolic representations of numbers, because it is trivial to name a number your computer cannot store in floating-point representation, even if it used arbitrary-precision arithmetic and all its memory.
  2. Required reading: http://www.scottaaronson.com/writings/bignumbers.html - that pretty much sums it up
  3. How do you represent a number then? You represent it by a program which will, if run to completion, print out that number. Even then, your computer is incapable of computing BusyBeaver(10^100) (if you dictated a program 1 terabyte in size, this well over the maximum number of finite clock cycles it could run without looping forever). You can see that we could easily have the computer print out 1 0 0... each clock cycle, making the maximum number it could say (if we waited nearly an eternity) would be 10^BusyBeaver(10^100). If you allowed it to say more complicated expressions like eval(someprogram), power-towers, Ackermann's function, whatever-- then I believe that would be no better than increasing the original 10^100 by some constant proportional to the complexity of what you described (plus some logarithmic interpreter factor, see Kolmogorov complexity).

So let's frame this another way:

Your opponent picks a finite computable number, and gives you a function tells you if the number is smaller/larger/equal by computing it. He also gives you a representation for the output (in a sane world this would be "you can only print numbers like 99999", but he can make it more complicated; it actually doesn't matter). Proceed to measure the size of this function in bits.

Now, answer with your own function, which is twice the size of his function (in bits), and prints out the largest number it can while keeping the code to less than 2N bits in length. (You use the same representation he chose: In a world where you can only print out numbers like "99999", that's what you do. If you can define functions, it gets slightly more complicated.)

ninjagecko
  • 88,546
  • 24
  • 137
  • 145
0

I do not understand the purpose here, but I this is what I thought of:

Reading your comments, I suppose you aren't looking for infinitely large number, but a "super large number" instead. And whatever be the number, it will have a large no. of digits. How you got them, isn't the concern. Keeping this in mind:

No complex computation is required. Just type random keys on your numeric keyboard to have a super large number, and then have a program randomly add/remove/modify digits of that number. You get a list of very large numbers - select any one out of them.

e.g: 3672036025039629036790672927305060260103610831569252706723680972067397267209 and keep modifying/adding digits to get more numbers

PS: If you state the purpose in your question clearly, we might be able to give better answers.

Rushil Paul
  • 2,010
  • 4
  • 26
  • 39