Created
November 25, 2010 06:30
-
-
Save hzno/714995 to your computer and use it in GitHub Desktop.
(Python) 素数かどうかを判定し、素数でなかったらどの数で分解できるのか表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
# Author: Iyori Komiyama | |
# Contact: [email protected] | |
# site: http://hazimarino.blogspot.com/ | |
"""\ | |
素数かどうかを判定し、素数でなかったらどの数で分解できるのか表示する | |
9973 の素数で分解できる範囲まで。 | |
""" | |
from __future__ import unicode_literals | |
def enter(): | |
while True: | |
try: | |
digits = int(raw_input('Please input a number>> ')) | |
if digits < 0: raise | |
except ValueError: | |
print("正の整数を入力してください") | |
else: return digits | |
def primes(value): | |
p_nums = (x for x in xrange(2, 10000) | |
if 0 not in (x % y for y in xrange(2, x))) | |
if (value == 1) or (value == 0): | |
print("{0} は素数ではありません".format(value)) | |
return | |
for p in p_nums: | |
if value == p: | |
print("{0} は素数です".format(value)) | |
return | |
elif not value % p: | |
print("{0} は素数ではありません: " \ | |
"{1} x {2}".format(value, value / p, p)) | |
return | |
else: | |
print("数値が大きすぎます") | |
def main(): | |
print('素数かどうかを判定します') | |
print('*' * 80) | |
num = enter() | |
print('*' * 80) | |
primes(num) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment