#coding=utf8
#程式集訓 A009:質數判別
'''
/*
描述 輸入一個正整數,如果是質數,則輸出 Yes,如果不是,則輸出 No。
'''
#==========================
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#==========================
n=1
while n!=0:
a=int(input("輸入正整數 A="))
if (is_number(a) != True) :
print ('這是不合法的輸入. 請再輸入一次...')
n=1
else:
A=int(a)
n=0
#==========================
x=0
for i in range (2,A+1):
x=A%i
if (A==i) :
print(A,"為質數")
break
if (x==0) :
print(A,"不是質數")
break
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================== RESTART: D:/程式語言 Python 入門/程式集訓/A009.py ==================
輸入正整數 A=12
12 不是質數
>>>
================== RESTART: D:/程式語言 Python 入門/程式集訓/A009.py ==================
輸入正整數 A=3
3 為質數
>>>
================== RESTART: D:/程式語言 Python 入門/程式集訓/A009.py ==================
輸入正整數 A=9
9 不是質數
>>>
2016年9月19日 星期一
訂閱:
張貼留言 (Atom)
MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構
MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 這張圖片展示了一個 結合 MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 (主要透過 Node-RED 進行資料整合)。 系統包含三個核心部分,其運作功能說明如下: 1. ESP32 終端設備(硬體控制層...
-
數位IC設計入門-Verilog combinational logic 8 to 1 Multiplexer 多工器 Behavioral Modeling (& Test Bench) //數位IC設計入門-Verilog combinationa...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
Line 發報機 Python TKinter (CONFIG_LINE Message API_2.py) import serial import serial.tools.list_ports import tkinter as tk from tkinter import...
沒有留言:
張貼留言