Material in the textbook on

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Computability and Complexity
Primality Testing Patrick Lee 12 July 2003 (updated on 13 July 2003)
Introduction to Modern Cryptography Lecture 6 1. Testing Primitive elements in Z p 2. Primality Testing. 3. Integer Multiplication & Factoring as a One.
COM 5336 Cryptography Lecture 7a Primality Testing
מבוא מורחב 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.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
6.001 SICP – September Introduction
1 Fingerprint 2 Verifying set equality Verifying set equality v String Matching – Rabin-Karp Algorithm.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
CSE 321 Discrete Structures Winter 2008 Lecture 10 Number Theory: Primality.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
RSA Parameter Generation Bob needs to: - find 2 large primes p,q - find e s.t. gcd(e, Á (pq))=1 Good news: - primes are fairly common: there are about.
Prabhas Chongstitvatana 1 Primality Testing Is a given odd integer prime or composite ? No known algorithm can solve this problem with certainty in a reasonable.
CPSC 490 Number Theory Primes, Factoring and Euler Phi-function Mar.31 st, 2006 Sam Chan.
Chapter 14 Randomized algorithms Introduction Las Vegas and Monte Carlo algorithms Randomized Quicksort Randomized selection Testing String Equality Pattern.
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers Miller-Rabin test.
The Complexity of Primality Testing. What is Primality Testing? Testing whether an integer is prime or not. – An integer p is prime if the only integers.
Approximation Algorithms Pages ADVANCED TOPICS IN COMPLEXITY THEORY.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
PRIMES is in P Manindra Agrawal NUS Singapore / IIT Kanpur.
Complexity A decidable problem is computationally solvable. But what resources are needed to solve the problem? –How much time will it require? –How much.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Algorithm Analysis Part of slides are borrowed from UST.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
מבוא מורחב 1 Lecture 4 Material in the textbook on Pages 44-46, of 2nd Edition Sections and Hanoy towers.
CS 312: Algorithm Analysis Lecture #4: Primality Testing, GCD This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
Implementation of Public Key Encryption Algorithms
1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of.
Chapter 1 Algorithms with Numbers. Bases and Logs How many digits does it take to represent the number N >= 0 in base 2? With k digits the largest number.
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.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
A Prime Example CS Lecture 20 A positive integer p  2 is prime if the only positive integers that divide p are 1 and p itself. Positive integers.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Lecture #5 מבוא מורחב.
High order procedures Primality testing The RSA cryptosystem
Probabilistic Algorithms
Handbook of Applied Cryptography - CH4, from 4.1~4.3
Randomness and Computation: Some Prime Examples
MA/CSSE 473 Day 08 Randomized Primality Testing Carmichael Numbers
Introduction to Number Theory
Material in the textbook on pages
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Number Theory (Chapter 7)
Enough Mathematical Appetizers!
Lecture #5 מבוא מורחב.
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Binding names קשירת שמות
CSE 321 Discrete Structures
Patrick Lee 12 July 2003 (updated on 13 July 2003)
Cryptography Lecture 16.
Presentation transcript:

Material in the textbook on Lecture 4 Material in the textbook on Pages 44-46, 50-53 of 2nd Edition Sections 1.2.4 and 1.2.6 + Hanoy towers מבוא מורחב

Review: Recursive Process (define (exp-R a b) ; computes ab (if (= b 0) 1 (* a (exp-R a (- b 1))))) ; ab = a * ab-1 (exp-R 3 4) (* 3 (exp-R 3 3)) (* 3 (* 3 (exp-R 3 2))) (* 3 (* 3 (* 3 (exp-R 3 1)))) (* 3 (* 3 (* 3 (* 3 (exp-R 3 0))))) (* 3 (* 3 (* 3 (* 3 1)))) (* 3 (* 3 (* 3 3))) (* 3 (* 3 9)) (* 3 27) 81 Space b <= R(b) <= b which is Q(b) Time b <= R(b) <= 2b which is Q(b) Linear Recursive Process מבוא מורחב

Review – Iterative process (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (define (exp-I a b) (exp-iter a b 1)) (exp-I 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter 3 1 27) (exp-iter 3 0 81) 81 Space Q(1) Time Q(b) Linear Iterative Process מבוא מורחב

Another algorithm for computing ab If b is even, then ab = (a2)(b/2) If b is odd, then ab = a*a(b-1) Note that here, we reduce the problem in half in one step. (define (exp-fast a b) ; computes ab (cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1))))))) מבוא מורחב

The conditional form (cond (<test-1> <consequent-1>) …. (<test-n> <consequent-n>) (else <consequent-else>)) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) (else (- x)))) ((< x 0) (- x)))) מבוא מורחב

