This article mainly describes the Python programming method to determine whether a positive integer is a prime number, involving Python mathematical operations related operations skills, the need for friends can refer to the next
The example in this paper describes how Python programming determines whether a positive integer is a prime number. Share to everyone for your reference, as follows:
Import Stringimport math# Determines whether the function of prime number Def isprime (n): if (n<2): return False; Elif (n==2): return True; Elif (n>2): for D in range (2,int (Math.ceil (MATH.SQRT (n)) +1): if (n%d==0): return False; return True;num=input (); strnum=list (num) #将输入值转换为List字符串flag =true; #设置一个标志位 # The following loop is used for all results resulting from a cyclic displacement of the user input value for I in range (0,len (strnum)): Lastp=strnum.pop (); #获取并删除最后一位 Strnum.insert (0,LASTP); #将上一步删除的数字添加到最前面 stempnumstr= "; #用于保存某一步位移结果的临时变量 for each in Strnum: Stempnumstr+=each, #将位移后的字符串合并 stempnum=string.atoi (STEMPNUMSTR), #转换为整形 # or stempnum= (num//(10**i)) + (num% (10 **i)) * (10** (lens-i)) if (IsPrime (stempnum) ==false): #再判断位移后的这个数是否是素数, if not flag=false;# The direct determination of the current user input is not the number of cyclic prime print ('%s not loop Prime No. '%num); break; #并中止后续计算, exit loop if (flag==true): #如果Flag的值依然为True, indicating that the number must be a cyclic prime of print ('%s is loop prime number '%num);
Running results such as: