miller rabin testing-资料

上传人:第*** 文档编号:32827272 上传时间:2018-02-12 格式:DOC 页数:10 大小:68KB
返回 下载 相关 举报
miller rabin testing-资料_第1页
第1页 / 共10页
miller rabin testing-资料_第2页
第2页 / 共10页
miller rabin testing-资料_第3页
第3页 / 共10页
miller rabin testing-资料_第4页
第4页 / 共10页
miller rabin testing-资料_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《miller rabin testing-资料》由会员分享,可在线阅读,更多相关《miller rabin testing-资料(10页珍藏版)》请在金锄头文库上搜索。

1、1.2.6 Example: Testing for PrimalityThis section describes two methods for checking the primality of an integer n, one with order of growth , and a probabilistic algorithm with order of growth . The exercises at the end of this section suggest programming projects based on these algorithms. Searchin

2、g for divisors Since ancient times, mathematicians have been fascinated by problems concerning prime numbers, and many people have worked on the problem of determining ways to test if numbers are prime. One way to test if a number is prime is to find the numbers divisors. The following program finds

3、 the smallest integral divisor (greater than 1) of a given number n. It does this in a straightforward way, by testing n for divisibility by successive integers starting with 2. (define (smallest-divisor n)(find-divisor n 2)(define (find-divisor n test-divisor)(cond ( (square test-divisor) n) n)( di

4、vides? test-divisor n) test-divisor)(else (find-divisor n (+ test-divisor 1)(define (divides? a b)(= (remainder b a) 0)We can test whether a number is prime as follows: n is prime if and only if n is its own smallest divisor. (define (prime? n)(= n (smallest-divisor n)The end test for find-divisor i

5、s based on the fact that if n is not prime it must have a divisor less than or equal to . 44 This means that the algorithm need only test divisors between 1 and . Consequently, the number of steps required to identify n as prime will have order of growth . The Fermat test The primality test is based

6、 on a result from number theory known as Fermats Little Theorem. 45 Fermats Little Theorem: If n is a prime number and a is any positive integer less than n, then a raised to the nth power is congruent to a modulo n. (Two numbers are said to be congruent modulo n if they both have the same remainder

7、 when divided by n. The remainder of a number a when divided by n is also referred to as the remainder modulo nmodulo n remainder of a modulo n, or simply as a modulo n.) If n is not prime, then, in general, most of the numbers a n will not satisfy the above relation. This leads to the following alg

8、orithm for testing primality: Given a number n, pick a random number a n and compute the remainder of an 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 another random number a and test it with the same method. If

9、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. To implement the Fermat test, we need a procedure that computes the exponential of a numb

10、er modulo another number: (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)This is very similar to the fast-expt procedure of section 1.2.4. It uses successive squaring, so that the number of steps

11、 grows logarithmically with the exponent. 46 The Fermat test is performed by choosing at random a number a between 1 and n-1 inclusive and checking whether the remainder modulo n of the nth power of a is equal to a. The random number a is chosen using the procedure random, which we assume is include

12、d as a primitive in Scheme. Random returns a nonnegative integer less than its integer input. Hence, to obtain a random number between 1 and n - 1, we call random with an input of n - 1 and add 1 to the result: (define (fermat-test n)(define (try-it a)(= (expmod a n n) a)(try-it (+ 1 (random (- n 1)

13、The following procedure runs the test a given number of times, as specified by a parameter. Its value is true if the test succeeds every time, and false otherwise. (define (fast-prime? n times)(cond (= times 0) true)(fermat-test n) (fast-prime? n (- times 1)(else false)Probabilistic methods The Ferm

14、at 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 tes

15、t, 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. Unfo

16、rtunately, 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. 47 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 intege

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号