Docsity

Primality Testing: Understanding Prime Numbers and Fermat's Test, Slides of Data Structures and Algorithms

Partial preview of the text

Download Primality Testing: Understanding Prime Numbers and Fermat's Test and more Slides Data Structures and Algorithms in PDF only on Docsity!

Primality Test

Primality Testing

A Classical Number Theoretic Problem:

Definition: A positive integer n is prime iff its only divisors are 1 and n.

Examples: 7 is a prime. 111 is not a prime.

Is 225593397919 a prime?

Is 2^229-91 a prime? (2^229-91 = 862718293348820473429344482784628181556388621521298319395315527974821 )

Primality Testing: Naïve

n is a prime iff its only divisors are 1 and n

(Prime? 7)

Primality Testing - II

(define (prime? n)

(= n (smallest-divisor n)))

(define (divides? a b) (= (remainder b a) 0)) (define (smallest-divisor n) (define (find-divisor n i) (cond ((> i (sqrt n)) n) ((divides? i n) i) (else (find-divisor n (+ i 1))))) (find-divisor n 2))

Analysis

  • Correctness:

If n is not a prime, then n=a * b for a,b>1.

Then at least one of them is √ n.

So n must have a divisor smaller

then √ n.

  • Time complexity: Θ ( √ n). For a number n, we test at most √ n numbers to see if they divide n.

If n is a 800 digit number, that’s is also very bad. Absolutely infeasible.

2013/4/23 10

Computing ab^ (mod m) fast.

Implementing Fermat test

Some mathematical facts

A fact: If n is not a Carmichael

number then for at least half of the

choices of a, a n^ <> a (mod n).

Fermat’s theorem: Every prime will always pass the test. Definition: A Carmichael number, is a number such that

  • n is Composite, and
  • n always passes the test. For every a, an^ = a (mod n) For example, n=225593397919 is a Carmichael number!.

Correctness

  • If n is a prime we are never wrong.
  • If n is a composite and not a Carmichael number we are wrong with probability at most 2 -^.

Error probability smaller than the chance the hardware is faulty.

Suppose we do the test t=100 times.

  • If n is a Carmichael number, we are always wrong

Fermat’s Little Theorem

  • If n is prime, then a n -1 mod n =
  • for any a such that 1 <= a <= n -
    • n = 3, a = 2 then 2^2 mod 3 =
  • n = 7, a = 4 then (4^6 mod 7) = (4096 mod 7) =

First Prime Number Test

function Fermat (n) a := random integer between 1 and n- if (expomod (a,n-1,n) = 1 then return true // n might be prime else return false // n is not prime

  • Know if n is not prime, but what don’t you know?
  • Why do you get “might be prime”?

Converse of Fermat’s Little Thm.

• Converse of “a then b” might not hold:

  • converse: if b then a

• What is the converse of Fermat’s little theorem?