You are on page 1of 4

Khandesh College Education Society’s

College of Engineering & Information Technology, Jalgaon


Department of Computer Engineering
Roll No: 10 PRN: 21510620171124510010 Date of Exp:
Class: TE Comp. Sign of Staff Member:
==================================================================================

PRACTICAL NO. 1

Aim : The Euclid Problem


Theory :
Introduction
From Euclid it is known that for any positive integers A and B there exist such integers X and
Y that AX + BY = D, where D is the greatest common divisor of A and B. The problem is to find
for given A and B corresponding X, Y and D.
Explanation:
A*x+B*y=gcd(A,B) .............................................(1)
Extended Euclidean Algorithm finds x and y for us.
=> B*x1 + (A%B)*y1 = gcd(A,B)
[This follows from the Euclidean Algorithm. Remember the recurrence
GCD(A,B)=GCD(B,A%B)]
=> B*x1 + (A-[A/B]*B)*y1 = gcd(A,B)
{[A/B] represents floor(A/B)}
Expanding,
B*x1 + A*y1 - [A/B]*B*y1 = gcd(A,B)
Taking B common,
B(x1-[A/B]*B) + A*y1 = gcd(A,B)
=> A*y1 + B*(x1-[A/B]*B) = gcd(A,B)......................(2)
Comparing 1 and 2,
x=y1 and y=x1-[A/B]*B
This shows that if y1 and x1 can be calculated we can calculate x and y.
Input
The input will consist of a set of lines with the integer numbers A and B, separated with space
(A;B < 1000000001).
Output
For each input line the output line should consist of three integers X, Y and D, separated with
space.
If there are several such X and Y , you should output that pair for which jXj + jY j is the minimal.
If there are several X and Y satisfying the minimal criteria, output the pair for which X ≤ Y .
Sample Input
4 6
17 17
Sample Output
-1 1 2
0 1 17

Conclusion:
In this Practical, I studied analyzing a property of an algorithm whose classification is not
known for all possible inputs using The Euclid Problem
Program:
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a , b;
cout<<"Enter the values of a and b: "<<endl;
cin>>a>>b;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
return 0;
}
Output :

You might also like