It Goes On And On

What will the process output be when we pass the input as ( 79 , 23 ) (79, 23) ?

Details and Assumptions

  • If you think this process will not return anything, submit 1 -1 .


The answer is 1.

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.

2 solutions

In mathematical (specifically, number theoretic) terms, this algorithm is precisely the Euclidean algorithm but using subtraction instead of division. Regardless, one inputs two numbers and the Euclidean algorithm will spit out the greatest common divisor (GCD) between two numbers.

Now, I could go through all the steps of the algorithm specifically, but I will cut to the chase: the numbers 79 and 23 are prime, and thus its GCD must be 1 (since 1 is the only common factor between two primes). I'll leave it for you to come up with your own conclusion from here.

Made the Same Thing.

Daniel Heiß - 4 years, 8 months ago

I know this is Euclidean algorithm, but I have one confusion. In the last step A = 1 and B = 1, Since A not lest then B (they are equal), then Swap function will be called and this process will continue infinite of times. Then how the output is 1? Would you please explain it.

Al Maruf Sharif - 4 years, 8 months ago

Log in to reply

There is another step after the one you have called the last step. When A=1 and B=1, A<B is false (as one isn't less than one), then A is subtracted from B, leaving: A=1 and B=0. This then meets the condition of B=0.

Adam Blakey - 4 years, 8 months ago

Log in to reply

Oh yes! That's my mistake. Thanks :)

Al Maruf Sharif - 4 years, 8 months ago
Arjen Vreugdenhil
Sep 23, 2016

Output = gcd(a, b).

Proof: In every step, a a and b b change but their greatest common divisor remains the same:

gcd ( a , b ) = gcd ( a , b a ) \gcd(a, b) = \gcd(a, b-a) for b a b \geq a , and gcd ( a , b ) = gcd ( b , a b ) \gcd(a, b) = \gcd(b, a-b) for b < a b < a .

In each step, the sum a + b a + b becomes smaller but remains positive. With the principle of finite descent (a non-negative integer cannot be decreased indefinitely), this algorithm will reach an endpoint. The answer is finally given based on gcd ( a , 0 ) = a \gcd(a, 0) = a .

Very poor wording. When you substract A from B you did not define it as the new B may be we call it C and the process does not end

Ilan Amity - 4 years, 8 months ago

Log in to reply

A more precise proof, by induction: Let f ( a , b ) f(a,b) be the output for any given integers a 1 a \geq 1 and b 0 b \geq 0 .

Basis step . If a + b = 1 a + b = 1 , then clearly the algorithm produces f ( 1 , 0 ) = 1 = gcd ( 1 , 0 ) f(1,0) = 1 = \gcd(1,0) .

Induction step . Suppose that for all situations with a + b < n a + b < n we have that f ( a , b ) = gcd ( a , b ) f(a,b) = \gcd(a,b) ; here n 2 n \geq 2 . We will prove that this is then also true for a + b = n a + b = n .

So let the input be such that a + b = n a + b = n . There are three cases to consider.

  • a = n a = n and b = 0 b = 0 . The algorithm immediately gives f ( n , 0 ) = n = gcd ( n , 0 ) f(n,0) = n = \gcd(n,0) .

  • a < b a < b . The algorithm prescribes f ( a , b ) = f ( a , b a ) f(a,b) = f(a, b-a) . Since a + ( b a ) = b < n a + (b-a) = b < n (we assumed a 1 a \geq 1 ), we use the induction step to conclude f ( a , b ) = f ( a , b a ) = gcd ( a , b a ) = gcd ( a , b ) , f(a,b) = f(a,b-a) = \gcd(a,b-a) = \gcd(a,b), the last equality being a key property of the gcd function.

  • a b a \geq b . Now we must evaluate f ( a , b ) = f ( b , a b ) f(a, b) = f(b, a-b) . Similar to the previous step, b + ( a b ) = a < n b + (a-b) = a < n (we ruled out b > 0 b > 0 ), and using the induction assumption we get f ( a , b ) = f ( b , a b ) = gcd ( b , a b ) = gcd ( a , b ) . f(a,b) = f(b,a-b) = \gcd(b,a-b) = \gcd(a,b).

Conclusion . Thus we conclude that f ( a , b ) = gcd ( a , b ) f(a,b) = \gcd(a,b) for all values of n = a + b n = a + b , which means that it is true for all pairs ( a , b ) (a,b) .

Let z ( a , b ) z(a,b) be the number of steps needed to calculate f ( a , b ) f(a,b) . It is easy to prove by induction that z ( a , b ) a + b + 1 z(a,b) \geq a+b+1 using induction on n = a + b n = a+b .

Arjen Vreugdenhil - 4 years, 8 months ago

In my original solution I swapped a a and b b . It should be better now. I also gave a more precise proof of the finitude of the process.

Arjen Vreugdenhil - 4 years, 8 months ago

Your solution is wrong. The two prime numbers are odd. Their difference is even. The last pair of numbers before B becomes zero is (2,2). The solution is 2.

David Forbus - 4 years, 8 months ago

Log in to reply

Perhaps this helps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
(79,23)
(23,56)
(23,33)
(23,10)
(10,13)
(10,3)
(3,7)
(3,4)
(3,1)
(1,2)
(1,1)
(1,0)
Output: 1

This was produced with the following Java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
int a = 79, b = 23;

while (true) {
    System.out.print('(');
    System.out.print(a);
    System.out.print(',');
    System.out.print(b);
    System.out.println(')');

    if (b == 0) {
        System.out.print("Output: ");
        System.out.println(a); break;
    }
    else {
        if (a >= b) { int t = a; a = b; b = t; }
        b -= a;
    }
}

Arjen Vreugdenhil - 4 years, 8 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...