Modified Euclidean Algorithm

The following procedure is a modified Euclidean algorithm. For a pair ( n , m ) (n,m) of positive integers with n < m n<m we construct a sequence of pairs of positive integers ( n 0 , m 0 ) , ( n 1 , m 1 ) , . . . (n_0,m_0), \ (n_1,m_1),\ ... such that ( n 0 , m 0 ) = ( n , m ) (n_0,m_0)=(n,m) and for all i 0 i\geq 0 m i + 1 = n i ; n i + 1 = min ( r i , n i r i ) , m_{i+1}=n_i;\ n_{i+1}=\min (r_i,n_i-r_i), where r i r_i is the remainder of m i m_i divided by n i n_i . We repeat this procedure until we get a pair ( n k , m k ) (n_k,m_k) with n k m k n_k \mid m_k , so that r k = 0 r_k=0 and the algorithm terminates.

Find the number of pairs ( 101 , m ) (101,m) with 101 < m < 1000 101<m<1000 for which this algorithm terminates in no more than 4 4 steps with n k = 1 n_k=1 (that is, n k = 1 n_k=1 for k 4 k\leq 4 ).

Details and assumptions

Example: If ( n , m ) = ( 101 , 215 ) (n,m)=(101,215) , then ( n 0 , m 0 ) = ( 101 , 215 ) , (n_0,m_0)=(101,215), ( n 1 , m 1 ) = ( 13 , 101 ) , (n_1,m_1) =(13,101), ( n 2 , m 2 ) = ( 3 , 13 ) , (n_2,m_2)=(3,13), ( n 3 , m 3 ) = ( 1 , 3 ) (n_3,m_3) = (1,3) , so for the pair ( 101 , 225 ) (101,225) the algorithm terminates with n k = 1 n_k=1 in 3 3 steps.


The answer is 854.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Daren Khu
May 20, 2014

If g c d ( n i , m i ) = n i gcd(n_i,m_i) = n_i the process is immediately terminated. Now we attempt to prove that g c d ( n i , m i ) = g c d ( n i + 1 , m i + 1 ) gcd(n_i,m_i) = gcd(n_{i+1},m_{i+1}) if g c d ( n i , m i ) n i gcd(n_i,m_i) \neq n_i . Let g c d ( n i , m i ) = d i gcd(n_i,m_i) = d_i and m i = q i n i + r i m_i = q_in_i+r_i where r i 0 r_i \neq 0 . Obviously d i m i + 1 = n i d_i | m_{i+1} = n_i . Also, r i = m i q i n i r_i = m_i - q_in_i and thus is divisible by d i d_i as well. Then, we have d i n i r i d_i | n_i - r_i so d i n i + 1 d_i | n_{i+1} . Thus we conclude that g c d ( n i + 1 , m i + 1 ) d i = g c d ( n i , m i ) gcd(n_{i+1},m_{i+1}) \geq d_i = gcd(n_i,m_i) . Conversely, we let g c d ( n i + 1 , m i + 1 ) = d i + 1 gcd(n_{i+1},m_{i+1}) = d_{i+1} . n i + 1 = r i n_{i+1} = r_i or n i r i n_i - r_i . We note that if d i + 1 m i + 1 = n i d_{i+1} | m_{i+1} = n_i and d i + 1 r i d_{i+1} | r_i then d i + 1 n i r i d_{i+1} | n_i - r_i . Also if d i + 1 m i + 1 = n i d_{i+1} | m_{i+1} = n_i and d i + 1 n i r i d_{i+1} | n_i - r_i then d i + 1 r i d_{i+1} | r_i . Thus in either cases, d i + 1 r i , n i r i d_{i+1} | r_i, n_i-r_i . Thus we also have d i + 1 m i = q i n i + r i d_{i+1} | m_i = q_in_i+r_i . Therefore we can conclude g c d ( n i , m i ) d i + 1 = g c d ( n i + 1 , m i + 1 ) gcd(n_i, m_i) \geq d_{i+1} = gcd(n_{i+1},m_{i+1}) . Hence, g c d ( n i , m i ) = g c d ( n i + 1 , m i + 1 ) gcd(n_i,m_i) = gcd(n_{i+1},m_{i+1}) .

Since 101 is prime, aside from m 0 = 101 d m_0 = 101d (where d = 2,3,.., 9), the rest of 101 < m 0 < 1000 101 < m_0 < 1000 can be reduced to the form ( 1 , m k ) (1, m_k) in k steps. From this point on, we only consider ( n i , m i ) (n_i,m_i) where g c d ( n i , m i ) = 1 gcd(n_i, m_i) = 1 .

