Skip to content

Instantly share code, notes, and snippets.

@st98
Last active December 31, 2015 05:59
Show Gist options
  • Save st98/7944245 to your computer and use it in GitHub Desktop.
Save st98/7944245 to your computer and use it in GitHub Desktop.
Rubyの練習。ユークリッドの互除法を使って2つの整数の最大公約数を求めます。
# ユークリッドの互除法(非再帰)
def gcd_euclid_l(a, b)
while 0 do
break if b == 0
a, b = b, a % b
end
a
end
# ユークリッドの互除法(再帰)
def gcd_euclid_r(a, b)
if b == 0 then
a
else
gcd(b, a % b)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment