Question

1) Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8...

1) Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8 898 and 14 321.

2) Program the Euclidean algorithm in 1) by using C++ programming language.

3) What is the greatest common divisor of 8 898 and 14 321?

4) Next, extend the Euclidean algorithm (write in pseudocode) to express gcd(8 898; 14 321) as a linear combination of 8 898 and 14 321.

5) Continue the programming in 2) to program the Extended Euclidean algorithm in 4).

6) What are the values of the linear combination of 8 898 and 14 321?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Euclidean Algorithm

Euclidean Algorithm is used to find the Greatest Common Divisor (GCD) of 2 numbers. Greatest Common Divisor is the largest digit or number that divides both the number without leaving any remainder (Perfect division).

Rules

Let a, b be two numbers.

1. If either one of a or b is 0 then GCD(a,b)=The number which is zero

For example: if a=0,b=3 then GCD(0,3)=3 because greatest common divisor is 3 only. Similarly GCD(3,0)=3

2. To find GCD(a,b)

  • Find a%b or find the remainder when a divided by b, let remainder be c
  • Find b%c or find the remainder when b divided by c, let remainder be d
  • Find c%d
  • Continue this step until the remainder becomes zero
  • The last divisor which gave us the remainder zero will be the GCD of the 2 numbers

Example

Extended Euclidean Algorithm

Extended Euclidean algorithm is used to compute x and y if

ax+by=gcd(a,b)

a and b will be provided to us. We have to reverse the steps that we have done in the euclidean algorithm. That is here we will start from GCD and we do all the steps in the Euclidean algorithm backward.

eg: GCD = 1

321 = 14*22 + 13
14 = 13*1 + 1
13 = 1*13 + 0

1 = 23*14 + -1*321

1.  Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8 898 and 14 321.

Pseudocode Of Program

  • 8 & 898

a=8,b=898

if (a<b) then

swap(a,b)\

while b!=0

temp=a%b

a=b

b=r

end while

print a

  • 14 & 321

a=14,b=321

if (a<b) then

swap(a,b)\

while b!=0

temp=a%b

a=b

b=r

end while

print a

Pseudocode Of Problem

  • 8 & 898

8%898=8

898%8=2

8%2=0

GCD =2

  • 14 & 321

14 % 321 = 14

321 % 14 = 13

14 % 13 = 1

13 % 1 = 0

GCD = 1

2) Program the Euclidean algorithm in 1) by using C++ programming language.

#include <iostream> //Header file for input output
using namespace std; 
int gcd(int a, int b) { //function definition 
   if (b == 0) //if b is zero than return a
   return a;
   return gcd(b, a % b); //call the function recursively
}
int main() {
   int a , b; // declare 2 variable a & b
   cout<<"Enter the values of a and b: "<<endl;  //print to the user to enter a & b
   cin>>a>>b; //get value from the user
   cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); //print gcd value by calling function //gcd(a,b) a and b are the values passed to it
   return 0;
}

OUTPUT

3) What is the greatest common divisor of 8 ,898 and 14, 321?

  • 8 & 898

8%898=8

898%8=2

8%2=0

GCD =2

  • 14 & 321

14 % 321 = 14

321 % 14 = 13

14 % 13 = 1

13 % 1 = 0

GCD = 1

4) Next, extend the Euclidean algorithm (write in pseudocode) to express gcd(8 898; 14 321) as a linear combination of 8 898 and 14 321.

Pseudocode Euclidean algorithm (8 898)

ExtendedEuclidean (a,b) { //definiton of function that accepts 2 parameters a & b

// outputs a triple (p,q,r) where p = gcd(first num,second num) and // p == first num*q + second num*r

if (b == 0) return (a,1,0) ;

(p_1, q_1, r1) = ExtendedEuclidean (8,8%818) ; // a and b will be passed back from this function

p = p_1 ;

q = r_1 ;

r = q_1 - (a div b) * r_1 ; // note: div = integer division return (p,q,r) ;

}

Pseudocode Euclidean algorithm (14 321)

ExtendedEuclidean (a,b) { //definiton of function that accepts 2 parameters a & b

// outputs a triple (p,q,r) where p = gcd(first num,second num) and // p == first num*q + second num*r

if (b == 0) return (a,1,0) ;

(p_1, q_1, r1) = ExtendedEuclidean (14,14%321) ; // a and b will be passed back from this function

p = p_1 ;

q = r_1 ;

r = q_1 - (a div b) * r_1 ; // note: div = integer division return (p,q,r) ;

}

