Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Prime Numbers COMPSCI 230 — Discrete Math January 29, 2015 COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 1 / 13 Outline 1 Prime Numbers The Sieve of Eratosthenes Racket Implementation with Lists More on Primes COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 2 / 13 Prime Numbers Primes • Natural numbers greater than 1 with no proper divisors • 1, 2, 3, 6 | 6 but 1, 7 | 7 • Numbers in red are proper divisors • Fundamental theorem of arithmetic: 1 < 𝑛 ∈ 𝐍 ⇒ 𝑛 = ∏ 𝑝𝑖 𝑖 where the 𝑝𝑖 s are prime • Example: 26040 = 23 × 3 × 5 × 7 × 31 • Fundamental in number theory • Applied in cryptography, electrical communications, computer chips,... COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 3 / 13 Prime Numbers Problems About Primes • Generate all primes up to 𝑛 (sieve) • Test whether 𝑛 is prime • Is 802117 prime? • Factor 𝑛 into primes • 802117 = 821 × 977 • You did factor, which subsumes test, so you know both • There are much more efficient methods COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 4 / 13 Prime Numbers The Sieve of Eratosthenes The Sieve of Eratosthenes To generate all primes up to 𝑛: • List all integers from 2 to 𝑛 • Let 𝑝 = 2 (smallest prime) • Cross 2𝑝, 3𝑝, … from the list • First remaining number must be prime • Set 𝑝 to that, and repeat until no more numbers are left COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 5 / 13 Prime Numbers The Sieve of Eratosthenes Example: 𝑛 = 36 2 9 16 23 30 COMPSCI 230 — Discrete Math 3 4 5 6 10 11 12 13 17 18 19 20 24 25 26 27 31 32 33 34 Prime Numbers 7 14 21 28 35 8 15 22 29 36 January 29, 2015 6 / 13 Prime Numbers The Sieve of Eratosthenes The Sieve of Eratosthenes • Speedups exist • Takes large amounts of storage for nontrivial 𝑛 • Each number is touched at least once • Takes at least 𝑛 steps COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 7 / 13 Prime Numbers Racket Implementation with Lists Racket Implementation: Preliminaries (build-list 3 (lambda (x) x)) (cons 1 ’(2 3 4)) (filter odd? ’(1 2 3 4)) (reverse ’(1 2 3)) → → → → (build-list 𝑛𝑎𝑡𝑢𝑟𝑎𝑙 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛) (cons 𝑖𝑡𝑒𝑚 𝑙𝑖𝑠𝑡) (filter 𝑝𝑟𝑒𝑑𝑖𝑐𝑎𝑡𝑒 𝑙𝑖𝑠𝑡) (reverse 𝑙𝑖𝑠𝑡) COMPSCI 230 — Discrete Math Prime Numbers ’(0 ’(1 ’(1 ’(3 → → → → 1 2) 2 3 4) 3) 2 1) 𝑙𝑖𝑠𝑡 𝑙𝑖𝑠𝑡 𝑙𝑖𝑠𝑡 𝑙𝑖𝑠𝑡 January 29, 2015 8 / 13 Prime Numbers Racket Implementation with Lists Implementation Strategy • build-list the integers → numbers • Initially, the list of primes is empty • (process primes numbers) will recursively • (cons (first numbers) primes) • (filter <non-multiples of (first numbers)> numbers) • call itself with primes and the filtered numbers • until (empty? numbers) • and then return primes • reverse the result if desired COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 9 / 13 Prime Numbers Racket Implementation with Lists Racket Code (define (sieve n) (define (process primes numbers) (cond ((empty? numbers) primes) (else (process (cons (first numbers) primes) (filter (lambda (x) (not (= 0 (remainder x (first numbers))))) numbers))))) (reverse (process ’() (build-list (- n 1) (lambda (x) (+ 2 x)))))) COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 10 / 13 Prime Numbers Racket Implementation with Lists Output Sample > (sieve 40) ’(2 3 5 7 11 13 17 19 23 29 31 37) COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 11 / 13 Prime Numbers More on Primes More on Primes • We will use cryptography as an example application of prime numbers • RSA (Ron Rivest, Adi Shamir, Leonard Adleman, 1978) cipher • Used in sending credit card info, encrypted email, file encryption, store passwords, secure shell or ftp, much more • We need a few preliminaries about primes COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 12 / 13 Prime Numbers More on Primes Co-Primes • 𝑚 and 𝑛 are co-primes if they share no • • • • • prime factors Not co-primes: 𝑚 = 24 = 23 × 3 and 𝑛 = 15 = 3 × 5 Co-primes: 𝑚 = 24 = 23 × 3 and 𝑛 = 35 = 5 × 7 Two distinct prime numbers are always co-prime: 𝑚 = 13 and 𝑛 = 2 A prime is co-prime with all the naturals it does not divide into Example: 2 is co-prime with all the odd numbers COMPSCI 230 — Discrete Math Prime Numbers January 29, 2015 13 / 13