开发者

Algorithm to find largest prime number smaller than x [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifyi开发者_运维知识库ng this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