Python programming method to determine whether a positive integer is a prime number, python Prime Number

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.