Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views1 page

Lab 07 For Cse

Lab c program

Uploaded by

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

Lab 07 For Cse

Lab c program

Uploaded by

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

Matrix Laboratory Manual

7c) Solving linear congruences 𝒂𝒙 ≡ (𝒎𝒐𝒅 𝒎)

Algorithm
1. Start
2. Define the coefficients , right-hand side of the congruence and modulus
Value ( a b and m)
3. Use extended Euclidean algorithm to find the inverse of a modulo m
4. Check if the inverse exists
5. if GCD is 1
Solve for x using the formula: x ≡ (b * x_inv) (mod m)
Else display No inverse exists
6. Display result

Example : Solve linear congruence 3x≡10(mod13)


Code:

a = input("coefficient of x ")

b = input("right-hand side of the congruence")


m = input("modulus Value")

% Step 1: Use the extended Euclidean algorithm to find the inverse of a modulo m
[gcd_val, x_inv] = gcd(a, m)

% Step 2: Check if the inverse exists (gcd must be 1)


if gcd_val ~= 1
disp('No inverse exists')
else
% Step 3: Solve for x using the formula: x ≡ (b * x_inv) (mod m)
x = mod(b * x_inv, m)
fprintf('The solution is x =%.f ', (x))
end

Output:
a=3
b = 10
m = 13
gcd_val = 1
x_inv = -4

The solution is x =12.0

Example: 2: Solve linear congruence 15x≡1(mod26)


Example : 3 Solve linear congruence 2x ≡ 5 (mod 4)
Example 4: Solve the Linear Congruence 11x = 1 mod 23

Department of Mathematics KLE Institute of Technology

You might also like