Python特徴①:シンプルな文法!
Python の文法は本当にシンプルなのか?
2つの値(a, b)の最大公約数を求めるプログラムを
Python, Java, Rubyの3つの言語で比較。
※最大公約数を求めるアルゴリズムはユークリッドの互除法
を使用
def gcd(a, b):
while b > 0:
a, b = b, a % b
return a
def gcd(a, b)
a, b = b, a if a > b
until a == 0
a, b = b % a, a
end
return b
end
private static long getKoyakusu(long a, long b) {
long candidate = a;
while (b % a != 0) {
candidate = b % a;
b = a;
a = candidate;
}
return candidate;
}
Java Ruby
Python