0
$\begingroup$

My brute force algorithm is as follows:

Given X and Y positive integers < 2^30-1 
while true
  If X == Y
    Terminate fail
  If Y > X
    swap X,Y
  If (X,Y) found in Q
    Terminate success
  Add (X,Y) to Q
  X -= Y
  Y += Y

My brute force algorithm works, but on large numbers it takes a loooong time to complete. I'm assuming there is some sort of algorithm for this but my math is not good enough to know what that algorithm is. In other words, I don't know what question to ask.

Just looking for a pointer to the algorithm, if it exists.

Some examples 1,2 Terminates true in a loop as sub 1 gets 1 and the add gets 2 so you have 1,2 again

$1,3$ terminates false as $1,3 \rightarrow 2,2$

$1,7$ terminates false as $1,7 \rightarrow 2, 6 \rightarrow 4,4$

I'm looking for a shortcut instead of doing the full brute force chasing down the potential chain.

  • 0
    I don't understand what you want, i.e., what your title means. Could you give an example?2017-01-24
  • 0
    Where do you have this problem from? Also, have you tried exploring a few small examples a bit more thoroughly, like all pairs with sum less than $10$ or $15$? Also, I see what your algorithm does, but if you write the problem statement explicitly (i.e. what "loop" means), that would make it easier for people to actually see what's going on, and as such, give you an intelligent answer.2017-01-24
  • 1
    are the integers positive?2017-01-24
  • 0
    What do you mean by numbers that form a loop? What is Q in your algorithm?2017-01-24
  • 0
    Loop as in the numbers appear previously (like 1,2) which stays at 1,2 and never changes2017-01-24

2 Answers 2

1

Let's instead play this game on the rational numbers $\frac{x}{x+y}$ and $\frac{y}{x+y}$. Key observation: the iteration consists of doubling the smallest of them, and then decreasing the largest one so that the sum is still $1$.

Under this observation, let's rewrite the game a bit: Given a rational number $q$ with $00.5$, and if it is, then replace it with $1-q$. Then, double it. You lose if you end up at $0.5$. Actually, I want to say that we lose if we end up at $1$, since that simplifies the next section. That is what would happen one iteration later anyways.

Now, what kind of rational numbers end up at $1$? Well, every time we double $q$, if the denominator was even, we can simplify by a factor of $2$, but if it was odd, then we can't. Also, the substitution to $1-q$ doesn't change neither the denominator nor whether the fraction can be simplified. So we keep dividing our denominator by $2$ until we eventually end up at an odd denominator. If said odd denominator is anything other than $1$ we will eventually win. Therefore the fractions $q$ that makes us lose are exactly the ones where the denominator, after simplifying, is a power of $2$.

Going back to our $x$ and $y$, this means that $x+y$ must be a power of $2$ if we are to ever lose. Well, not exactly, since if $(x,y)$ makes us lose, then so does $(kx,ky)$ for any positive integer $k$ (or, actually, any rational $k$ that makes $kx,ky$ positive integers). Therefore, the exact criterion is whether $\frac{x+y}{\gcd(x,y)}$ is a power of $2$.

  • 0
    It's so simple when you know the math. Thank you so much. Amazing that I was on the totally wrong path thinking that the key lay in the track that brute force algorithm left. I knew that x=y was there but didn't even come close to seeing the relationship to number game you created. Great solution. If i could give you more points I would. BTW took 5 minutes to code the solution.2017-01-24
  • 0
    @cryptoref Brute force, or at least trial-and-error, is still important. In fact, that's what led me to this solution. I first looked for what kind of pairs ended up with two equals. That is the pairs of the form $(3y, y)$. So I immediately knew that if the numbers are not equal, their sum must be divisible by $4$. Then I looked for what kind of pairs become $(3y, y)$, and that's $(7y, y)$ and $(5y, 3y)$, which means the sum must now be divisible by $8$. That's when I started looking for a power-of-two pattern, and also fractions to justify simplifications and keeping the total sum constant.2017-01-24
2

Two numbers always form a loop eventually.

Notice that $x+y$ remains constant, and the number of pairs $(c,d)$ of non-negative integers that add to $x+y$ is $x+y+1$.

Therefore the program will always terminate after at most $2^{32}-1$ loops.

  • 0
    Thanks for the first hint, that x&y remain constant. But i'm trying to figure out HOW it terminates and trying to keep from doing 2^32 attempts2017-01-24
  • 1
    @cryptoref: It's not very helpful to change your question like this after it's been answered.2017-01-24
  • 0
    Sorry for the confusion. With the return of True or False I thought it was clear that i was looking for that boolean result. That is my issue. Plus the constant hint is a big help as it does fix the max size necessary for the queue. I'm just trying to avoid having to loop through the entire chain and short circuit getting the True or False.2017-01-24
  • 0
    @cryptoref: I think I did you a disservice: I didn't appreciate that although your algorithm will always terminate, it can return success or fail. I will think about this some more!2017-01-24