2016年5月14日土曜日

開発環境

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

Exercises 6-4(No. 1512)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def is_power(a, b):
    if a == 0:
        return False
    if a == 1:
        return True
    if b == 1:
        return False
    return a % b == 0 and is_power(a / b, b)

if __name__ == '__main__':
    for b in range(1, 3):
        for a in range(20):
            if is_power(a, b):
                print('{0} is a power of {1}'.format(a, b))
            else:
                print('{0} is not a power of {1}'.format(a, b))

入出力結果(Terminal, IPython)

$ ./power.py
0 is not a power of 1
1 is a power of 1
2 is not a power of 1
3 is not a power of 1
4 is not a power of 1
5 is not a power of 1
6 is not a power of 1
7 is not a power of 1
8 is not a power of 1
9 is not a power of 1
10 is not a power of 1
11 is not a power of 1
12 is not a power of 1
13 is not a power of 1
14 is not a power of 1
15 is not a power of 1
16 is not a power of 1
17 is not a power of 1
18 is not a power of 1
19 is not a power of 1
0 is not a power of 2
1 is a power of 2
2 is a power of 2
3 is not a power of 2
4 is a power of 2
5 is not a power of 2
6 is not a power of 2
7 is not a power of 2
8 is a power of 2
9 is not a power of 2
10 is not a power of 2
11 is not a power of 2
12 is not a power of 2
13 is not a power of 2
14 is not a power of 2
15 is not a power of 2
16 is a power of 2
17 is not a power of 2
18 is not a power of 2
19 is not a power of 2
$

0 コメント:

コメントを投稿