0% found this document useful (0 votes)
60 views4 pages

Practical No. 1 Aim: The Euclid Problem Theory

The document describes solving the Euclid problem to find integers x, y, and the greatest common divisor (gcd) of integers A and B using the extended Euclidean algorithm. It provides the theory behind the algorithm, showing that if x1 and y1 can be found from the algorithm, then x=y1 and y=x1-(A/B)*B. The program implements the extended Euclidean algorithm to find x, y, and gcd for a given input of A and B.

Uploaded by

Kuntal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

Practical No. 1 Aim: The Euclid Problem Theory

The document describes solving the Euclid problem to find integers x, y, and the greatest common divisor (gcd) of integers A and B using the extended Euclidean algorithm. It provides the theory behind the algorithm, showing that if x1 and y1 can be found from the algorithm, then x=y1 and y=x1-(A/B)*B. The program implements the extended Euclidean algorithm to find x, y, and gcd for a given input of A and B.

Uploaded by

Kuntal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Khandesh College Education Society’s

College of Engineering & Information Technology, Jalgaon


Department of Computer Engineering
Roll No: 33 PRN: 21510620171124511002 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 <stdio.h>
int gcd(int a, int b)
{
int tmp, flag = 0;
int x1 = 1, y1 = 0, x2 = 0, y2 = 1;
while(a%b)
{
if(flag)
{
x2 -= a/b*x1;
y2 -= a/b*y1;
}
else{
x1 -= a/b*x2;
y1 -= a/b*y2;
}
tmp = a, a = b, b = tmp%b;
flag ^= 1;
}
if(flag)
printf("%d %d", x1, y1);
else
printf("%d %d", x2, y2);
printf(" %d\n", b);
return b;
}
int main(){
int A, B;
while(scanf("%d %d", &A, &B) == 2) {
gcd(A, B);
}
return 0;
}
Output :

You might also like