素数判定 费马测试

;; Fermat‘s Little Theorem:

;; If N is a prime number and A is any positive integer less than N, 

;; then A raised to the N-th power is congruent to A modulo N

;; Two numbers are said to be congruent modulo N if they both

;; have the same remainder when divided by N

;; The Fermat test:

;; Given a number N, pick a random number A < N

;; and compute the remainder of A^N modulo N.

;; If the result is not equal to A, then N is certainly not prime

;; If it is A, then chances are good that N is prime.

;; Now pick anthor random number A and test it with the same method.

;; If it also satisfies the equation,

;; then we can be even more confident that N is prime.

;; By trying more and more values of A,

;; We can increase our confidence in the result.

;; This algorithm is known as the Fermat test.

( define ( bad-exp base exp )

( cond  ( ( = exp 0 ) 1 )

( ( even? exp )

( square ( bad-exp base ( / exp 2 ) ) ) )

( else ( * base ( bad-exp base ( - exp 1 ) ) ) ) ) )

( define ( bad-expmod base exp m )

( remainder ( bad-exp base exp ) m ) )

( define ( expmod base exp m )

( cond  ( ( = exp 0 ) 1 )

( ( even? exp )

( remainder 

( square ( expmod base ( / exp 2 ) m ) ) m ) )

( else 

( remainder 

( * base ( expmod base ( - exp 1 ) m ) ) m ) ) ) )

;; 第一个过程比第二个过程慢,且超过一定长度的数值就不能计算,

;; 因为第一个是先将阶乘算出来,再取模,而第二个是在阶乘的过程中就取模

;; 26 mod 4 和 ( 2 mod 4 ) * ( 13 mod 4 ) 结果一样

;; ( expmod 32 10911110033 10911110033 )

;; ( my-expmod 32 1091111003 1091111003 )

( define ( fermat-test n )

( define ( try-it a )

( = ( expmod a n n ) a ) )

( try-it ( + 1 ( random ( - n 1 ) ) ) ) )

( define ( fast-prime? n times )

( cond  ( ( = times 0 ) true )

( ( fermat-test n ) 

( fast-prime? n ( - times 1 ) ) )

( else false ) ) )

;; The Fermat test differs in character from most familiar algorithms,

;; in which one computes an answer that is guaranteed to be correct.

;; Here, the answer obtained is only probably correct.

;; More precisely, if N ever fails the Fermat test,

;; we can be certain that N is not prime.

;; But the fact that N passes the test,

;; while an extremely strong indication,

;; is still not a guarantee that N is prime.

;; What we would like to say is that for any number N,

;; if we perform the test enough times and find that N always passes the test,

;; then the probability of error in our primality test can be made as small as we like.

;; Unfortunately, this assertion is not quite correct.

;; There do exist numbers that fool the Fermat test:

;; numbers N that are not prime and yet have the property that

;; an is congruent to a modulo n for all integers A < N.

;; Such numbers are extremely rare,

;; so the Fermat test is quite reliable in practice

;; There are variations of the Fermat test that cannot be fooled.

;; In these tests, as with the Fermat method,

;; one tests the primality of an integer N

;; by choosing a random integer A < N and

;; checking some condition that depends upon N and A.

;; On the other hand, in contrast to the Fermat test,

;; one can prove that, for any N,

;; the condition does not hold for most of the integers A < N

;; unless N is prime.

;; Thus, if N passes the test for some random choice of A,

;; the chances are better than even that N is prime.

;; If N passes the test for two random choices of A,

;; the chances are better than 3 out of 4 that N is prime.

;; By running the test with more and more randomly chosen

;; values of A we can make the probability of error as small as we like.

;; The existence of tests for which one can prove that

;; the chance of error becomes arbitrarily small has sparked interest in

;; algorithms of this type,

;; which have come to be known as probabilistic algorithms.

;; There is a great deal of research activity in this area,

;; and probabilistic algorithms have been fruitfully applied to many fields

素数判定 费马测试

时间: 2025-01-04 23:13:20

素数判定 费马测试的相关文章

素数判定(米勒测试定理-费马小定理+快速乘)

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<vector> 7 #include<ctime> 8 #define llg long long 9 llg i,j,k,x,n,m; 10 using namespace std; 11 ll

