×

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

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 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.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.