What will the process output be when we pass the input as ( 7 9 , 2 3 ) ?
Details and Assumptions
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.
Made the Same Thing.
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.
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.
Output = gcd(a, b).
Proof: In every step, a and b change but their greatest common divisor remains the same:
g cd ( a , b ) = g cd ( a , b − a ) for b ≥ a , and g cd ( a , b ) = g cd ( b , a − b ) for b < a .
In each step, the sum 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 g cd ( 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
Log in to reply
A more precise proof, by induction: Let f ( a , b ) be the output for any given integers a ≥ 1 and b ≥ 0 .
Basis step . If a + b = 1 , then clearly the algorithm produces f ( 1 , 0 ) = 1 = g cd ( 1 , 0 ) .
Induction step . Suppose that for all situations with a + b < n we have that f ( a , b ) = g cd ( a , b ) ; here n ≥ 2 . We will prove that this is then also true for a + b = n .
So let the input be such that a + b = n . There are three cases to consider.
a = n and b = 0 . The algorithm immediately gives f ( n , 0 ) = n = g cd ( n , 0 ) .
a < b . The algorithm prescribes f ( a , b ) = f ( a , b − a ) . Since a + ( b − a ) = b < n (we assumed a ≥ 1 ), we use the induction step to conclude f ( a , b ) = f ( a , b − a ) = g cd ( a , b − a ) = g cd ( a , b ) , the last equality being a key property of the gcd function.
a ≥ b . Now we must evaluate f ( a , b ) = f ( b , a − b ) . Similar to the previous step, b + ( a − b ) = a < n (we ruled out b > 0 ), and using the induction assumption we get f ( a , b ) = f ( b , a − b ) = g cd ( b , a − b ) = g cd ( a , b ) .
Conclusion . Thus we conclude that f ( a , b ) = g cd ( a , b ) for all values of n = a + b , which means that it is true for all pairs ( a , b ) .
Let z ( a , b ) be the number of steps needed to calculate f ( a , b ) . It is easy to prove by induction that z ( a , b ) ≥ a + b + 1 using induction on n = a + b .
In my original solution I swapped a and b . It should be better now. I also gave a more precise proof of the finitude of the process.
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.
Log in to reply
Perhaps this helps:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
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 |
|
Problem Loading...
Note Loading...
Set Loading...
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.