1, define the calculation using prime number
# - * - Coding: UTF-. 8 - * - # Version: python3.7 '' ' @ File: prime_number @ author: zhangyangyang @ Create: 2020/3/22 @ the remark: ' '' # version1 for
Import datetime # Import module computational efficiency
Start = datetime.datetime.now () COUNT = 0 for X in range (2,100000): # find a prime number within the specified range for I in range (2, X): # divided by itself other than 1 and number IF X% I == 0: BREAK the else : #Print (X) COUNT =. 1 + Delta = (datetime.datetime.now () - Start) .total_seconds () # total_seconds () Total number of seconds Print ( ' COUNT = ' , COUNT, ' Delta = ' , Delta) # Wall time on the results: COUNT = = 148.146291 # 9592 Delta extremely inefficient
2, optimization 1: is calculated, the critical value square value
# Version2: Optimization
Import datetime # import modules computational efficiency Start = datetime.datetime.now () COUNT = 0 for X in Range (2,100000 ): for I in Range (2, int (X + 0.5 **. 1)) : # optimization 1, tested: critical value square value IF X% I == 0: BREAK the else : # Print (X) COUNT = 1 + Delta = (datetime.datetime.now () - Start) .total_seconds ( ) # total_seconds () total number of seconds Print ( ' COUNT = ', COUNT, ' Delta = ' , Delta) execution results: COUNT = 9592 = 1.084154 # Delta efficiency greatly improved
3, optimization 2: the even number greater than two full engagement
# Version3: Optimization + Import datetime # import modules computational efficiency Start = datetime.datetime.now () COUNT =. 1 # Print (2) # 3 from the beginning, their printing 2 for X in Range (3,100000,2): # optimization 2: 3 from the beginning of the odd # for i in range (3, int (x ** 0.5 +. 1)): optimization # 3: odd and not modulo 2 for I in Range (3, int (X ** 0.5 ) + 2): # optimization: i.e., they do not and the even modulus IF X% I == 0: BREAK the else : # Print (X) COUNT =. 1 + Delta= (Datetime.datetime.now () - Start) .total_seconds () # total_seconds () Total number of seconds Print ( ' COUNT = ' , COUNT, ' Delta = ' , Delta) # Wall time execution result of: COUNT = 9592 delta = 0.553471 # to further improve performance
4, Optimization 3: 5 are all multiple composite number, excluding multiples of 5
# Version4: Optimization ++ Import datetime # import modules computational efficiency Start = datetime.datetime.now () COUNT =. 1 # Print (2) # 3 from the beginning, their printing 2 for X in Range (3,100000,2): # optimization 2: 3 starting from the odd IF X> 10 and X 5% == 0: Continue # optimization: 5 excluding multiples of # for I in Range (3, int (X + 0.5 **. 1)): # optimization 3: odd and not modulo 2 for I in Range (3, int (X ** 0.5) + 1,2): # optimization: i.e., they do not and the even modulus IF X% I == 0: BREAK the else : # Print (X) COUNT =. 1 + Delta = (datetime.datetime.now () - Start) .total_seconds () # total_seconds () Total number of seconds Print ( ' COUNT = ' , COUNT, ' Delta = ' , Delta ) # time on the wall of the results: COUNT = 9592 = 0.493866 Delta
5, the number of quality applications:
Application in the field of cryptography, have to use a large prime number