Python programming method to determine whether a positive integer is a prime number, python Prime Number
This article describes how to determine whether a positive integer is a prime number through Python programming. We will share this with you for your reference. The details are as follows:
Import stringimport math # function 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 (str (num) # convert the input value to List string flag = True; # Set a flag # the following cycle is used to calculate all the results obtained after the Cyclic Displacement of user input values for I in range (0, len (strNum): lastP = strNum. pop (); # obtain and delete the last strNum. insert (0, lastP); # Add the number deleted in the previous step to stempNumStr = ''; # temporary variable for saving the result of a certain step of displacement in strNum: stempNumStr + = each; # merge the displaced string with stempNum = string. atoi (stempNumStr); # convert to an integer # Or stempNum = (num // (10 ** I) + (num % (10 ** I )) * (10 ** (lens-I) if (isPrime (stempNum) = False): # determine whether the number after displacement is a prime number, if not flag = False; # It is determined that the number entered by the current user is not a cyclic prime number print ('% s not Loop prime number' % num); break; # and the subsequent calculation is aborted, exit Loop if (flag = True): # if the Flag value is still True, it indicates that this number must be a cyclic prime number print ('% s is Loop prime number' % num );
The running result is as follows: