If i were divisible by j (for some 2 < j < i), it couldnt be prime. Then, I'm going to have oupby divide the list into prime and non-prime sequences that we splice onto the appropriate lists: def is_prime(number):įor divisor in range(3, int(number ** 0. 1 if (ij) 0: tests for divisibility of i by j, specifically that i is not divisible by j. The Python way is to use list comprehension: primes i for i in range (1, 9999999) if isPrime (i). When you use a for loop in Python, you are almost surely doing it wrong. It would be better to generate the primes directly with a sieve. First, I recommend you move your trial division code into its own predicate function is_prime(), that returns True or False, so it can be optimized independently of the rest of your code. This is a very inefficient way of calculating a list of primes. ![]() For learning purposes, let's play with a different approach.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |