当前位置:网站首页>Rabin Miller prime test
Rabin Miller prime test
2022-06-11 07:36:00 【CaptainHarryChen】
This paper does not prove the correct probability , Because I won't
Determine whether large integers are prime numbers
Premise theorem
- Fermat's small Theorem : if n n Prime number ,, be an−1≡1 (mod n) a n − 1 ≡ 1 ( m o d n ) ; If any integer x x Satisfy , be x x There is a high probability that it is a prime number ;
- Theorem 2: if For more than 2 The prime number of , be a2≡1 (mod p) a 2 ≡ 1 ( m o d p ) The solution of can only be a≡1 a ≡ 1 or a≡−1 a ≡ − 1 ;
Rabin-Miller Prime number test
If the number to be measured n n For more than 2 The prime number of , be For the even
set up n−1=2s×d n − 1 = 2 s × d
Random one less than n n The integer of
a2s×d≡1 (mod n) a 2 s × d ≡ 1 ( m o d n )
(a2s−1×d)2≡1 (mod n) ( a 2 s − 1 × d ) 2 ≡ 1 ( m o d n )
a2s−1×d≡1 (mod n) a 2 s − 1 × d ≡ 1 ( m o d n ) or a2s−1×d≡−1 (mod n) a 2 s − 1 × d ≡ − 1 ( m o d n )
if 1, You can also continue recursion , Until get -1 Or get ad≡±1 (mod n) a d ≡ ± 1 ( m o d n )
The implementation , Turn it upside down
First, calculate ad a d , Judge whether it is ±1 ± 1
And then keep squaring , obtain a2×d,a22×d,a23×d...a2s×d a 2 × d , a 2 2 × d , a 2 3 × d . . . a 2 s × d
Until one step in the middle gets −1 − 1 , Pass the test .
If you get -1 Before , Got it 1, It means that the number is not equal to ±1 The square of the number of gets 1, Not satisfied with the theorem 2, Then the number test fails
Generally, you have to pick a lot at random a a , Test many times to improve the probability .
But in OI in , Use this string a That's it
It guarantees 3,825,123,056,546,413,051 3 , 825 , 123 , 056 , 546 , 413 , 051 The number within is calculated correctly ( Has more than long long)
Code
Edition title :HDU2138
#include<cstdio>
const int TEST_VAL[]={
2,3,5,7,11,13,17,19,23,29,31,37,41},TEST_NUM=13;
long long PowMod(long long a,long long b,long long p)
{
long long res=1LL;
while(b)
{
if(b&1LL)
res=1LL*res*a%p;
a=1LL*a*a%p;
b>>=1LL;
}
return res;
}
bool MillerRabin(long long a,long long n,long long d)
{
long long x=PowMod(a,d,n);
if(x==1||x==n-1)
return true;
while(d<n-1)
{
x=1LL*x*x%n;
d=d*2LL;
if(x==1)
return false;
if(x==n-1)
return true;
}
return false;
}
bool isPrime(long long x)
{
if(x<10)
{
if(x==2||x==3||x==5||x==7)
return true;
return false;
}
long long d=x-1;
while((d&1LL)==0)
d>>=1LL;
for(int i=0;i<TEST_NUM;i++)
if(TEST_VAL[i]<x&&!MillerRabin(TEST_VAL[i],x,d))
return false;
return true;
}
int main()
{
int n,ans;
long long x;
while(scanf("%d",&n)!=EOF)
{
ans=0;
while(n--)
{
scanf("%I64d",&x);
ans+=isPrime(x);
}
printf("%d\n",ans);
}
return 0;
}边栏推荐
- 欧拉定理及扩展(附证明)
- Miscellany C language
- Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
- Implementation of stack (C language)
- 测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
- 20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
- Bidirectional linked list simple template (pointer version)
- [analysis of STL source code] summary notes (3): vector introduction
- MFC debugger OutputDebugString override
- Zero foundation self-study SQL course | outer join external connection
猜你喜欢

May 30-June 5, 2022 AI industry weekly (issue 100): three years
![[STL source code analysis] summary notes (12): functors and adapters](/img/6d/a3a9cde2c8792579af7505c2226914.jpg)
[STL source code analysis] summary notes (12): functors and adapters

Zero foundation self-study SQL course | outer join external connection

【IoT】项目管理:如何打造更好的跨职能团队?

C memory alignment

Simple configuration of vscade

Multi thread review summary parsing volatile keyword

10 advanced concepts that must be understood in learning SQL

【AtCoder1980】Mysterious Light(数学模拟)

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
随机推荐
Gobang interface of mobile console (C language)
Qstring to hexadecimal qstring
C language to achieve minesweeping games
[STL source code analysis] summary note (2): overview of containers
Sdl-2 thread logic
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
【软件测试】这样的简历已经刷掉了90%的面试者
20200810 T2 dispatch money
I/o multiplexing - select/poll/epoll
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
【HDU6357】Hills And Valleys(DP)
Classes and objects (medium)
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
【Oracle 数据库】奶妈式教程day03 排序查询
Compound ratemodel contract analysis
[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode
C language inherits memory management mechanism (unfinished)
RTMP protocol