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

a 夏天 提交于 2019-12-30 06:42:08

问题


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.


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/6741947/algorithm-to-find-largest-prime-number-smaller-than-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!