Download presentation
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 primes Given n such that n=pq it is not known how to efficiently find p and q. מבוא מורחב
3
Public key cryptography
Alice Eve Bob מבוא מורחב
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
RSA (cont) To send a message M (< n) to Bob, Alice computes
C=Md (mod n) and sends C Bob upon receiving C calculates Ck (mod n) and this equals M! מבוא מורחב
6
High order procedures מבוא מורחב
7
Finding fixed points x 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 x1, let new guess by f(x1) 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 )
8
Using fixed points (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1) --> 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
So damp out the oscillation
(define (average-damp f) (lambda (x) (average x (f x)))) g(x) = [x + f(x)]/2 has the same fixed points 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 מבוא מורחב
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) (lambda (y) (/ x (square y)))) מבוא מורחב
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
Newton’s method A solution to g(x) = 0 is a fixed point of
f(x) = x - g(x)/g’(x) (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)) מבוא מורחב
13
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 procedure Output: type Input: type Details of contract for converting input to output מבוא מורחב
14
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 מבוא מורחב
15
The rational number abstraction
Wishful thinking: Constructor: Selectors: (make-rat <n> <d>) Creates a rational number <r> (numer <r>) Returns the numerator of <r> (denom <r>) Returns the denominator of <r> מבוא מורחב
16
(make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x)))
(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)) (define (mul-rat x y) (make-rat (* (numer x) (numer 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)))) מבוא מורחב
17
Need some guarantees A contract:
(numer (make-rat <n> <d>)) = <n> (denom (make-rat <n> <d>)) = <d> Hey, but can’t we have (numer (make-rat 6 8)) = 3 ?? Yes we could if (denom (make-rat 6 8)) = 4 מבוא מורחב
18
A better contract (numer (make-rat <n> <d>)) <n> =
(denom (make-rat <n> <d>)) = <n> <d> How do we do it ? מבוא מורחב
19
Pairs (cons cells) (cons <x-exp> <y-exp>) ==> <P> ;type: x, x Pair Where <x-exp> evaluates to a value <x-val>, and <y-exp> evaluates to a value <y-val> Returns a pair <P> whose car-part is <x-val> and whose cdr-part is <y-val> (car <P>) ==> <x-val> ;type: Pairtype of car part Returns the car-part of the pair <P> (cdr <P>) ==> <y-val> ;type: Pairtype of cdr part Returns the cdr-part of the pair <P> מבוא מורחב
20
Contract Note how there exists a contract between the constructor and the selectors: (car (cons <a> <b> )) <a> (cdr (cons <a> <b> )) <b> Abstraction barrier: We say nothing about the representation or implementation of pairs ! מבוא מורחב
21
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” מבוא מורחב
22
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) 3 2 1 מבוא מורחב
23
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 מבוא מורחב
24
Back to rational numbers
(define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) מבוא מורחב
25
Alternative implementation
(define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) Abstraction Violation (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) מבוא מורחב
26
Reducing to lowest terms
(define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) מבוא מורחב
27
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) (/ (cdr x) g))) מבוא מורחב
28
Programs that use rational numbers
Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons מבוא מורחב
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.