HDU 4344 随机法判素数(费马小定理

#include <cstdio> #include <ctime> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int N = 108; const int S = 10; ll mult_mod(ll a, ll b, ll c) { a %= c; b %= c; ll ret = 0; while(b) { if(b&am

利用辛达拉姆筛进行素数判定

1 常规判定方法 素数判定问题就是对给定的正整数n判定是否为素数.所谓素数,是指恰好有2个约数的整数.因为n的约数都不超过n,所以只需要检查2~n-1的所有整数是否整除n就能判定是不是素数.不过,我们还能进一步优化.如果d是n的约数,那么n/d也是n的约数.由n=d*n/d可知min(d,n/d),所以只需要检查2~的所有整数就足够了.此时,素数判定的复杂度为O().代码实现如下: int classic(long long a) { long long half=(long long)sqrt

HDU 1098 Ignatius&#39;s puzzle 费马小定理+扩展欧几里德算法

题目大意: 给定k,找到一个满足的a使任意的x都满足 f(x)=5*x^13+13*x^5+k*a*x 被65整除 推证: f(x) = (5*x^12 + 13 * x^4 + ak) * x 因为x可以任意取 那么不能总是满足 65|x 那么必须是 65 | (5*x^12 + 13 * x^4 + ak) 那么就是说 x^12 / 13 + x^4 / 5 + ak / 65 正好是一个整数 假设能找到满足的a , 那么将 ak / 65 分进x^12 / 13 + x^4 / 5中得到

费马小定理是数论的基础理论之一

费马小定理 关于费马小定理,读到注解的时候,还是有点震撼的. 皮埃尔•得•费马(1601-1665)是现代数论的奠基人,他得出了许多有关数论的重要理论结果,但他通常只是通告这些结果,而没有提供证明.费马小定理是在1640年他所写的一封信里提到的,公开发表的第一个证明由欧拉在1736年给出(更早一些,同样的证明也出现在莱布尼茨的未发表的手稿中)费马的最著名结果——称为费马的最后定理——是l637年草草写在他所读的书籍<算术>里(3世纪希腊数学家丢番图所著),还带有一句注释“我已经发现了一个极其美

Miller-Rabin算法 codevs 1702 素数判定 2

转载自:http://www.dxmtb.com/blog/miller-rabbin/ 普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(slog³n)的算法. 定理一:假如p是质数,且(a,p)=1,那么a^(p-1)≡1(mod p).即假如p是质数,且a,p互质,那么a的(p-1)次方除以p的余数恒等于1.(费马小定理) 该定理的逆命题是不一定成立的,但是令人可喜的是大多数情况是成立的. 于是我们就得到了一个定理的直接应用,对于待验证的数p,我们不断取a∈[1,p-1]且a∈

大素数判定

START 判断一个数是不是素数可以直接暴力或者是素数筛. 但是对于一个特别大的数,直接用素数筛也有可能TLE. 这个时候就要想点别的办法: 1. 筛选法+试除法 首先用素数筛筛出[2,sqrt(n)+1]的素数,然后用这些素数来判断能不能整除n,如果可以,那么n一定是合数,如果都不行,那么n是素数. void olas()//欧拉筛 { int i,j; num=1; memset(u,true,sizeof(u)); for(int i=2;i<=1000000;i++) { if(u[i]

[Miller-Rabin][CODEVS1702]素数判定2 解题报告

题面描述:判定一个数P∈[1,2^63-1]∩N是素数么. 按照朴素的判定素数方法,至少也需要O(P^0.5)的,但这道题就是霸气到连这样的时间复杂度都过不了的地步. 实在是不会做了,就学习了传说中的Miller-Rabin素数判定法. 两个引理: ①费马小定理: 设p为质数,且不满足p|a, 则a^(p-1)=a(mod p). 证: 又一个引理,若n与p互质,且a与p互质,则n*a与p互质. 这真的是一个看似很简单的引理,但它却意味着一些看似不那么简单的事情. 设A=(0,p)∩N,则 ①对

POJ 1811 Prime Test(费马小定理+二次探测定理)

素数的测试: 费尔马小定理:如果p是一个素数,且0<a<p,则a^(p-1)%p=1. 利用费尔马小定理,对于给定的整数n,可以设计素数判定算法,通过 计算d=a^(n-1)%n来判断n的素性,当d!=1时,n肯定不是素数,当d=1时,n   很可能是素数. 二次探测定理:如果n是一个素数,且0<x<p,则方程x^2%p=1的解为:x=1或    x=p-1. 利用二次探测定理,可以再利用费尔马小定理计算a^(n-1)%n的过程 中增加对整数n的二次探测,一旦发现违背二次探测条件,