Question

In: Computer Science

the following is the euclid 2,300 year old algorithm for fining the greatest common divisorof two...

the following is the euclid 2,300 year old algorithm for fining the greatest common divisorof two positive integers I an J
step Operation
1. get two positive integers as input; call the larger value I and the smaller value J.
2. Divide I by J, and call rhe remainder R.
3. If R is no 0, then reset I to the value of J, reset J to the cslue of R, and go back to step 2
4. print out the answer , which the value of J.
5 stop.

go throw the algorithm using the input value 20 and 32 . After each step of the algorithm completed, give the value of I,J abd R. Determine the final output of the algorithm.

Solutions

Expert Solution

Euclid’s algorithm to find the greatest common divisor

Algorithm for finding the greatest common divisor of two positive integers “ I ” and “ J ”:

Step 1: Assign the larger integer input value as “I” and smaller integer input value as “J”.

Step 2: Divide the value of “I” with the value of “J” and set the remainder to “R”.

Step 3: If the value of “R” is not equal to zero, then assign the value of “J” to “I” and assign the value of “R” to “J” and go to step 2.

Step 4: Print the value of “J” as the greatest common divisor.

Step 5: Stop the process.

a) Analyze the above “Euclid’s algorithm” with the inputs 20 and 32 and trace the values of “ I ”, “ J ”, and “ R ” after completing each step.

Step 1: Assign the larger value 32 to “I” and smaller value 20 to “J”.

Value of “I”

32

Value of “J”

20

Value of “R”

Not assigned

Step 2:

-> Divide the value of “I” with the value of “J” and set the remainder to

“R”.

-> Dividing 32 by 20 gives the quotient as 1 and remainder “R” as 12.

Value of “I”

32

Value of “J”

20

Value of “R”

12

Step 3:

-> If the value of “R” is not equal to zero, then assign the value of “J” to “I” and assign the value of “R” to “J” and go to step 2.

-> The value of “R” is 12, which is not equal to 0. So, “I” becomes 20 and “J” becomes 12 and go to step 2.

Value of “I”

20

Value of “J”

12

Value of “R”

12

Moved to Step 2:

Step 2:

-> Divide the value of “I” with the value of “J” and set the remainder to “R”.

-> Dividing 20 by 12 gives the quotient as 1 and remainder “R” as 8.

Value of “I”

20

Value of “J”

12

Value of “R”

8

Step 3:

-> As the value of “R” is 8, which is not equal to 0. So, “I” becomes 12 and “J” becomes 8.

Value of “I”

12

Value of “J”

8

Value of “R”

8

Next iteration of step 2:

Step 2:

-> Divide the value of “I” with the value of “J” and set the remainder to “R”.

-> Dividing 12 by 8 gives the quotient as 1 and remainder “R” as 4.

Value of “I”

12

Value of “J”

8

Value of “R”

4

Step 3:

-> As the value of “R” is 4, which is not equal to 0. So, “I” becomes 8 and “J” becomes 4.

Value of “I”

8

Value of “J”

4

Value of “R”

4

Next iteration of step 2:

Step 2:

-> Divide the value of “I” with the value of “J” and set the remainder to “R”.

-> Dividing 8 by 4 gives the quotient as 2 and remainder “R” as 0.

-> As the value of “R” becomes 0, go to step 4.

Value of “I”

8

Value of “J”

4

Value of “R”

0

Step 4:

-> Print the answer.

-> Hence, the greatest common divisor of 32 and 20 is 4.

Step 5:

-> Stop the process.

Therefore, the “Euclid’s algorithm” determines the greatest common divisor among two positive integers 32 and 20 as .


Related Solutions

4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13,...
4. (a) Use the Euclidean algorithm to find the greatest common divisor of 21 and 13, and the greatest common divisor of 34 and 21. (b) It turns out that 21 and 13 is the smallest pair of numbers for which the Euclidean algorithm requires 6 steps (for every other pair a and b requiring 6 or more steps a > 21 and b > 13). Given this, what can you say about 34 and 21? (c) Can you guess...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
Write a C function gcd that returns the greatest common divisor of two integers. The greatest...
Write a C function gcd that returns the greatest common divisor of two integers. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers.
What are the tax consequences to Euclid from the following independent events? a. Euclid bought 500...
What are the tax consequences to Euclid from the following independent events? a. Euclid bought 500 shares of common stock five years ago for $50,000. This year, Euclid receives 20 shares of common stock as a nontaxable stock dividend. What is Euclid's basis per share after this event? b. Assume instead that Euclid received a nontaxable preferred stock dividend of 20 shares. The preferred stock has a fair market value of $5,000, and the common stock, on which the preferred...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf)...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers. The greatest common divisor of a and b is written as gcd (a, b), or sometimes simply as (a, b). For example, gcd (12, 18) = 6, gcd (−4, 14) = 2 and gcd (5, 0) = 5. Two numbers are called co-prime or relatively prime...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y):     5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, write gcd(xy). Write a recursive function, gcd, that takes two integers as parameters and returns the greatest common divisor of numbers. Also write a program to test your function. Write a recursive function, reverseDigits, that takes an integer as a parameter and returns the number with the digits reversed. Also write a program to test your application.
The greatest common divisor of two integers x and y is the largest positive integer that...
The greatest common divisor of two integers x and y is the largest positive integer that evenly divides both. For example, gcd(12,6)=6, gcd(21,14)=7, gcd(31,10)=1, gcd(55,25)=5 Recall that the gcd of two integers is gcd(a,0) = a gcd(a,b) = gcd(b, a%b) Create int gcd(int x, int y). Assume that the two integers are zero or positive. Write the code as a pure function. Put it in a file gcd.c. Of course, you will need to test it. The function Φ(n) counts...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and uses your function to...
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use...
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55). Activity 7. Write a Prolog program to find the last item in a list. give me the screenshot pls
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT