Skip to content

Instantly share code, notes, and snippets.

@dannycjones dannycjones/euclids.rb
Last active Jun 2, 2016

Embed
What would you like to do?
An implementation of Euclid's algorithm in Ruby finding the highest common factor for two numbers.
def euclids(a, b) # Euclid's Algorithm
quotient = a / b
remainder = a % b # modulo
puts "#{a} = #{quotient}(#{b}) + #{remainder} [hcf(#{a}, #{b})]"
return b if remainder.zero?
euclids(b, remainder)
end
def hcf(a, b)
euclids(a, b)
end
begin
num1 = ARGV[0].chomp.to_i
num2 = ARGV[1].chomp.to_i
puts "\nhcf(#{num1}, #{num2}) = #{hcf(num1, num2)}"
rescue
puts "Invalid arguments given!"
puts "euclids.rb <num1> <num2>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.