eliteolz.blogg.se

Loop prime number list to 100 python
Loop prime number list to 100 python










loop prime number list to 100 python

You can halve the amount of candidates tested with a simple, one line change. If you think about which candidates are tested, you should realise that this does twice as much work as needed. Recursion is a very unnatural way to do prime detection, especially since you are making multiple copies of the “primeNum” list of primes. If i were divisible by j (for some 2 < j < i), it couldnt be prime. There is no good reason to use recursion here (except maybe to prove it can be done, or to deliberately write an inefficient, obfuscated version). 1 if (ij) 0: tests for divisibility of i by j, specifically that i is not divisible by j. Why do you have a list of primes called “primeNum” as if it was a single prime number?

loop prime number list to 100 python

100 won't definitively tell us whether the number is prime, but it might. Therefore, if we want to generate a list of non-primes under 50 we can do so by generating multiples. A prime number is one that is only divisible by 1 and itself. To begin, we need to think about what a prime number is.

loop prime number list to 100 python

I think you should throw out this code and start again with a better algorithm for checking for prime numbers.Īnd better names for your variables would also help. When num isn't less than 2, we can use the LOW PRIMES list as a shortcut to. 1 First, generate a list of non-primes Generating a list of non-primes is much simpler than generating a list of primes.












Loop prime number list to 100 python