Home > other >  Use the Python list method for prime Numbers within 100000 efficiency why so low
Use the Python list method for prime Numbers within 100000 efficiency why so low

Time:09-24

Use Python list methods for prime Numbers within 100000 why so low efficiency, I use digital integer common calculation method for the time is about 3.8 seconds, with the method of the list stored prime time of 70 + seconds, the difference is too big,

CodePudding user response:

# print all prime Numbers within 100000
K=0
Ss_l=# [2] to establish a prime number list
For I in range (3100-000) :
K=0
For j in ss_l: # as long as it can be divided exactly by a prime number is 1
If I % j==0:
K +=1
Break
If k==0:
Ss_l. Append (I)
Print (ss_l)

CodePudding user response:

Not right, I can't even 1 SEC out ah, my computer speed is only 1.8,,,

CodePudding user response:

The import time
The from math import SQRT
Def is_prime (n) :
If n & lt;=1:
Return False
For I in range (2, int (SQRT (n) + 1)) :
If I==0 n % :
Return False
Return True
M=2
N=100000
Prime_list=[]
Start=time. Clock ()
For I in range (m, n) :
If (is_prime (I)) :
Prime_list. Append (I)
T=time. Clock () - start
# print (prime_list)
Print (" find prime range (" + STR (m) + ", "+ STR (n) +") using the time..
")print(t)

CodePudding user response:

With the list method, the greater the number, the longer the length of the list, can also add a judgment, list value is greater than the square root of the current value for judgment, more than you can need not to continue the operation

CodePudding user response:

In this way: https://github.com/PyUnit/pyunit-prime#%E8%BF%94%E5%9B%9E%E5%8C%BA%E9%97%B4%E5%86%85%E7%9A%84%E8%B4%A8%E6%95%B0

Less than 0.1 seconds
  • Related