Python judges composite, prime numbers

Source: Internet
Author: User

First, define the concepts of composite and prime numbers

Composite: In addition to being divisible by 1 and itself, the number of natural numbers can be evenly divisible by other numbers. (4,6,9,10 ...)

1 defHeshu (m):2List_a = []3      forIinchRange (2,m+1):4          forJinchRange (2, i):5             ifI% J = =0:6List_a.append (i)#determine if I can be divisible by the number (2,i), divisible by composite, added to list7                  Break8     Print(list_a)9Heshu (20)

Prime numbers: The number of natural numbers greater than 1 that cannot be divisible by other numbers except those that are divisible by 1 and itself. (2,3,5,7 ...)

1 defZhishu (m):2List_a = []3      forIinchRange (2,m+1):4Flag =05          forJinchRange (2, i):6             ifI% J = =0:7Flag = 1#If I can be divisible by the number between (2,i), the flag is set to 1, jumping out of the current loop8                  Break9         ifFlag = =0:TenList_a.append (i)#after the inner loop is executed, if flag is not set to 1, then I is prime, add list One     Print(list_a) AZhishu (20)

Tips

Since the beginning of the use of Python, its built-in functions are still not understood, so in code writing may be more thought of C, the total sense of the overall code is a bit bloated.

Especially when writing the prime number function, I j = = 0 is used to judge Composite, but we have to output prime numbers, so think of flag to mark the composite to achieve the purpose.

Later, I saw the method of using the filter () function to handle it, and I feel that it is in line with the elegant nature of Python ...

1 def zhishu2 (num): 2      for  in range (2, num):3         if num% i = = 0:4             return  False5     return  True6 new_list = List (Filter ( Zhishu2,range (2,21))7print(new_list)

How to use the filter () function:

two parameters are received, the first is the function f (output Boolean type), the second is the sequence ist, the elements in the list are sequentially put into F, and the elements in the function are chosen to return True or false, and the last element that returns True is re-formed into a new list.

When you use this function, you initially want to print it directly:

New_list = Filter (Zhishu2,range (2,21))print(new_list)

But there's a problem with the output.

Looking closely at the use of the function, it is found that the filter function output should be the element that makes up the list , not just a list, so it needs to be converted to a list:

New_list = List (Filter (Zhishu2,range (2,21)))print(new_list)

So that we can output it normally.

Python judges composite, prime numbers

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.