Can you transform a pair of numbers to equal each other based on given rules?

386 Views Asked by At

Given two different positive integers, $a$ and $b$, can they ever equal each other given the following rules for transforming them (written in python-esque pseudocode):

if a < b:
  a = 2a
  b = b-a
elif a > b:
  b = 2b
  a = a-b
else: 
  return True

Example of a pair that will equal:

11,21
22,10
12,20
24,8
16,16

And an example of a pair that will never equal each other:

1,4
2,3
4,1
3,2
1,4
repeating...

So my question is, can you tell that two numbers will equal without running through a function similar to the one above and looking for equality or an infinite loop?

I started approaching this by graphing all combinations of two numbers ($a$ on the y-axis and $b$ on the x-axis): the yellow points indicating pairs that will equal each other. See image here.This was computed with a modification of the above function. But it would be great not to have to do this because it quickly becomes intractable.

I can clearly see a pattern and now I'm trying to figure out the rules for computing it. Can anyone point me in the right direction? Does this form of problem have a name or is there a field of mathematics that would contain the answer?

2

There are 2 best solutions below

4
On

Write $a$ and $b$ in binary. If $\gcd(a,b)=1$ they become equal (and it returns True the next time) if $a+b$ is a power of $2$. If $\gcd(a,b) \gt 1$, they become equal if $\frac {a+b}{\gcd(a,b)}$ is a power of $2$. In other cases they will cycle.

Added: the chain starting with $11,21$, written in binary goes $$01011,10101\\ 11010,01010\\ 01100,10100\\ 11000,01000\\ 10000,10000\\ 10000,00000$$ The chain starting with $35,77$ goes $$010011,101101\\ 100110,101010\\ 11100,1010100\\ 111000,111000\\ 1110000,0000000$$

Note how the rightmost ones move left once per iteration.

To prove this, represent $a,b$ as $mp,mq$ with $p,q$ coprime and $p \lt q$. Then the next iteration is $2a, 2^n-2a$, so the sum is maintained. If $2^k$ is the highest power of $2$ that divides $a,b$, after one iteration $2^{k+1}$ divides both numbers. If $a,b$ are common multiples of an odd number, that is maintained by the iteration. The $\gcd$ doubles each iteration until it is $\frac 12(a+b)$. We can only get to $a=b$ if the sum is $2^n\gcd(a,b)$

1
On

Your (Python) code fragment seems to be the only definition of your function. But the output run you show does not seem to be from the given code. Possibly you are executing it in your head using the correct definition but I can only go by your code.

Starting with initial values a = 11, b = 21. First we double the smaller number and set "a" to be that value. So "a" becomes 22. Immediately b is decreased by "a" . So "b" should get the new value $21 -22 = -1$. Once one number is negative and another is positive, we will be caught in an infinite loop. (Negative number becomes more negative, and positive number becomes bigger and bigger).

Please specify function mathematically instead of through any code.