prime number generation code in python -
I'm trying the following approach to create primes:
primes = [2] Candidates = 3 lac = 1000000 while candidates & lt; Million: If all (prime candidate for prime prime minister ** 2 and lieutenant; = candidate): primes.append candidate (candidate) = 2
However, it is relatively high Slower than my previous code, where I was not using the function, and was checking myself below:
primes = [2] Candidates = 3 million = 1000000 While the candidate & lt; Million: can_be_prime = In primes for P: if P ** 2 & gt; Candidate: Elife candidate% break = p == 0: can_be_prime = Incorrect break if can_be_prime: primes.append (candidate) candidate + 2 =
while the other one ends within 10 seconds Is it always takes to complete the first one, does anyone help me to understand why the first 100000 numbers are being produced for the first time slowly?
The idea of shortening the code using that generator expression and any
Good, they are not equal. Specifically, in your long-term solution, you are going to run a list of (sorted) primes and brake as soon as you find a key, for which p ** 2 & gt ; Candidate
is true.
However, if you use this check by using the x, then in PRIs P ** 2 & lt; = Trying to use the candidate
. Unfortunately, not is the same even if that check fails, then the generator will continue on the remaining primes, for p ** 2
Em> every one on prime, on every repetition external while
loop.
You can verify that this is a problem, if you write it in a slightly different way:
P ** 2 & gt; Candidates: While producing brakes while the candidate & lt; Million: If the candidate for prime minister in all (candidate)% prime (candidate): primes.append (candidate) candidate + = 2
So now, we are using the same idea Longer version and as soon as we press a prime, p ** 2 & gt; Cancel to run on
is true, in this way, we get momentum with a long-term solution. primes
. Candidate
Comments
Post a Comment