Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active November 22, 2017 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/65e34de2dc90bf6434f8a6f6169e3667 to your computer and use it in GitHub Desktop.
Save komasaru/65e34de2dc90bf6434f8a6f6169e3667 to your computer and use it in GitHub Desktop.
Ruby script to compute G.C.D. with Euclid's algorithm.
#! /usr/local/bin/ruby
# ********************************************
# ユークリッドの互除法(再帰的に求める方法)
# ********************************************
class Calc
def calc_gcd(a, b)
return b == 0 ? a : calc_gcd(b, a % b);
end
end
begin
print "1つ目の自然数:"
a = gets.chomp.to_i
print "2つ目の自然数:"
b = gets.chomp.to_i
puts "a = #{a}, b = #{b}"
obj_calc =Calc.new
puts "最大公約数 = #{obj_calc.calc_gcd(a, b)}"
rescue => e
$stderr.puts "例外発生!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment