"Python" programming language Introduction Classic 100 cases--12

Source: Internet
Author: User

1 #题目: Determine the number of primes between 101-200 and the output of all primes.


Code:

3 TMP = [n for n in range (101,201)] 4 for I in Reversed (TMP): 5 for J in Range (2,i): 6 if (i%j = = 0): 7 Tmp.remove (i) 8 break 9 print (' 101 to 200 with%d primes '%len (tmp)) ' Print (' These primes are: ') print (TMP)


Operation Result:

[[email protected] code_100]# Python code_12.py 101 to 200 there are 21 prime numbers, respectively: [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199][[email protected] code_100]#


Code Explanation:

    This code with a more cumbersome way, but to practice the operation of the list is good, because the number of elements involved in the count, I think with Len (list) More appropriate,  idea is to traverse the entire list, If you find that it is not a prime number, it is removed from the list and the last number is the prime number from 101 to 200.   3 tmp = [n for n in range (101,201)]        #建立101到200的升序list   4 for i in reversed (TMP):                  #倒叙遍历tmp列表   5      for j in range (2,i):                 #针对tmp中的每个成员, with 2 to (I-1) to take the remainder   6          if  (i%j == 0):                   #如果取余等于零, description not prime number   7              tmp.remove (i)                #将不是素数的数字从list中删除   8              break                        #直接跳出循环, do not perform subsequent validations to conserve resources   9 print (' 101 to 200 has%d prime number '%len (TMP))    #格式化输出素数的个数  10 print (' These primes are: ')  11 print  (TMP)                                 #打印素数                 used a flashback in line fourth to traverse the list, Because of the characteristics of the tmp.remove, if the positive order is not the desired result, because remove deletes an element, the index of all elements behind this element will be reduced by 1 (if all elements are not changed the subscript is not a problem), but the For loop iteration is subscript followed by 1, This will result in the traversal of the list when there are some elements are not traversed, with the step of debugging can see the effect of a detailed explanation of remove, where the use of flashbacks from the back to start minus, the previous subscript will not be changed, so that the final result will not be affected.



This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1745166

"Python" programming language Introduction Classic 100 cases--12

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.