Algorithm for finding the largest prime number less than x

How to calculate the largest prime smaller than x?

In fact, this does not have to be exact, just approximate and close to x.

x is a 32-bit integer.

The idea is that x is a configuration parameter. I use the largest prime less than x (name it y) as a parameter to the class constructor. The y value must be a prime number.

+6
source share
2 answers

Some useful information here about the function pi (x). Apparently

pi(x) = the number of primes less than x 

and you can approximate pi (x) with

 x/(log x - 1) 

a

 the n-th prime of that list of primes is equal to approximately n(log n) 
+4
source

How fast do you need a program? And how often do you calculate this problem?

If you need fast achievement and don't care about memory. You can generate an increasing prime table by the sieve method, and then hold it for the program life cycle, and then when you need to find β€œthe largest prime number smaller than x,” just find the table and time O (log N) when you may find the exact answer.

+1
source

Source: https://habr.com/ru/post/893038/


All Articles