I want to calculate prime number just below a number. How can this be done efficiently.
I used Sieve of Eratosthenes but it failed as my numbers are in range 10^20
Any other algorithm??
I want to calculate prime number just below a number. How can this be done efficiently.
I used Sieve of Eratosthenes but it failed as my numbers are in range 10^20
Any other algorithm??
The chance of a random 20-digit number being prime is approximately 1/20 (source). If you want the largest prime below x, start at x-1 and run a primality test on each number, working your way down until you find a prime. The following is a related answer I gave and lists a highly reliable and extremely fast pseudoprime test that should suffice:
Use a segment of a sieve for sufficiently many numbers just below n
so that you can be reasonable sure it contains a prime (say a couple of thousand numbers). Then sieve with small primes to remove most of the non-primes. Use a full primality test on the remaining numbers (either probabilistic, or for 20 digits trial division up to the root might still be reasonable).
How many small primes you use for sieving depends on the relative cost of sieving versus running the full primality test.