2014年3月29日土曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、8.(Primes revisited)を解いてみる。

その他参考書籍

8.(Primes revisited)

コード(BBEdit)

sample.py

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

def isPrime(y):
    if y < 2:
        print(y, 'is not prime')
    else:
        x = y // 2
        while x > 1:
            if y % x == 0:
                print(y, 'has factor', x)
                break
            x -= 1
        else:
            print(y, 'is prime')

for y in [13, 13.0, 15, 15.0, 1, 1.0, 0, 0.0, -10, -10.0]:
    isPrime(y)

入出力結果(Terminal)

$ ./sample.py
13 is prime
13.0 is prime
15 has factor 5
15.0 has factor 5.0
1 is not prime
1.0 is not prime
0 is not prime
0.0 is not prime
-10 is not prime
-10.0 is not prime
$

0 コメント:

コメントを投稿