Skip to content

Commit

Permalink
Create Extended_Euclidean_Algorithm.rb (#2949)
Browse files Browse the repository at this point in the history
  • Loading branch information
atarax665 authored May 22, 2020
1 parent 1fcf573 commit f4b3b3f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.rb
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

0 comments on commit f4b3b3f

Please sign in to comment.