Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.

Similar presentations


Presentation on theme: "מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large."— Presentation transcript:

1 מבוא מורחב 1 Lecture #6

2 מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large primes Given n such that n=pq it is not known how to efficiently find p and q.

3 מבוא מורחב 3 Public key cryptography Bob Alice Eve

4 מבוא מורחב 4 RSA Rivest, Shamir, Adelman Bob: Pick two very large primes p and q Calculate n=pq, anounce n Calculate a small integer d prime to (p-1)(q-1), anounce d Calculate the unique k such that kd mod (p-1)(q-1) = 1

5 מבוא מורחב 5 RSA (cont) To send a message M (< n) to Bob, Alice computes C=M d (mod n) and sends C Bob upon receiving C calculates C k (mod n) and this equals M!

6 מבוא מורחב 6 High order procedures

7 7 Finding fixed points Find y such that f(y) = y If f(y) = x/y then f(y) = y iff y = x Here’s a common way of finding fixed points Given a guess x 1, let new guess by f(x 1 ) Keep computing f of last guess, till close enough (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) (define tolerance 0.00001)

8 8 Using fixed points (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1) --> 1.6180 or x = 1 + 1/x when x = (1 + 5)/2 (define (sqrt x) (fixed-point (lambda (y) (/ x y)) 1)) Unfortunately if we try (sqrt 2), this oscillates between 1, 2, 1, 2, (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess))

9 מבוא מורחב 9 So damp out the oscillation (define (average-damp f) (lambda (x) (average x (f x)))) Check out the type: (number  number)  (number  number) that is, this takes a procedure as input, and returns a NEW procedure as output!!! ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55 g(x) = [x + f(x)]/2 has the same fixed points

10 מבוא מורחב 10 … which gives us a clean version of sqrt (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Compare this to our previous implementation – same process. (define (cbrt x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1))

11 מבוא מורחב 11 General operations Standard procedures map numbers to numbers square: x --> x*x Functionals map functions to functions (or procedures to procedures) Derivative D:f(x)--> (f(x+dx) - f(x))/dx (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))) (define dx 0.01) (deriv square) (lambda (x) (/ (- (square (+ x dx)) (square x)) dx))

12 מבוא מורחב 12 Newton’s method (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) (define (sqrt x) (newtons-method (lambda (y) (- (square y) x)) 1.0)) A solution to g(x) = 0 is a fixed point of f(x) = x - g(x)/g’(x)

13 מבוא מורחב 13 A brain-twister: composef (define composef (lambda (f g) (lambda (x) (f (g x))))) (define myfunc (composef square double)) (myfunc 3) ==> 36 The value of a lambda expression is a procedure The body of composef is a lambda expression Therefore, the result value of composef is a procedure composef: (A  B), (C  A)  (C  B) (define compose (lambda (f g x) (f (g x))))

14 מבוא מורחב 14 Procedural abstraction Process of procedural abstraction Define formal parameters, capture process in body of procedure Give procedure a name Hide implementation details from user, who just invokes name to apply procedure Input: type Output: type Details of contract for converting input to output procedure

15 מבוא מורחב 15 Language Elements Primitives prim. data: numbers, strings, booleans primitive procedures Means of Combination procedure application compound data Means of Abstraction naming compound procedures –block structure –higher order procedures conventional interfaces – lists data abstraction

16 מבוא מורחב 16 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator of (denom ) Returns the denominator of Constructor: Selectors:

17 מבוא מורחב 17 (define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (sub-rat x y) (make-rat (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) (define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x))))

18 מבוא מורחב 18 Need some guarantees A contract: (numer (make-rat )) = (denom (make-rat )) = Hey, but can’t we have (numer (make-rat 6 8)) = 3 ?? Yes we could if (denom (make-rat 6 8)) = 4

19 מבוא מורחב 19 A better contract (numer (make-rat )) (denom (make-rat )) = How do we do it ?

20 מבוא מורחב 20 Pairs (cons cells) (cons ) ==> ;type: x, x  Pair Where evaluates to a value, and evaluates to a value Returns a pair whose car-part is and whose cdr-part is (car ) ==> ;type: Pair  type of car part Returns the car-part of the pair (cdr ) ==> ;type: Pair  type of cdr part Returns the cdr-part of the pair

21 מבוא מורחב 21 Contract Note how there exists a contract between the constructor and the selectors: (car (cons ))  (cdr (cons ))  Abstraction barrier: We say nothing about the representation or implementation of pairs !

22 מבוא מורחב 22 Compound data Need a way of gluing data elements together into a unit that can be treated as a simple data element Need ways of getting the pieces back out Need a contract between the “glue” and the “unglue” Ideally want the result of this “gluing” to have the property of closure: “the result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object”

23 מבוא מורחב 23 Pair abstraction Note how pairs have the property of closure – we can use the result of a pair as an element of a new pair: (cons (cons 1 2) 3) 32 1

24 מבוא מורחב 24 Another example (define x (cons 1 2)) (define y (cons 3 4)) (define z (cons x y)) (car (car z)) ==> 1 (car (cdr z)) ==> 3

25 מבוא מורחב 25 Back to rational numbers (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x))

26 מבוא מורחב 26 Alternative implementation (define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) Abstraction Violation

27 מבוא מורחב 27 Reducing to lowest terms (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g))))

28 מבוא מורחב 28 Reducing to lowest terms, another way (define (make-rat n d) (cons n d)) (define (numer x) (let ((g (gcd (car x) (cdr x)))) (/ (car x) g))) (define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g)))

29 מבוא מורחב 29 Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons


Download ppt "מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large."

Similar presentations


Ads by Google