Python programming quality instance code, python programming quality instance

Source: Internet
Author: User

Python programming quality instance code, python programming quality instance

This article focuses on the Python programming quantity examples, and selects several numbers for testing, as shown below.

Definition:Prime number. A natural number greater than 1, except 1 and itself, cannot be divisible by other natural numbers called prime numbers; otherwise, it is called a union number.

We know that all natural numbers (except 0 and 1) can be written in the format of multiplying a few prime numbers and then multiplying them by one, so we can use the number to try to see if it can divide the prime numbers smaller than it.

First, we create an empty list, And then we know that 2 is the smallest prime number, so we add 2 to this blank list, and then we start the loop. The first number starts from 3, divide by 3 by the prime number smaller than 3, and the prime number smaller than it can be divisible by it. we add 3 to the list we created, and then cycle to 4, in the list, there is a prime number 2 which can be divisible. We will remove this number because it is not the prime number we want. In the following example, the sum is removed, and the prime number is saved to the list. Finally, the list is the set of prime numbers.

Import timestart = time. clock () I = input ('Please enter an integer: ') # create an empty listr = list () # add element 2r. append (2) # Start from 3 to filter for a in range (3, I): B = False # use a divided by the prime number less than a B for B in r: if a % B = 0: B = False break else: B = True if B = True: r. append (a) print rt = (time. clock ()-start) print t

As shown below:

The following code is tested with 1000:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997]
3.29336980871

This Code requires a little list knowledge. (This is just the case in the school class.) You can just practice how to add and call the elements in the list!

Let's compare the efficiency of the easiest way to think.

import timestart=time.clock()b=Falsei=input('enter an integer please:')for a in range(2,i):  for c in range(2,a):    if a%c==0:      b=False      break    else:      b=True  if b==True:    print a,elapsed=(time.clock()-start)print elapsed

The running result is:

Enter an integer please: 200
3 5 7 11 13 17 19 23 29 31 37 41 47 53 59 61 67 71 73 79 83 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 2.86033831663
>>>

It is obviously slower to run than the previous code.

The following code uses the first section to calculate the prime number less than 100000. There are actually some cards. The following results are for your reference (only some results are taken ):

Summary

The above is all the content of this article on Python programming quality instance code, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.