Given a number X, estimate where in an ordered list of primes that number may fall

依然范特西╮ 提交于 2019-12-06 05:22:35

The number of primes less than x is approximately the logarithmic integral of x, li(x). Inverting the function* gives a very good estimate of how large the k-th prime is.

If you want to avoid programming the logarithmic integral, a reasonable approximation is

k ln n + k ln ln k - k

After looking at the value at that point in the table, you can estimate the correct position even more accurately by using the density of prime numbers at that point. So if you wanted the millionth prime and estimated it to be 15,502,175 but found that the nearest prime at that point was the 1,001,000-th, you could re-estimate the millionth prime as old estimate - 1000 ln(15502175).

* Technically, the function isn't bijective and hence not invertable, but it's easy enough to invert on the region you care about, say x >= 2.

Correct me if I misunderstood the question but wouldn't simple binary search find the right pair in the ordered list?

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