首页 > 代码库 > Python 正则表达式验证是否为素数

Python 正则表达式验证是否为素数

CODE:

import re

while True:
    number = int(input('Input number (0 to quit): '))
    if number == 0:
        break
    subject = ''.join([str((1)) for a in range(number)])
    reobj = re.compile(r"^1?$|^(11+?)\1+$")
    if reobj.search(subject):
        print "number %s is not prime number!" % number
    else:
        print "number %s is a prime number!" % number

RESULT:

Input number (0 to quit): 8
number 8 is not prime number!
Input number (0 to quit): 11
number 11 is a prime number!
Input number (0 to quit): 1
number 1 is not prime number!
Input number (0 to quit): 2
number 2 is a prime number!
Input number (0 to quit): 3
number 3 is a prime number!
Input number (0 to quit): 0


Python 正则表达式验证是否为素数