Ferma Test for determining prime numbers

Source: Internet
Author: User


; 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,
; 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 ))))



; The first process is slower than the second process, and a value that exceeds a certain length cannot be calculated,
; Because the first is to calculate the factorial first and then take the modulo, and the second is to take the modulo in the factorial process.
; 26 mod 4 and (2 mod 4) * (13 mod 4) have the same results
; (Expmod 32 10911110033 10911110033)
; (My-expmod 32 1091111003 1091111003)


(Define (Fermat-Test N)

(Define (try-It)

(= (Expmod a n) ))

(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 wowould 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.


; 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,
; The chances are better than even that N is prime.
; If n passes the test for two random choices of,
; 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 define Fields

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.