Pseudocode Of Problem

  • 8 & 898

GCD=2

898 = 8*112 + 2
8 = 2*4 + 0

Linear combination :  2 = -112*8 + 1*898

Pseudocode Of Problem

  • 14 & 321

GCD = 1

321 = 14*22 + 13
14 = 13*1 + 1
13 = 1*13 + 0

1 = 23*14 + -1*321

5) Continue the programming in 2) to program the Extended Euclidean algorithm in 4).

#include <bits/stdc++.h> // header file
using namespace std;

int main() { //main function
int x, y;
int a , b ;
cout<<"Enter the values of a and b: "<<endl; //print to the user to enter a & b
cin>>a>>b; //get value from the user

cout<<"gcd "<<Extended_gcd(a, b, &x, &y); //print the output from calling function
return 0;
}

int Extended_gcd(int a, int b, int *x, int *y) { //function definition
if (a == 0) { //checking if a is zero
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = Extended_gcd(b%a, a, &x1, &y1); //recursive call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd; //return the calculated gcd back
}

OUTPUT

6) What are the values of the linear combination of 8 898 and 14 321?

Problem

  • 8 & 898

GCD=2

898 = 8*112 + 2
8 = 2*4 + 0

Linear combination :  2 = -112*8 + 1*898

Problem

  • 14 & 321

GCD = 1

321 = 14*22 + 13
14 = 13*1 + 1
13 = 1*13 + 0

1 = 23*14 + -1*321

Add a comment
Know the answer?
Add Answer to:
1) Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 20 points Problem 4: Extended Euclidean Algorithm Using Extended Euclidean Algorithm compute the greatest common divisor...

    20 points Problem 4: Extended Euclidean Algorithm Using Extended Euclidean Algorithm compute the greatest common divisor and Bézout's coefficients for the pairs of integer numbers a and b below. Express the greatest common divisor as a linear combination with integer coefficients) of a and b. (Do not use factorizations or inspection. Please demonstrate all steps of the Extended Euclidean Algo- rithm.) (a) a 270 and b = 219 (b) a 869 and b 605 (c) a 4930 and b-1292 (d)...

  • a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm....

    a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm. gcd- By working back in the Euclidean Algorithm, express the gcd in the form 322m196n where m and n are integers b) c) Decide which of the following equations have integer solutions. (i) 322z +196y 42 ii) 322z +196y-57

  • Write down the Euclidean algorithm then use the algorithm to find the greatest common divisor of...

    Write down the Euclidean algorithm then use the algorithm to find the greatest common divisor of the following pairs of numbers. 315, 825 2091, 4807

  • Cryptography Computer Security Greatest Common Divisor Assignment Instructions In software, implement the Euclidean algorithm to find...

    Cryptography Computer Security Greatest Common Divisor Assignment Instructions In software, implement the Euclidean algorithm to find the greatest common divisor of any two positive integers. It should implement the pseudocode provided in the text. It should allow the user to enter two integers.   Your program should output the intermediate values of q, r1, r2 for each step and should return the greatest common divisor. Challenge component: Allow the user's input to be zero as well as the positive integers. Provide...

  • can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of...

    can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of two positive integers p and q can be given by the Euclid's algorithm explained in the lecture will be determined. · Write a function gcdIterative that uses the largest common divisor of p and q Calculates loop structure and returns. Use the pseudocode given in the lecture as a starting point and implement it as directly as possible into a C ++ program. Use...

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • Apply Euclid’s algorithm to find the GCD (Greatest Common Divisor) of 126 and 28. Describe or...

    Apply Euclid’s algorithm to find the GCD (Greatest Common Divisor) of 126 and 28. Describe or give the pseudocode of the consecutive integer checking algorithm for finding the GCD. What is the time complexity of this second algorithm? Explain.

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest...

    (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x, y) is x; otherwise gcd(x, y) is gcd(y, x % y), where % is the remainder operator. c programming need the whole...

  • We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and...

    We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and v. We want to extend and compute the gcd of n integers gcd⁡(u1,u2,….un). One way to do it is to assume all numbers are non-negative, so if only one of if uj≠0 it is the gcd. Otherwise replace uk by uk mod uj  for all k≠j where uj is the minimum of the non-zero elements (u’s). The algorithm can be made significantly faster if one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT