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

佐手、 提交于 2019-11-30 20:52:41

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.

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