Python Learning-Exercises (12)

Source: Internet
Author: User

"" "Title: Determine how many primes are in between 101-200 and output all primes. Prime number, also known as prime number, has an infinite number. Prime numbers are defined as the natural number greater than 1, except that there are no other factors other than 1 and itself. "" "Import Mathdef Answer1 ():" "According to the prime number definition, one judgment: return:" "Print (" Output One ", end=": ") Sumcount = 0 findti                Mes = 0 for I in range (101, +): for J in range (2, i): Findtimes + = 1 if i% J = = 0:    Break Else:print (I, end= ",") Sumcount + = 1 Print ("Total%d"% Sumcount, end= ",") Print ("A total of%d times"% findtimes) answer1 () def answer2 (): "" "The above-mentioned judgment method, obviously has the very low efficiency question. For each number of n, actually do not need to judge from 2 to n-1, we know that a number if the factorization can be decomposed, then the decomposition of the two number must be a less than or equal to sqrt (n), a greater than or equal to sqrt (n), whereby the above code does not need to traverse to n-1, traverse to sqrt (n    ), because if the sqrt (n) is not found on the left, then the right side must not be able to find the approximate. : return: "" Print ("Output two", end= ":") sumcount = 0 Findtimes = 0 for I in range (101, a): Sqrtnum = i                NT (MATH.SQRT (i)) for J in range (2, Sqrtnum + 1): Findtimes + = 1 if i% J = = 0:           Break Else:print (I, end= ",") Sumcount + = 1 Print ("Total%d"% Sumcount, end= ",") print ("%d times"% findtimes) answer2 () def answer3 (): "" "Party The method (2) should be the most common judgment algorithm, the time Complexity O (sqrt (n)), the speed is much faster than the method (1) O (n). Recently on the Internet by chance to see another more efficient method, for the moment called Method (3), because there is no original source, there is no link posted here, if there are original people see, please contact me, will make up the copyright reference. Here's a quick way to judge this, first look at a rule about the distribution of prime numbers: prime numbers greater than or equal to 5 must be adjacent to multiples of 6. For example 5 and 7,11 and 13,17 and 19 etc.; proof: To make x≥1, the natural number greater than or equal to 5 is expressed as follows:    6x-1,6x,6x+1,6x+2,6x+3,6x+4,6x+5,6 (x+1), 6 (x+1) +1 As can be seen, not in multiples of 6, that is, 6x on both sides of the number of 6x+2,6x+3,6x+4, because 2 (3x+1), 3 (2x+1), 2 (3x+2), so they must not be prime, and then remove the 6x itself, it is clear that the prime will appear only in 6x adjacent sides.    Here is an off-topic, about the twin Prime, interested in the friends can learn a little bit, because it has nothing to do with our topic, skip.    One thing to note here is that in multiples of 6, adjacent sides are not necessarily prime numbers. According to the above rules, judge the prime number can be 6 for the unit fast Forward, the method (2) cycle in the i++ step increase of 6, speed up the judgment speed, the code is as follows: Master everywhere ah from the can get, as long as 96, every 6 calculated once: return: "" "Print (" Output three "            , end= ":") sumcount = 0 Findtimes = 0 for I in range (102, A, 6): For Num in (i-1, i + 1): sqrtnum = Int (math.sqrt (num)) for J in range (2, Sqrtnum + 1): Findtimes + 1 if n         Um% J = = 0:           Break Else:print (num, end= ",") Sumcount + = 1 Print ("Total%d"% Sumcou NT, end= ",") print ("A total of%d times"% findtimes) Answer3 () def answer4 (): "" "above the method, the master can also be improved because the above available, 6x-1 and 6x+1 pits will not be 6x,6x+    2,6x+3,6x+4 divisible, weak it is a prime number, then it is also 6x-1 or 6x+1 divisible so the above method, the inside of the judgment can also be judged across 6来: return: "" "Print (" Output four ", end=": ") Sumcount = 0 Findtimes = 0 for I in range (102, 1, 6): For Num in (i-1, i +): sqrtnum = Int (math.sqrt (num )) for J in range (5, Sqrtnum + 1, 6): Findtimes + = 1 if num% j = = 0 or num% (j + 2) = = 0:break Else:print (num, end= ",") Sumcount + 1 PR    Int ("Total%d"% Sumcount, end= ",") print ("A total of%d times"% findtimes) answer4 () def answer5 (): "" "excludes even numbers, array statistics: return: "" "Print (" Output five ", end=": ") findtimes = 0 Numlist = [] for I in range (101, 2): Numlist.append (str (i )) Sqrtnum = Int (MATH.SQRT (i)) for J in range (2, Sqrtnum + 1): Findtimes + = 1 if i% J = 0:nu Mlist.pop () Break print (",". Join (Numlist), end= ",") print ("%d"% len (numlist), end= ",") Print ("Total Compared%d "% findtimes" Answer5 () class Answer6: "" "" "" "" "" "" "" "Def __init__ (self):" "Constructor" " "Self.sumcount = 0 self.findtimes = 0 Self.startnum = 102 Self.endnum = + print (" Output Six ", end=": ") def __iter__ (self):" "" Iterator: Return: "" Return self def __next__ (SE LF): "" "Iterator Next:return:" "" If Self.startnum > Self.endNum:raise Stopi  Teration for Num in (self.startnum-1, Self.startnum + 1): sqrtnum = Int (math.sqrt (num)) for                    I in range (5, Sqrtnum + 1, 6): Self.findtimes + = 1 if num% i = = 0 or num% (i + 2) = = 0: Break           Else:print (num, end= ",") Self.sumcount + = 1 Self.startnum + 6answer = an    SWER6 () for I in Answer:passprint ("total%d"% Answer.sumcount, end= ",") print ("Total%d times"% answer.findtimes) def answer7 (): "" "Practice yield generator, note and Answer6 area" "" Print ("Output seven", end= ":") Startnum = 102 Sumcount = 0 findtimes = 0 whi            Le startnum < 2000:for num in (startNum-1, Startnum + 1): sqrtnum = Int (math.sqrt (num))                    For I in range (5, Sqrtnum + 1, 6): Findtimes + = 1 if num% i = = 0 or num% (i + 2) = = 0: Break Else:sumcount + = 1 print (num, end= ",") Yie LD num Startnum + = 6 Print ("Total%d"% Sumcount, end= ",") print ("%d times"% findtimes) for I in Answer7 (): pas    Sdef Answer8 (): "" "Practice array Builder Expression reference: Http://blog.csdn.net/u014745194/article/details/70176117:return:" "   Print ("Output eight", end= ":") Numlist = List (filter (lambda x:x not in set ([I-I in range (101, a) for J in range (2, i-1) if not I% j]), Range (101)) print (Numlist, end= ",") print ("%d"% len (numlist)) Answer8 ()

  

Python Learning-Exercises (12)

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.