Last active
November 22, 2017 05:27
-
-
Save komasaru/65e34de2dc90bf6434f8a6f6169e3667 to your computer and use it in GitHub Desktop.
Ruby script to compute G.C.D. with Euclid's algorithm.
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
#! /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