Lecture #5 מבוא מורחב.

Slides:



Advertisements
Similar presentations
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Advertisements

Introduction to Modern Cryptography Lecture 6 1. Testing Primitive elements in Z p 2. Primality Testing. 3. Integer Multiplication & Factoring as a One.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
1 Fingerprint 2 Verifying set equality Verifying set equality v String Matching – Rabin-Karp Algorithm.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers Miller-Rabin test.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
9/22/15UB Fall 2015 CSE565: S. Upadhyaya Lec 7.1 CSE565: Computer Security Lecture 7 Number Theory Concepts Shambhu Upadhyaya Computer Science & Eng. University.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
MA/CSSE 473 Day 10 Primality Testing. MA/CSSE 473 Day 10 In-class exam: Friday, Sept 28 –You may bring a two-sided 8.5x11 inch piece of paper containing.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 9 Primality Testing Encryption Intro.
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Lecture #5 מבוא מורחב.
Which program is better? Why?
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
High order procedures Primality testing The RSA cryptosystem
CSE565: Computer Security Lecture 7 Number Theory Concepts
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Randomness and Computation: Some Prime Examples
Cryptography and Network Security
Introduction to Number Theory
Material in the textbook on pages
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
This Lecture Substitution model
CS220 Programming Principles
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Material in the textbook on
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
Lecture #6 section pages pages72-77
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
Lecture # , , , , מבוא מורחב.
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Good programming practices
Presentation transcript:

Lecture #5 מבוא מורחב

Primality Testing (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) מבוא מורחב

Analysis Time complexity (n) – linear There are infinitely many n’s for which it takes c*n time מבוא מורחב

A more efficient test (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) מבוא מורחב

Analysis Time complexity  (n) מבוא מורחב

The Fermat Primality Test Fermat’s little theorem: If n is a prime number then: an mod n = a for every 0 < a < n, integer If n is not a prime, most numbers a < n will not meet the conditions of the theorem. The Fermat Test 1. Pick a random a < n and compute an mod n If  a return not prime If = a chances are good that it is prime. 2. Repeat step 1 מבוא מורחב

A more efficient test (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)))) (define (fermat-test n) (define (try-it a)(= (expmod a n n) a)) (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)))

Some mathematical facts There are composite numbers n that pass the Fermat test, that is an mod n = a for every 0 < a < n, integer They are called Carmichael numbers. The smallest is 561. A slight modification of fast-prime? due to Rabin and Miller can handle every input and return an answer which is correct with very high probability within Q(1) number of tests and Q(log(n)) time . מבוא מורחב

Types (+ 5 10) ==> 15 (+ 5 "hi") ;The object "hi", passed as the second argument to integer-add, is not the correct type Addition is not defined for strings The type of the integer-add procedure is number, number  number two arguments, both numbers result value of integer-add is a number מבוא מורחב

Type examples expression: evaluates to a value of type: 15 number "hi" string square number  number > number,number  boolean (> 5 4) ==> #t 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 מבוא מורחב

Types, precisely A type describes a set of scheme values number  number describes the set: all procedures, whose result is a number, which require one argument that must be a number Every scheme value has a type מבוא מורחב

number, number, number number 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, number, number number Boolean string number

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 Types: a mathematical theory for reasoning efficiently about programs useful for preventing certain common types of errors basis for many analysis and optimization algorithms מבוא מורחב

What is procedure abstraction? Capture a common pattern (* 2 2) (* 57 57) (* k k) Formal parameter for pattern Actual pattern (lambda (x) (* x x)) Give it a name (define square (lambda (x) (* x x))) Note the type: number  number מבוא מורחב

Other common patterns 1 + 2 + … + 100 = (100 * 101)/2 1 + 2 + … + 100 = (100 * 101)/2 1 + 4 + 9 + … + 1002 = (100 * 101 * 201)/6 1 + 1/32 + 1/52 + … + 1/1012 = p2/8 (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)))) (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) מבוא מורחב

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

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

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

Integration as a procedure Integration under a curve f is given roughly by dx (f(a) + f(a + dx) + f(a + 2dx) + … + f(b)) a b dx f (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)))

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

The syntactic sugar “Let” (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) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))

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

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 0.00001)

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

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

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