Python Programming example code sharing that determines whether a positive integer is a prime number

Source: Internet
Author: User
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:

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.