Блог пользователя oversolver

Автор oversolver, история, 9 лет назад, По-английски

Your given a simple task:

For given number 2 ≤ n ≤ 1018 check prime or not it is.

Limits are: 1 second TL, 16Mb ML.

And we have solution:

bool isprime(LL n){
	if(n<2) return false;
	for(LL i=2;i*i*i<=n;++i) if(n%i==0) return false;
	for(int it=0;it<1e5;++it){
		LL i = rand()%(n-1)+1;
		if(__gcd(i,n)!=1) return false;
		if(mpow(i,n-1,n)!=1) return false;
	}
	return true;
}

where rand() returns 64-bit random integer and mpow(a,b,m) is ab modulo m.

Can you challenge this solution?

  • Проголосовать: нравится
  • +42
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How likely is it that it is gonna say that the number is a prime, when in reality it isn't?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Fails for 999999999999999989

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Link

Line if(mpow(i,n-1,n)!=1) return false; won't never be executed.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

im fake reyna 15241579244017681=123456791^2

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится
bool isprime(LL n){
	if(n<2) return false;
	for(LL i=2;i<1000000;++i) if(n%i==0) return false;
	LL i = rand()%(n-1)+1;
	if(mpow(i,n-1,n)!=1) return false;
	return true;
}

I slightly modified the code and now it performs the power test only once, still I'm quite confident that it's correct.

The only interesting case is N = pq where p, q > 1000000 are large primes. The test fails when in - 1 = 1. However, by Euler's theorem, you know i(p - 1)(q - 1) = 1, and by combining these two equations you get ip + q - 2 = 1. There are at most p + q - 2 such i and the probability that this happens is at most (p + q - 2) / (n - 1) < 0.000002.

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +15 Проголосовать: не нравится

Do you know how to challenge this solution or you wondering whether it possible? It seems to me that this code is correct and 105 iterations is way too much.

Explanation
»
9 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Btw, it's Fermat primality test, with hack to work with Carmichael numbers.

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +10 Проголосовать: не нравится

Just in case someone didn't know about this. Some time ago, collares told me there were small bases of witnesses to perform Miller-Rabin deterministically for values up to 264.

Then I found this. Here you can find such witnesses and perform safely your primality test in O(logn).

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

There is a inbuilt method in JAVA in BigInteger class for prime checking. Is that method slow for prime checking? Should I avoid using that in live competition? { Method is object.isProbablePrime(int certainty))