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

旧城冷巷雨未停 提交于 2020-01-02 10:08:48

问题


Given a pre-calculated ordered list of primes, and a supplied number X, I want to estimate roughly where X would fall in the list of primes, and start searching at that point.

So, I have calculated and stored a list of primes from 1..2^32-1, in a binary file. I've got methods in a program that runs against that file to give me the nth prime, a random prime, how many primes exist, etc. But in order to add a function to this program to tell me where a supplied number is prime, I am having trouble coming up with a way to estimate where to begin searching. Doing it the naïve O(n) method quickly becomes infeasible, even for numbers < 2^32.

I've tried the Prime Number Theorem's (x/ln x), and done research in some other areas, but haven't quite found the right distribution, and I'm afraid my number theory isn't up to par.

I'm looking for something like, e.g.

1 2 3 4  5  6 .. 100 ..  500 .. 1000 ..  5000 ..  10000
2 3 5 7 11 13 .. 541 .. 3571 .. 7919 .. 48611 .. 104729

So, lookup(13) would give me a number near, but <= 6, lookup(7920) would give me a number <= 1000, and lookup(104729) would give a number <= 10000, etc.

P.S. I realize this is a stupid method for several reasons: a) I could store it in a different way and have O(1) lookups; b) I could compress the storage substantially; c) for such small numbers, I could do a prime test on the given number at runtime, skip the lookup table entirely, and it would be faster. I'm not interested in solutions for these issues; I genuinely want to know if there is a proven method of estimating where in a sorted list of primes a given number may fall. Hence, this is more a mathematical/number-theory question than an implementation question.

P.P.S. It's not homework.

P.P.P.S. I did a pretty thorough search on StackOverflow, but may have missed a direct answer to this.

Thank you.


回答1:


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.




回答2:


http://en.wikipedia.org/wiki/Prime_number#Number_of_prime_numbers_below_a_given_number to the rescue.




回答3:


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



来源:https://stackoverflow.com/questions/4709828/given-a-number-x-estimate-where-in-an-ordered-list-of-primes-that-number-may-fa

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