首页 > 其他分享 >Miller-Rabin primality test

Miller-Rabin primality test

时间:2023-03-20 09:22:28浏览次数:45  
标签:prime Miller ll test primality find equiv

Assume that we are asked if \(n\) is a prime.

Fermat primality test: choose some numbers \(a_1,a_2,\cdots,a_x<n\) and check if \(\forall i,a_i^{n-1}\equiv 1\pmod n\).

However, this may get wrong answer on some special numbers such as \(561\).

Here is a theorem: if \(p\) is an odd prime, there are only two solutions of \(x^2\equiv 1\pmod p\), one is \(x\equiv 1\), the other is \(x\equiv p-1\).

Prove: the equation can be transformed into \((x+1)(x-1)\equiv 0\). Since \(p\) is an odd prime, you cannot find two positive integers less than \(p\) of which product is a multiple of \(p\), aka. either \(x+1\equiv 0\) or \(x-1\equiv 0\) is true.

So, If you find a \(x\) satisfying \(x^2\equiv 1\pmod n\) and \(x\not\equiv\pm 1\pmod n\), \(n\) cannot be a prime.

Miller-Rabin primality test performs both the above-mentioned process and Fermat primality test to determine whether \(n\) is a prime.

First, find the biggest integer \(t\) makes \(n-1=u\cdot 2^t\) (\(u\) is a integer). Then test several times, each time choose a integer \(a\) which is smaller than \(n\). Let \(v\) be \(a^u\bmod n\). Transform \(v\) into \(v^2\bmod n\) for \(t\) times. If \(n\) is an odd prime, either \(v\) is \(1\) at first or there is a moment \(v=n-1\).

typedef long long ll;
bool isPrime(ll n){
	if((n&1)^1)return n==2;
	ll u=n-1;int t=0;
	while((u&1)^1)u>>=1,t++;
	for(ll a:TestBase){
		a%=n;ll v=fpow(a,u,n);
		if(v==1||a==0)continue;
		int k=0;for(;k<t;k++){
			if(v==n-1)break;
			v=(__int128)v*v%n;
		}
		if(k==t)return 0;
	}
	return 1;
}

If you choose the first \(12\) primes as \(a\) and \(n\in[1,2^{64})\), it can absolutely determine whether \(n\) is a prime.

标签:prime,Miller,ll,test,primality,find,equiv
From: https://www.cnblogs.com/hihihi198/p/17235172.html

相关文章

  • AtCoder Beginner Contest 294
    A-Filter(abc294a)题目大意给定一个数组,不改变原顺序,输出是偶数的数。解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL......
  • 【单元测试】Junit 4(七)--junit4 TestRunnner
    TestRunners我没想到一个特别合适的词来形容TestRunners的作用,所以多说几句:TestRunners是具有特殊功能的执行测试用例的通道,也可以理解为测试的执行者,例如可以同时运......
  • test
    暂无TRANSLATEwithxEnglishArabicHebrewPolishBulgarianHindiPortugueseCatalanHmongDawRomanianChineseSimplifiedHungarianRussian......
  • AtCoder Beginner Contest 293
    A-SwapOddandEven#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ strings; cin>>s; for(inti=0;i+1<s.size();i+=2) swap(......
  • pytest用例的执行顺序
    1.默认是从上往下setup_module->setup_claas->setup_function->testcase->teardown_function->teardown_claas->teardown_module1)文件之间按照名称的ASCLL码从小到大排序......
  • pytest 获取帮助信息
     查看帮忙信息F:\PycharmProjects\djangotest>pytest--h查看版本号F:\PycharmProjects\djangotest>pytest--version查看mark相关功能F:\PycharmProjects\dja......
  • keymaster 4.0 VTS测试之HmacKeySharingTest
    ./VtsHalKeymasterV4_0TargetTest--gtest_filter=PerInstance/HmacKeySharingTest.GetParameters/0_default#./VtsHalKeymasterV4_0TargetTest--gtest_filter=PerInsta......
  • 2023 ICPC香港区域赛(UCup) D Shortest Path Query
    啊对对对,下次题解写详细一点好不好。首先考虑naive的\(O(n^2)\),记\(dp[i][j]\)表示从\(1\)走到\(i\),恰好走了\(j\)条黑边的时候走过白边的最少数量。\(O(nm)\)......
  • python单元测试unittest
    快速上手#被测代码defadd_func(a,b):returna+b#测试代码importunittestclassMyTest(unittest.TestCase):deftest_add_func(self):#......
  • PentestLab-web安全SQL注入-EXP6
    我们打开靶机,选择“SQL Injections”选择“Example6”观察页面我们使用工具测试参数为-u"http://192.168.29.148/sqli/example6.php?id=2"--dumpall开始测试没有发现我......