Great question. This has not yet been proved in polynomial time (polynomial in the number of digits, there are 20 here) - it was the β Polymath Project Searchβ , where several mathematicians (including field polarists Terence Tao and Tim Gowers!) Tried to develop an algorithm, but the project, does not seem to have any concrete results yet.
Anyway, there are a few things you can do. One of them, as you and others have noted, is to try each number and check if it is simple, with a quick criterion of strength, for example Miller-Rabin . According to the well-known number-theoretic heuristic (based on the prime number theorem ), the "probability" of a number near n is prime 1 / ln (n), so for 10 ^ 20, approximately every 46 numbers will be first. Therefore, if you want k 20-digit numbers, you will conduct a Miller-Rabin test for approximately 50 thousand numbers.
The second approach, which, in my opinion, can be faster if you do this for many many A (without thinking), should instead use a strong sieve, such as the Eratosthenes sieve . If you want k primes, make an array of about 50 thousand (or more to be safe) and sift them. You should start with a pre-computed list of primes smaller than some number. (10 10 to be completely correct, but since you are ready to endure some compound numbers, making a smaller list of primes, for example, testing the first 50 million primes , ensures that your numbers do not have major factors below 982 451 653, and they are not so much.)
The third approach is to find another implementation for this problem. :-) For example, there is a web page that contains a number, finds the next prime number, or finds the next ten primes . If you use it online, you will need to copy them manually, but the source code is also available.