From ( n i , m i ) (n_i,m_i) , we now attempt to create the smallest m i 1 m_{i-1} to form the smallest ( n i 1 , m i 1 ) (n_{i-1},m_{i-1}) (we define this as the smallest n i 1 + m i 1 n_{i-1} + m_{i-1} ) since n i 1 = m i n_{i-1} = m_i is fixed. We know that n i 1 1 2 m i 1 n_{i-1} \leq \frac{1}{2}m_{i-1} since n i 1 n_{i-1} takes the smaller of r i 2 r_{i-2} and n i 2 r i 2 = m i 1 r i 2 n_{i-2} - r_{i-2} = m_{i-1} - r_{i-2} . Hence m i 1 = 2 n i 1 + j m_{i-1} = 2n_{i-1} + j for some integer j. To create ( n i , m i ) (n_i,m_i) from ( n i 1 , 2 n i 1 + j ) (n_{i-1}, 2n_{i-1} + j) , j only takes values in the form of a n i an_i or b n i 1 n i bn_{i-1} - n_i (for positive integers a,b). Obviously the smallest j is then n i n_i (since n i m i n i = n i 1 n i n_i \leq m_i - n_i = n_{i-1} - n_i ). Smallest ( n i 1 , m i 1 ) (n_{i-1}, m_{i-1}) is thus ( m i , 2 m i + n i ) (m_i, 2m_i + n_i) .

Now we let the sequence be ( n 0 , m 0 ) , ( n 1 , m 1 ) , . . . , ( n k , m k ) (n_0,m_0), (n_1,m_1),...,(n_k,m_k) . The smallest possible ( n k , m k ) = ( 1 , 2 ) (n_k,m_k) = (1,2) , and using our earlier proof, we can construct the smallest ( n k 1 , m k 1 ) = ( 2 , 5 ) (n_{k-1},m_{k-1}) = (2,5) , ( n k 2 , m k 2 ) = ( 5 , 12 ) (n_{k-2},m_{k-2}) = (5,12) , ( n k 3 , m k 3 ) = ( 12 , 29 ) (n_{k-3},m_{k-3}) = (12,29) , ( n k 4 , m k 4 ) = ( 29 , 70 ) (n_{k-4},m_{k-4}) = (29,70) , ( n k 5 , m k 5 ) = ( 70 , 169 ) (n_{k-5},m_{k-5}) = (70,169) , ( n k 6 , m k 6 ) = ( 169 , 408 ) (n_{k-6},m_{k-6}) = (169,408) . From this we see that ( 101 , m 0 ) (101,m_0) takes at most 5 steps before it reaches ( 1 , m k ) (1,m_k) . We now consider all such ( 101 , m 0 ) (101,m_0) that requires 5 steps to achieve 5 steps to achieve ( 1 , m 5 ) (1,m_5) .

We know that when ( n 0 , m 0 ) = ( 101 , 101 d ± g ) (n_0, m_0) = (101, 101d \pm g) then ( n 1 , m 1 ) = ( g , 101 ) (n_1,m_1) = (g, 101) where g = 1,2..,50 and d=1,2..,10. Every m 0 m_0 (that is not divisible by 101) can be uniquely expressed as 101 d ± g 101d \pm g for some d and g, so we only need to consider ( g , 101 ) (g,101) that requires 4 steps to reach ( 1 , n 5 ) (1,n_5) . From our earlier construct, we know that 29 g 50 29 \leq g \leq 50 .

Consider reducing the 22 cases of ( g , 101 ) (g,101) to ( n 2 , g ) (n_2,g) - we only have 8 cases where n 2 12 n_2 \geq 12 . They are (14,29), (13,38), (16,39), (19,40), (19,41), (17,42), (15,43), (13, 44). We further reduce them to ( n 3 , m 3 ) (n_3, m_3) and consider n 3 5 n_3 \geq 5 - they are (7,16), (8,17) and (5, 13). Considering ( n 4 , m 4 ) (n_4,m_4) where n 4 1 n_4 \neq 1 gives us the only 2 possibilities (2,7) and (2,5). Tracing them back to ( n 1 , m 1 ) (n_1, m_1) gives us ( 39 , 101 ) (39,101) and ( 44 , 101 ) (44,101) .

For g = 39 and 44, we can produce 36 of ( n 0 , m 0 ) = ( 101 , 101 d ± g ) (n_0, m_0) = (101, 101d \pm g) where 101 < m 0 < 1000 101 < m_0 < 1000 , that requires 5 steps to reach ( 1 , n 5 ) (1, n_5) . Also we have 8 of m 0 = 101 d m_0 = 101d (d=2,3,..9) that will result in the process being terminated immediately. Subtracting these 44 cases, we have 854 cases of m 0 m_0 that will cause the algorithm to terminate in no more than 4 steps with n k = 1 n_k = 1 .

Eric Zelikman
May 20, 2014

First, there are a total of 898 numbers, from 999 to 102, +1 as it's an inclusive set. Clearly, the solution was not any multiple of 101, as that would immediately have yielded 0 as the result of m%n, ending the function immediately. This removes 8 from the solution set immediately.

In addition, all solutions from 101 to 201 would occur 9 times, with a difference of 1 every time, because (x*101+y)%101 would simply be y for all integers x and y. In other words, the total number of exceptions from 101 to 201 could simply be multiplied by 9 and added to the previously mentioned 8 exceptions for the total number of exceptions.

