-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Extended_Euclidean_Algorithm.rb (#2949)
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
=begin | ||
Extended Euclidean Algorithm | ||
============================== | ||
GCD of two numbers is the largest number that divides both of them. | ||
A simple way to find GCD is to factorize both numbers and multiply common factors. | ||
GCD(a,b) = ax + by | ||
If we can find the value of x and y then we can easily find the | ||
value of GCD(a,b) by replacing (x,y) with their respective values. | ||
=end | ||
|
||
def extended_gcd(a, b, x, y) #Function for extended Euclidean Algorithm | ||
|
||
#Base Case | ||
return b if a == 0 | ||
|
||
x1, y1 = 1, 1 #To store results of recursive call | ||
gcd = extended_gcd(b % a, a, x1, y1) | ||
#Update x and y using results of recursive call | ||
x = y1 - (b / a) * x1 | ||
y = x1 | ||
return gcd | ||
end | ||
|
||
x , y = 1, 1 | ||
a = gets.to_i | ||
b = gets.to_i | ||
puts "GCD of numbers #{a} and #{b} is #{extended_gcd(a, b, x, y)}" | ||
|
||
=begin | ||
INPUT: | ||
27 | ||
81 | ||
OUTPUT: | ||
GCD of numbers 27 and 81 is 27 | ||
=end |