How do I calculate the largest prime number smaller than value x?
In actual fact, it doesn't 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'm using the largest prime number less than x (call it y) as the parameter to a class constructor. The value y must be a prime number.
Some good info here on 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)
while
the n-th prime of that list of primes is equal to approximately n(log n)
How fast you need the program runs? And how frequently you calculate this problem?
If you need a fast achievement and don't care about the memory. You can generate an increasing prime table by sieve method then hold it during the program lifetime, and then when you what to find 'the largest prime number smaller than value x', just look up the table and in O(log N) time you can find an exact answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With