I know that you can use sieve of Eratosthenes for finding all prime numbers in (1,N) interval, but can you suggest algorithm that can do that for (N,M) interval, when N,M are large, for instance 10^9 and N=500,000,000; M=501,000,000.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
I know that you can use sieve of Eratosthenes for finding all prime numbers in (1,N) interval, but can you suggest algorithm that can do that for (N,M) interval, when N,M are large, for instance 10^9 and N=500,000,000; M=501,000,000.
Name |
---|
If the difference between N and M is less (about 10^5), then a modified sieve will work.
You need to cancel out all multiples of 2, 3, 5, and so on upto prime just less than or euqal to sqrt(M).
Are you talking about this problem?
Yes
Hi, you could search about segmented sieve, look this problem http://goo.gl/6nlsFP and its respective editorial http://goo.gl/oAjzCT both resources belong to codeforces user lbv, I hope that this has been helpful.
Find all prime numbers between 1 and sqrt(10^9), and for all numbers between m and n look by sieve, all numbers that divisible for this primes(except this primes) is not prime. Аsymptotic will be O(N(log(log(N)))+(n-m)*mehrunesartem(n-m)). If you want, I can write pseudocode.
I solved it in , where K = M - N
If there's no divisor of X, which is not greater than , then, there's no divisor, which is greater than , so X is prime.
Can you explain how you do it?
I think it will be TLE.
Hmm, then I'm not sure :)
Actually, I think that was O(TKlogM). Half of all numbers divide by 2, 1/3 divide by 3, etc.
I know, that in java there is method n.nextProbablePrime(), that find first prime number after n and it's complexity is like O(n^(1/3)), but I don't know, how it works.
You can use the Miller-Rabin test, which applies Fermat's Little Theorem to test whether a number is prime. So if we're given n, the number we're testing, we write n - 1 = 2ab, and take some integer k and look at kb, k2b, k4b and so on up to k2ab, all modulo p. If we ever have a residue not equal to ± 1 that squares to become 1 modulo p, we know n can't be prime. This is because if p were prime and x2 ≡ 1 modulo p, then x ≡ ± 1 modulo p. More details can be found in the second half of this PDF.
If the numbers we are all testing are less than 4.7 billion, it suffices to test k = 2, 7, 61, according to this page. And for each test, we can do exponentiation by squaring to get all the needed powers of k in time. So each primality test can be done time, which should be fast enough for the problem. (They usually call Rabin-Miller a time algorithm, but I'm pretty sure that's only after considering implementing bignum multiplication, which we don't have to do because we can just use long long's for everything).
Also, look at this.