-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_extended_euclidean_algo.py
More file actions
44 lines (40 loc) · 1.31 KB
/
06_extended_euclidean_algo.py
File metadata and controls
44 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def find_gcd(a, b):
print(f" = GCD({a}, {b})")
# Edge cases
if a == 0 or a == b:
return b
# Using recursion to find answer
return find_gcd(b % a, a)
def extended_euclidean_algo(a, b, gcd):
# Find x and y recursively
if a == 0:
return 0, 1
x1, y1 = extended_euclidean_algo(b % a, a, gcd)
# Update x and y from the results
x = y1 - (b // a) * x1
y = x1
print(f" {gcd} = {a} * ({x}) + {b} * ({y})")
# Return the results
return x, y
print("\n\n### EXTENDED EUCLIDEAN ALGORITHM ###\n\n")
# Get the two numbers
print("-> Enter the values:")
num1 = int(input(" Number 1 = "))
num2 = int(input(" Number 2 = "))
# Assign lower value to a and larger to b
if num1 < num2: a, b = num1, num2
else: a, b = num2, num1
# Find the GCD
print(f"\n-> GCD of {a} and {b}:")
gcd_value = find_gcd(a=a, b=b)
print(f" => GCD({a}, {b}) = {gcd_value}")
# Find equation -> gcd value = (a * x) + (b * y)
print("\n-> Linear equation: ", end="")
print(f"{gcd_value} = ({a} * x) + ({b} * y)")
x, y = extended_euclidean_algo(a=a, b=b, gcd=gcd_value)
# Result
print(f"\n\n==> Equation: {gcd_value} = ({a} * x) + ({b} * y)")
print(f" Value of x = {x}")
print(f" Value of y = {y}")
# Footer
print("\n\nDevanshu Gupta 21BCE0597\n\n")