Python: find prime with the filter function (Reevaluation generator)

Objective: To become more familiar with the application generator.

Reference: https://www.liaoxuefeng.com/wiki/1016959663602400/1017404530360000

 

Prime Definition:

Primes: prime, also known prime number. Is a natural number greater than 1, except 1 and itself, the called number is not divisible by the number of other natural prime number.

 

method:

Calculating primes a process is Eppendorf sieve method :

First of all, from the list 2of all natural numbers, to construct a sequence begins:

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

A second step of taking a first sequence number 2, it must be a prime number, then 2the sequence of the 2multiple screen out:

3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...

At this time, the sequence is set ⚠️ 3 starting from the odd.

The third step, taking the first new sequence number 3, which must be a prime number, then 3the sequence of the 3multiple screen out:

5, 6, 7, 8910, 11, 12, 13, 141516, 17, 18, 19, 20, ...

A fourth step, the first new sequence number to take 5, and then 5the sequence 5out of multiple screens:

7, 8910, 11, 12, 13, 141516, 17, 18, 19, 20, ...

Follow-up, continue to sift through, you can get all the prime numbers.

 

Code:

2 is a prime number, screening starts from an odd sequence:

# Generates an odd sequence. Starting with 3 
DEF _odd_iter (): 
    n- =. 1
     the while True: 
        n- + 2 =
         the yield n-

 

 Filter functions:

  1. From the above analysis, every screening, are divided by the first number sequence.
  2. With the proviso that x% n> 0. x is a number in the sequence is determined, n is a number for the first sequence. The results are not divisible == 0 is a prime tree. Deleted from the sequence.
  3. Therefore, the determination expression is not fixed. Definition of a function, passing the parameters n, returns for determining a lambda expression. 
# Pass parameters n, generate different lambda expressions. 
DEF _not_divisible (the n-):
     # return a lambda expression. For determining x, is not a prime number. 
    # Prime number can only be called a prime number is divisible by 1 and itself a natural number. 
    # Divisible remainder is zero. 
    # If the numbers are divisible non-self, that is, the remainder is 0, it is naturally not a prime number, delete. 
    return  the lambda X: X% n-> 0

 

The main function:

  1. The first line of code: yield 2, 2 returns prime.
  2. Second line: Create generator, first run, it produces an odd sequence, this sequence will be gradually screened to remove non-prime numbers.
  3. Loop code:
    1. n = next (it), to obtain a first sequence of numbers.
    2. yield n, which is a prime number.
    3. A filter () to filter, a digital generator of it. Because it is iterative. And the new sequence assigned to it variable.

⚠️ new understanding 3. function generator object filter to filter it, in fact, a combination of the code, the _not_divisible (n) Returns the lambda expression and the combination of it's own code, to form a new object <filter object>.

⚠️ my understanding is that the original algorithm _odd_iter () produced is no longer used, but the increased use of new algorithms lambda conditional judgment, <filter object>.

This step is not really ⚠️ digital filter out non-compliant, _ood_iter () simply represents the algorithm to generate a sequence of numbers. But this algorithm is replaced by the new algorithm.

DEF primes ():
     the yield 2   # 2 is a prime number. 
    _odd_iter = IT () # Create a <generator object _odd_iter>, representative of the odd sequence. 
    the while True: 
        n- = Next (IT) # Returns an odd number sequence. 
        yield the n-   # this number is prime. So return. 
        # Create a <filter object>, on behalf of the new sequence. 
        it = filter (_not_divisible (n) , it)

 

 

 

Since primes()所代表的算法生成的an infinite sequence, there is no exit mechanism, it is necessary to set a condition to exit the loop when calling:

for n in primes():
    if n < 1000:
        print(n)
    else:
        break

⚠️primes itself a generator function, primes () is a generator, which itself does not store data, store only data generated algorithm. 

to sum up;

Can be seen from this example, generator algorithm in fact, it does not produce data all at once, but according to the number of calls, and gradually write data to memory.

The present embodiment generates an object as a parameter of the filter () function, in fact, this update code generator, i.e., improved algorithm. Finally, it returns a new iterator. The present embodiment returns <filter object>.

 

 

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11799867.html