So, the challenge now becomes calculating the exceptions from 102 to 201, or with %101, from 1 to 100. Now, as the values approach the halfway point, the modded value subtracted from the original value will begin to exceed the modded value alone, or the min() function will return a different result. What this means is that there is a "mirror" in this group at the center of the first 101 values, at the center or 51st.

All of this should be quickly determined. The remaining values are 1 to 51. These must be the n 2 's for all of the functions. So, where s is the total number of values from 1 to 51 that satisfy the role as the n 2, there are 898-(18*s+8) solutions.

Now, min(101%x,x-101%x). The final numbers will be notably smaller than the original numbers. The maximal value for the final n value after 4 steps as the new value must be less than half of the original value, using 51 as an upper bound, would be 51\rightarrow 25\rightarrow 12\rightarrow 5. The pattern in m is similar, with 101\rightarrow 51\rightarrow 25\rightarrow 12. This follows theoretical maxima. However, the problem that makes all of these unrealistic is that the greater the value of one level past a certain point, the lesser the resulting value. As with the original Euclidean algorithm, this results in all numbers eventually reaching relative primehood. Conveniently, for values so small like 51, all fourth steps must be not only relatively prime, but also prime. Another good thing, is that by virtue of the path taken by this algorithm, third steps with such small values should also be relatively prime. By this, we can determine that the final n, n 4 , must be 2. As the number n i can never be more than half of the m_i, 2:3 is impossible.

The first value must be greater than 1/4 of 101, by extending the previous 2x rule, and 25-51 is already an awfully small range. In fact, for all of the levels, the rule extends. So, the minimum value for 51 is 12, for 12 is 3. This reestablishes new maxima with all numbers past 44 being excluded by the minimum of 12. All of this reducing should take no more than 10 minutes.

One final reduction, recognizing that values up to about 1/3 for the first are unacceptable, as they will result in numbers that either have a difference equal to a number which would result in a small mod or a difference that would be small already, where the mod would act to reduce it. This approximation is by no means generalized, but serves specifically for this scenario as exceptions are only realistic with far greater original n's. At this point, brute force for numbers between 33 and 44 is simple and looking for the values that will end in 2 will result in two, 39 and 44. 2*18+8=44. 898-44=854 total solutions.

The cases are not worked out clearly and openly, but the solution was most probably found.

Calvin Lin Staff - 7 years ago
Michael Tong
Nov 5, 2013

Not the most elegant solution, but I solved it using congruences and modular math. First, any integer k k with the same remainder ( m o d 101 ) \pmod {101} will take the same number of steps. In addition, since it takes the minimum of r i , n i r i r_i, n_i - r_i , we can work with k 50 k \leq 50 . The first 4 steps of the algorithm gives:

( 101 , k ) (101, k) \rightarrow

( k , 101 ) (k, 101) \rightarrow

( 101 ( m o d k ) , k ) (101 \pmod k, k) \rightarrow

( k ( m o d 101 ( m o d k ) ) , 101 ( m o d k ) ) (k \pmod {101 \pmod k}, 101 \pmod k) \rightarrow

( ( 101 ( m o d k ) ) ( m o d k ( m o d 101 ( m o d k ) ) ) , k ( m o d 101 ( m o d k ) ) ) ((101 \pmod k) \pmod {k \pmod {101 \pmod k}}, k \pmod {101 \pmod k})

And then just solve each congruence on the left for 1 1 . The first one is just 1 1 , the second one is the divisors of 100 100 less than 50 50 , and from then on it's essentially just brute force. For ease of calculation, solve the simplest congruences first for k 50 k \leq 50 and then don't bother to try k k which has already been proven to work for another congruence.

Next, once you find all k k that works, then find all integers 101 < m < 1000 101 < m < 1000 such that m ± k ( m o d 101 ) m \equiv \pm k \pmod {101} . There are 854 854 such integers.

There is probably no conceptual way to do solve this problem, some case-by-case analysis cannot be avoided.

It is amusing, how fast this algorithm actually is.

Alexander Borisov - 7 years, 7 months ago

Log in to reply

Yea, especially since you find so many in the first 2 congruences that you only end up trying 10 or so for the rest.

Michael Tong - 7 years, 7 months ago
Sanjay Banerji
Nov 4, 2013

Frankly speaking I have no idea how to do this manually ; so I did this with a program in Java ::

import java.io.*;

import java.math.*;

public class ModifiedEuclideanAlgorithm

{

public static void main(String[] args)

{

    int i=0,c=0;

    ModifiedEuclideanAlgorithm obj=new ModifiedEuclideanAlgorithm();

    for(i=102;i<1000;i++)

    {

        if(obj.fun(101,i,0)==1)

            c++;

    }

    System.out.println(" "+c);

}

int fun(int x,int y, int g)

{

    int m=0,h=0;    

    h=y%x;

    if(h==0)

    {

        if((x==1)&&(g<5))

            return 1;

        else return 0;

    }

    m=Math.min(h,x-h);

    return fun(m,x,g+1);

}

}

OUTPUT :: 854

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...