(exp-fast 3 56) (define (exp-fast a b) (cond ((= b 0) 1) ((even? b) (exp-fast (* a a) (/ b 2))) (else (* a (exp-fast a (- b 1))))))) (exp-fast 3 56) ; compute 3^56 (exp-fast 9 28) (exp-fast 81 14) (exp-fast 6561 7) 6561 * (exp-fast 6561 6) 6561 * (exp-fast 43046721 3) 6561 * 43046721 * (exp-fast 43046721 2) 6561 * 43046721 * (exp-fast 1853020188851841 1) 6561 * 43046721 * 1853020188851841 * (exp-fast .. 0) 6561 * 43046721 * 1853020188851841 523347633027360537213511521 מבוא מורחב

How much time does exp-fast take? Denote T(b) the number of arithmetic operations it takes to compute (exp-fast a b). If b is even: T(b) = T(b/2)+2 and if b is odd then: T(b) = T((b-1)/2)+3 T(b) <= T(b/2)+O(1) T(1) = O(1) I’ll show on the board why T(b)=T(b/2)+O(1) implies T(b)=O(log b) Conclusion: T(b)=O(log b) The analysis is tight. The order of growth in time and space is Q(log b) -- logarithmic. מבוא מורחב

Comparing the three exponentiation procedures Assume a,b are integers, written in binary with 400 digits. a = 100101010101010111110100110101…. b = 101001010101011000101001010101…. 2400 <= a,b <= 2401 Time Space exp-R (recursive) Q(b) exp-I (iterative) Q(1) exp-fast Q(log b) מבוא מורחב

Is exp-R feasible? exp-R takes Q(b) space. We need at least 2400 storage bits. That’s about 2370 giga bits. Each gigabit costs a dollar… Never mind. Let’s go to the dealer Sorry, that’s more the number of particles in the universe….. Absolutely infeasible !!!! מבוא מורחב

Is exp-I feasible? exp-I takes at least 2400 operations. We can run 1 billion (109 ) operations a second. We need about 2370 seconds. That’s about 2343 years. That’s about 2340 millenniums. Might be longer then the universe age…. Might be longer than the time our plant will last…. Infeasible !!!! מבוא מורחב

Let’s buy a faster computer and make exp-I feasible. Our new computer can run giga billion (1018 ) operations a second. Absolutely the last word in the field of computing. We need about 2340 seconds. That’s about 2313 years. That’s about 2310 millenniums. Does not help much. Infeasible !!!! מבוא מורחב

Exp-fast is feasible. We use a first generation pc, manufactured at 1977 and executing one operation a second. We need about 1200 operations. That’s about 20 minutes. We need 1200 storage bits. Feasible !!!! מבוא מורחב

Let’s buy a faster computer.. We use a second generation pc, manufactured at 1987 and executing one million operations a second. We need about 1200 operations. That’s so much less than a second that we do not bother counting it. We still need 1200 storage bits. Very feasible !!!! מבוא מורחב

Towers of Hanoi Three posts, and a set of different size disks A disk can be only on a larger size disk. At the beginning all the disks are on the left post. The goal is to move the disks one at a time, while preserving these conditions, until the entire stack has moved from one post to another מבוא מורחב

Use our paradigm Wishful thinking: Smaller problem: A problem with one disk less How do we use it ? Move n-1 disks from peg A to peg B Move the largest from peg A to peg C Move n-1 disks from peg B to peg C We solve 2 smaller problems ! מבוא מורחב

Towers of Hanoi (define (move-tower size from to aux) (cond ((= size 1) (one-move from to)) (else (move-tower (- size 1) from aux to) (one-move from to) (move-tower (- size 1) aux to from)))) (define (one-move from to) (display "Move top disk from ") (display from) (display " To ") (display to) (newline)) מבוא מורחב

Towers of Hanoi -- trace (move-tower 3 2 1 3) Move top disk from 2 to 1 Move top disk from 2 to 3 Move top disk from 1 to 3 Move top disk from 3 to 2 Move top disk from 3 to 1 (move-tower 2 2 3 1) (move-tower 2 3 1 2) 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 מבוא מורחב

Tree Recursion (mt 3 2 1 3) (mt 2 2 3 1) (mt 2 3 1 2) (move-one 2 1) מבוא מורחב

Orders of growth for towers of Hanoi Denote by T(n) be the number of steps that we need to take to solve the case for n disks. T(n) = 2T(n-1) + 1 T(1) = 1 This solves to: T(n) = 2n - 1 exponential For the space complexity we have S(n) = S(n-1) + O(1) S(n) = O(n) מבוא מורחב

Moral If you wish to see the result Of your programming efforts Better think ahead What your algorithm Is מבוא מורחב

n is a prime iff its only divisors are 1 and n Primality Testing 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)) (define (prime? n) (= n (smallest-divisor n))) מבוא מורחב

(= 7 (smallest-divisor 7)) (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 מבוא מורחב

Analysis Time complexity: T(n) = T(n-1) + O(1) 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 very bad. Absolutely infeasible. מבוא מורחב

Primality Testing - II n is a prime iff its only divisors are 1 and n Iff it has no divisors between 2 and (sqrt 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)) (define (prime? n) (= n (smallest-divisor n))) מבוא מורחב

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 very bad. Absolutely infeasible. מבוא מורחב

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

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

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

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

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

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

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