for prime numbers within 100
by white Shinhuata (http://blog.csdn.net/whiterbear) reprint need to indicate the source, thank you.
Description:
Output - all prime numbers within the prime number are separated by a single space
Analysis:
First, we know the prime number: prime number, also known as prime number, there are infinite. A natural number greater than 1, except 1 and itself, cannot be divided into other natural numbers (prime numbers), in other words, the number has no other factor other than 1 and itself; otherwise called composite. The smallest prime number is 2.
Program 1: Use Trial Division to see if there is a factor of n in a number smaller than n, and if not, then the number is prime.
Def output_all_prime (): For I in range (2, 101): flag = truefor J in range (2, i): if i%j = = 0:flag = Falsebreakif Flag:print i ,
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 8997
Program 2: upgrade Trial Division, we do not need to try to divide all the integers less than n, just try to divide 2 to square root n on the line. Because the factors are all paired up. A pair of factors, one of which is necessarily less than or equal to the square root n, and the other must be greater than the square root n.
Procedure 3: The more pythonic approach:
def output_all_prime5 ():p rint ". Join (['%s '% x for x in range (2, 101) if not [y for y in range (2, int (POW (x,. 5) + 1)) I F x%y = = 0]]) #或者print ". Join ([STR (i) for x in range (2, 101) if not [y for y in range (2, int (POW (x,. 5) + 1)) if x%y = = 0 ]])
Here, the trial division ends.
Below we study the n Realm (n > 10) of the prime number algorithm and give the relevant Python code.
Program 4: Use space-time-change method.
Description: "Try all the odd numbers from 3 to √x, or some waste." For example, to determine whether 101 prime, 101 of the root of the root is 10, then, according to Realm 4, the odd number to try is: 3,5,7,9. But you find no, the attempt to 9 is superfluous. Can not be divisible by 3, must not be divisible by 9 ... Following this line of thought, these ape-like programs will find: In fact, just try to be less than √x prime number. And these prime numbers happen to be in front of you (do you think it's wonderful?) )”
We use an array of prime_list to hold all prime numbers, and each time a prime number appears, it is added to the array. And then every time you judge whether N can be saved, these prime numbers want to be divisible.
Def output_all_prime6 (): ' Realm five ' prime_list = [2]for i in range (3, 101): flag = TRUESQRTN = Int. (POW (I,. 5)) for J in Prime_list: If Sqrtn >= j:if i%j ==0:flag = Falsebreakelse:breakif flag:prime_list.append (i) print '. Join ([STR (i) for I in prime_l IST])
Program 5: Eratosthenes Sieve method (can be Wikipedia).
Introduction: First of all, 2 is recognized as the smallest prime number, so the multiples of all 2 are removed, and then the remaining ones greater than 2, the smallest is 3, so 3 is prime, and then all the multiples of 3 are removed, the remaining ones greater than 3, the smallest is 5, so 5 is prime number ...
As the process repeats, it is possible to remove all the composite in a certain range (as if the sieve had been sifted away), and the rest is prime. Wikipedia has a very image of the animation, can intuitively reflect the work process of the Sieve method. Image address.
Def output_all_prime7_1 (): ' Eratosthenes sieve ' ori_list = [True for i in range (0, 101)]ori_list[0], ori_list[1] = False, Falsei = 2n = 100while I*i <= n:for J in range (2, N): If I*j <= n:ori_list[i*j] = falseelse:breakwhile true:i + 1if i*i <= n and ori_list[i] = = False:continueelse:breakfor I,num in Enumerate (ori_list): if num = = True:print I,
For prime numbers within 100