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
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
ADVERTISEMENT