1 / 32

Primality Testing: Definition, Naïve Solution, and Fermat Test

This text discusses the definition of prime numbers and presents the naive solution for primality testing using the smallest divisor approach. It also introduces the Fermat Test for primality and explains its implementation. Additionally, the text mentions mathematical facts about Carmichael numbers and discusses the correctness and error probability of the Fermat Test.

keppler
Download Presentation

Primality Testing: Definition, Naïve Solution, and Fermat Test

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5 Material in the textbook on pages 50-53 (1.2.6) 56-66 (1.3.1, 1.3.2) Of second edition מבוא מורחב

  2. 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 ) מבוא מורחב

  3. Primality Testing: Naïve Solution (define (prime? n) (= n (smallest-divisor n))) n is a prime iff its only divisors are 1 and n (define (divides? a b) (= (remainder b a) 0)) (define (smallest-divisor n) (define (find-divisor n i) (cond ((divides? i n) i) (else (find-divisor n (+ i 1))))) (find-divisor n 2)) מבוא מורחב

  4. (Prime? 7) (= 7 (smallest-divisor 7)) (= 7 (find-divisor 7 2)) (= 7 (cond (divides? 2 7) 2) (else (find-divisor 7 3)))) (= 7 (find-divisor 7 3)) (= 7 (cond (divides? 3 7) 3) (else (find-divisor 7 4)))) (= 7 (find-divisor 7 4)) (= 7 (find-divisor 7 5)) (= 7 (find-divisor 7 6)) (= 7 (find-divisor 7 7)) (= 7 7) #t מבוא מורחב

  5. Analysis Time complexity: T(n)= (n) – linear in n In fact, for every prime n, the running time is n. If n is a 400 digit number, that’s bad news. Absolutely infeasible. מבוא מורחב

  6. 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)) מבוא מורחב

  7. 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. מבוא מורחב

  8. The Fermat Test: Do 1000 times: Pick a random a < n , and compute an (mod n) If  a then for sure n is not a prime. If all 1000 tests passed, declare that n is a prime. The Fermat Primality Test Fermat’s little theorem: If n is a prime number then: an = a (mod n) for every 0 < a < n, integer מבוא מורחב

  9. Computing ab (mod m) fast. (define (expmod a b m) ; computes ab (mod m) (cond ((= b 0) 1) ((even? b) (remainder (expmod (remainder (* a a) m) (/ b 2) m) m)) (else (remainder (* a (expmod a (- b 1) m)) m))))

  10. Implementing Fermat test (define (one-test n) (define (test a)(= (expmod a n n) a)) (test (+ 1 (random (- n 1))))) (define (many-tests n t); calls one-test t times (cond ((= t 0) true) ((one-test n) (many-test n (- t 1))) (else false))) מבוא מורחב

  11. Time complexity To test if n is a prime. We run 100 tests. Each takes about log(n) multiplcations. T(n) = O(log n) מבוא מורחב

  12. Some mathematical facts A fact: If n is not a Carmichael number then for at least half of the choices of a, an <> 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!. מבוא מורחב

  13. Correctness Suppose we do the test t=100 times. • 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-100 . • Error probability smaller than the chance the hardware • is faulty. • If n is a Carmichael number, we are always wrong מבוא מורחב

  14. A probabilistic algorithm An algorithm that uses random coins, and for every input gives the right answer with a good probability. Even though Carmichael numbers are very rare Fermat test is not good enough. There are inputs on which it is wrong. There are modifications of Fermat’s test, that for every input give the right answer, with a high probability. מבוא מורחב

  15. Procedures as types מבוא מורחב

  16. Types so far • Numbers: 1, 7, 1.2 • Boolean: #t , #f • Strings: “this is a string” • Procedures: (< 2 3), (even? 7), (+ 6 3), (define (f x) (if (< x 0) “x is negative” “x is not negative”)) מבוא מורחב

  17. Procedures have types • A procedure • may have requirements regarding the • number of its arguments, • may expect each argument to be of a certain type. The procedure + expects numbers as its arguments. Can not be applied on strings. The procedure < expects at least one argument. Will not accept strings as arguments. (< “abc” “xyz”) מבוא מורחב

  18. Procedures have types The type of a procedure is a contract: • If the operands have the specified types,the procedure will result in a value of the specified type • otherwise, its behavior is undefined • maybe an error, maybe random behavior מבוא מורחב

  19. number, number number two arguments,both numbers result value of integer-addis a number Example The type of the integer-add procedure is (+ 7 “xx”) - causes an error. מבוא מורחב

  20. number, number, number number Boolean string Your turn • The following expressions evaluate to values of what type?(lambda (a b c) (if (> a 0) (+ b c) (- b c)))(lambda (p) (if p "hi" "bye"))(* 3.14 (* 2 5)) number

  21. Types (summary) • type: a set of values • every value has a type • procedure types (types which include ) indicate • number of arguments required • type of each argument • type of result of the procedure מבוא מורחב

  22. Can procedures get and return procedures? • Can a procedure return a procedure as its value? • Can a procedure get a procedure as an argument? • Can this be useful? מבוא מורחב

  23. Consider the following three sums • 1 + 2 + … + 100 = (100 * 101)/2 • 1 + 4 + 9 + … + 1002 = (100 * 101 * 201)/6 • 1 + 1/32 + 1/52 + … + 1/1012 = p2/8 In mathematics they are all captured by the notion of a sum: מבוא מורחב

  24. (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ 1 a) b)))) (define (sum-squares a b) (if (> a b) 0 (+ (square a) (sum-squares (+ 1 a) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) Let’s have a look at the three programs (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) מבוא מורחב

  25. procedure procedure procedure Let’s check this new procedure out! (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? (number  number, number, number number, number)  number מבוא מורחב

  26. Higher order procedures A higher order procedure: takes a procedure as an argument or returns one as a value Examples: 1. (define (sum-integers1 a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b)) 2. (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b)) 3. (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b)) מבוא מורחב

  27. Does it work? (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (sum square 1 (lambda (x) (+ x 1)) 100) (+ (square 1) (sum square ((lambda (x) (+ x 1)) 1) (lambda (x) (+ x 1)) 100)) (+ 1 (sum square 2 (lambda (x) (+ x 1)) 100)) (+ 1 (+ (square 2) (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (+ 9 (sum square 4 (lambda (x) (+ x 1)) 100))) מבוא מורחב

  28. f dx b a Integration as a procedure Integration under a curve f is given roughly by dx (f(a) + f(a + dx) + f(a + 2dx) + … + f(b)) (define (integral f a b) (* (sum f a (lambda (x) (+ x dx)) b) dx)) (define dx 1.0e-3) (define atan (lambda (a) (integral (lambda (x) (/ 1 (+ 1 (square x)))) 0 a)))

  29. A moment of reflection It is nice that procedures can be treated as any other value. It can help abstract our thinking as with the sum example. Sometime we would actually send a program rather than execute it. E.g., if the data is not under our control. In fact, it happens quite a lot with web (and other highly distributed) settings. It is nice we can send a mobile agent free to Wonder around and execute somewhere else. What does it cost us? מבוא מורחב

  30. The syntactic sugar “Let” Suppose we wish to implement the function f(x,y) = x(1+x*y)2 + y(1-y) + (1+x*y)(1-y) We can also express this as a = 1+x*y b = 1-y f(x,y) = xa2 + yb + ab

  31. The syntactic sugar “Let” (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))

  32. The syntactic sugar “Let” (Let ((<var1> <exp1>) (<var2> <exp2>) .. (<varn> <expn>)) <body>) ((lambda (<var1> ….. <varn>) <body>) <exp1> <exp2> … <expn>)

More Related