×

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume)

The GCD of two integers is the greatest number that divides them both. GCD may be calculated simply by factorizing both integers and multiplying common factors.

The expanded version additionally discovers a way to express GCD in terms of p and q, i.e., coefficients i and j, for which:

(p * i) + (q*j) = gcd(p,q)

It is important to remember that we can always discover such a depiction.

Extended Euclidean Algorithm (EEA) using the C language

In this part, we will use the letter "h" to represent the GCD of p and q. The modifications to the original method (algorithm) are straightforward. If we remember the procedure, we can observe that it concludes with p = h and q = 0. We may readily derive coefficients for these parameters, namely (h * 10) + (0 * 0) = h

We may continue to backtrack the repetitive calls, starting with these coefficients (i, j) = (10, 0)

All that remains is to determine how the coefficients i and j change throughout the transition from (p, q) to (q, p mod q).

Assume that we have the coefficients (i1, j1) for (q, p mod q):

(q * i­1 )­­+ ((p mod q) * j­1 ) = h

We wish to obtain the (i, j) pair for (p, q):

p * i + q * j = h

(p mod q) can be represented as:

C Program for Extended Euclidean algorithms

Substituting this expression into the (i1, j1) coefficient equation yields:

C Program for Extended Euclidean algorithms

Pseudo Code for the Extended Euclidean Algorithm (EEA):

function Gcd_Extended(p, q)
    a<- 0;    old_a <- 1
    b <- 1;    old_b <- 0
    c <- q;    old_c <- p
    while c ? 0
        quotient <- old_c div c
        (old_c, c) <- (c, old_c - quotient * c)
        (old_a, a) <- (a, old_a - quotient * a)
        (old_b, b) <- (b, old_b - quotient * b)       
    output "Bézout coefficients:", (old_a, old_b)
    output "greatest common divisor:", old_c
    output "quotients by the GCD:", (b, a)

Example 1:

This program code shows the Extended Euclidean Algorithm (EEA) in the C programming language using the Recursive Method.

/* C program code to show the operation of an Extended Euclidean Algorithm (EEA)*/
#include <stdio.h>


/* Extended Euclidean Algorithm (EEA) C Function */
int Gcd_Extended(int p, int q, int* i, int* j)
{
	/* Base Condition for EEA */
	if (p == 0) 
	{
		*i = 0;
		*j = 1;
		return q;
	}


	int i1, j1;  /* To save the result of the recursive call function */ 
	int Gcd_E = Gcd_Extended(q % p, p, &i1, &j1);


	/* Using the results of the recursive call, update the values of i and j */
	*i = j1 - (q / p) * i1;
	*j = i1;


	return Gcd_E;
}


/* main() function block */
int main()
{
	int i, j;
	int p = 18, q = 63;
	int h = Gcd_Extended(p, q, &i, &j);
	printf("The GCD of two numbers (%d, %d) = %d", p, q, h);
	return 0;
}

Output:

C Program for Extended Euclidean algorithms

Auxiliary Space of EEA : O(1)

Time Complexity of EEA : O(log min(p, q))

This extended Euclidean algorithm implementation yields proper answers for negative numbers as well.

Example 2:

This program code shows the Extended Euclidean Algorithm (EEA) in the C language using the Iterative Method.

#include<stdio.h>
int Gcd_Extended(int,int);
int main()
{
	int p,q;
	int Gcd_E;
	printf("Enter the first number: ");
	scanf("%d",&p);
	printf("\nEnter second number:");
	scanf("%d",&q);
	
	Gcd_E=Gcd_Extended(p,q);
	printf("The gcd of two numbers %d and %d is: %d",p,q,Gcd_E);
	
	return(0);
	
}
int Gcd_Extended(int p,int q)
{
	int r;
	if(p==0)
	{
		return(q);
	}
	
	else if(q==0)
	{
		return(p);
	}
	
	else
	{
		while(q!=0)
		{
			r=p%q;
			p=q;
			q=r;
		}
		return(p);
	}
}

Output:

C Program for Extended Euclidean algorithms

When p and q are coprime, the extended Euclidean method comes in handy (or the gcd is 1). Because i is the mod multiplicative inverse of "p modulo q," and j is the mod multiplicative inverse of "q modulo p." The calculation of the modulo inverse, in particular, is a critical step in the RSA public-key encryption scheme.


Related Topics

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.