![]() ![]() ![]() Here is another example using gmpy2 and the Sieve of Eratosthenes to create a table of primes. If you will be searching through primes repeatedly, creating and reusing a table of all primes below a reasonable limit (say 1,000,000) will be faster. This simple function will create a generator that will provide an infinite supply of primes: import gmpy2 The gmpy2 library has a next_prime() function. Sieve.primerange(a, b) # Generate all prime numbers in the range [a, b), implemented as a dynamically growing sieve of Eratosthenes. Nextprime(n) # Return the ith prime greater than n Prevprime(n, ith=1) # Return the largest prime smaller than n The nth prime is approximately n*log(n) and can never be larger than 2**n. Prime(nth) # Return the nth prime, with the primes indexed as prime(1) = 2. Primepi(n) # Return the number of prime numbers less than or equal to n. Randprime(a, b) # Return a random prime number in the range [a, b). ![]() Primerange(a, b) # Generate a list of all prime numbers in the range [a, b). isprime(n) # Test if n is a prime number (True) or not (False). It is a Python library for symbolic mathematics. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |