## 2016年5月15日日曜日

### Python - Fruitful Functions(greatest common divisor(gcd), recursion)

Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 6.(Fruitful Functions)のExercises 6-5(No. 1512)を取り組んでみる。

Exercises 6-5(No. 1512)

コード(Emacs)

``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- def gcd(a, b): if b == 0: return a return gcd(b, a % b) if __name__ == '__main__': for a in range(2, 11): for b in range(0, a): print('gcd({0}, {1}) = {2}'.format(a, b, gcd(a, b))) print() ```

```\$ ./gcd.py
gcd(2, 0) = 2
gcd(2, 1) = 1

gcd(3, 0) = 3
gcd(3, 1) = 1
gcd(3, 2) = 1

gcd(4, 0) = 4
gcd(4, 1) = 1
gcd(4, 2) = 2
gcd(4, 3) = 1

gcd(5, 0) = 5
gcd(5, 1) = 1
gcd(5, 2) = 1
gcd(5, 3) = 1
gcd(5, 4) = 1

gcd(6, 0) = 6
gcd(6, 1) = 1
gcd(6, 2) = 2
gcd(6, 3) = 3
gcd(6, 4) = 2
gcd(6, 5) = 1

gcd(7, 0) = 7
gcd(7, 1) = 1
gcd(7, 2) = 1
gcd(7, 3) = 1
gcd(7, 4) = 1
gcd(7, 5) = 1
gcd(7, 6) = 1

gcd(8, 0) = 8
gcd(8, 1) = 1
gcd(8, 2) = 2
gcd(8, 3) = 1
gcd(8, 4) = 4
gcd(8, 5) = 1
gcd(8, 6) = 2
gcd(8, 7) = 1

gcd(9, 0) = 9
gcd(9, 1) = 1
gcd(9, 2) = 1
gcd(9, 3) = 3
gcd(9, 4) = 1
gcd(9, 5) = 1
gcd(9, 6) = 3
gcd(9, 7) = 1
gcd(9, 8) = 1

gcd(10, 0) = 10
gcd(10, 1) = 1
gcd(10, 2) = 2
gcd(10, 3) = 1
gcd(10, 4) = 2
gcd(10, 5) = 5
gcd(10, 6) = 2
gcd(10, 7) = 1
gcd(10, 8) = 2
gcd(10, 9) = 1

\$
```