CPSC 411 Design and Analysis of Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

CPSC 411 Design and Analysis of Algorithms

Description:

{ Extract 10 digit number p at position i. return p if p is prime. i := i 1 ... Drawback: Needs rational arithmetic with long rationals ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 18
Provided by: kla75
Category:

less

Transcript and Presenter's Notes

Title: CPSC 411 Design and Analysis of Algorithms


1
CPSC 411 Design and Analysis of Algorithms
  • Andreas Klappenecker

2
Motivation
3
Eulers Number
4
Billboard Question
5
Strategy
  • Compute the digits of e
  • i 0
  • while true do
  • Extract 10 digit number p at position i
  • return p if p is prime
  • i i1

6
Computing the Digits of e
  • First Approach
  • Drawback Needs rational arithmetic with long
    rationals
  • Too much coding unless a library is used.

7
Extracting Digits of e
  • We can extract the digits of e in base 10
  • by
  • d0 floor(e) (equals
    2)
  • e1 10(e-d0)
  • d1 floor(e1) (equals
    7)
  • e2 10(e1-d1)
  • d2 floor(e2) (equals 1)
  • Unfortunately, e is a transcendental number, so
    there is no pattern to the generation of the
    digits in base 10.
  • Idea Use a mixed-radix representation that leads
    to a more regular pattern of the digits.

8
Mixed Radix Representation
The digits ai are nonnegative integers. The base
of this representation is (1/2,1/3,1/4,). The
representation is called regular if ai is less
than i for i gt1. Number is written as (a0 a1,
a2, a3,)
9
Computing the Digits of e
  • Second approach
  • In mixed radix representation
  • e (21,1,1,1,)

10
Mixed Radix Representations
  • In mixed radix representation
  • (a0 a1, a2, a3,)
  • a0 is the integer part and (0 a1, a2, a3,)
    the fractional part.
  • 10 times the number is (10a0 10a1, 10a2,
    10a3,), but the representation is not regular
    anymore.
  • Renormalize the representation to make it regular
    again
  • The algorithm given for base 10 now becomes
    feasible this is known as the spigot algorithm.

11
Spigot Algorithm
  • define N (1000) / compute N-1 digits of e, by
    brainwagon_at_gmail.com /
  • main( i, j, q )
  • int AN
  • printf("2.")
  • for ( j 0 j lt N j )
  • Aj 1
  • for ( i 0 i lt N - 2 i )
  • q 0
  • for ( j N - 1 j gt 0 )
  • Aj 10 Aj q
  • q Aj / (j 2)
  • Aj (j 2)
  • j--
  • putchar(q 48)

12
Probability to be Prime
  • Let pi(x) of primes less than or equal to x.
  • Prnumber with lt 10 digits is prime
  • pi(99999 99999)/99999 99999
  • 0.045 (roughly)
  • Thus, we need just a few digits of e to find the
    first 10-digit prime number in e.

13
Reinventing the Wheel!
  • Since we will likely need just few digits of
    Eulers number e, there is no need to reinvent
    the wheel.
  • You can use tabulated numbers or
  • you can use tools such as GNU bc
  • to obtain a few hundred digits of e.
  • Know your resources!

14
How do we check Primality?
  • If a number x is not prime, then it has a divisor
    d in the range 2lt d lt sqrt(x).
  • Trial divisions are fast enough here!
  • Simply check whether any number d in the range 2
    lt d lt 100 000 divides a 10-digit chunk of e.

15
Simple Programhttp//discuss.fogcreek.com/joelons
oftware/default.asp?cmdshowixPost160966ixRepli
es23
  • !/bin/sh
  • echo "scale1000 e(1)" bc -l \
  • perl -0777 -ne '
  • s/0-9//g
  • for i (0..length(_)-10)
  • jsubstr(_,i,10)
  • j 0
  • print "i\tj\n" if is_p(j)
  • sub is_p
  • my n shift
  • return 0 if n lt 1
  • return 1 if n lt 3
  • for (2 .. sqrt(n))
  • return 0 unless n _
  • return 1

16
What was it all about?
  • The billboard was an ad paid for by Google. The
    website
  • http//www.7427466391.comcontained another
    challenge and then asked people to submit their
    resume.
  • Algorithmic problems are commonly used by
    companies when evaluating computer science
    candidates.

17
Culture
  • Upcoming CSE Distinguished Lectures
  • Information Visualization for Knowledge
    Discovery,Dr. Ben Shneiderman, University of
    Maryland, 410 p.m., Wed, January 21, 2009, Room
    124, Bright Building
  • Camera Networks for Security Applications Dr.
    Nikolaos Papanikolopoulos, University of
    Minnesota, 410 p.m., Mon, January 26, 2009Room
    124, Bright Building
Write a Comment
User Comments (0)
About PowerShow.com