Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring...

111
Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven Rudich CS 15-251 Spring 2004 Lecture 3 Jan 20, 2004 Carnegie Mellon University
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    0

Transcript of Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring...

Page 1: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Unary, Binary, and Beyond

Great Theoretical Ideas In Computer Science

Steven Rudich

CS 15-251 Spring 2004

Lecture 3 Jan 20, 2004 Carnegie Mellon University

Page 2: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

We will soon need this fundamental

sum:

The Geometric Series

1 + X1 + X11 + X + X22 + X + X 33 + … + X + … + Xn-2n-2 + X + Xn-1n-1 ==

XXn n – 1 – 1 XX - -

11

Page 3: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

A Frequently Arising Calculation

(X-1) ( 1 + X1 + X2 + X 3 + … + Xn-2 + Xn-1 )

= X1 + X2 + X 3 + … + Xn-1 + Xn

- 1 - X1 - X2 - X 3 - … - Xn-2 – Xn-1

= - 1 + Xn

= Xn - 1

Page 4: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

The Geometric Series

(X-1) ( 1 + X1 + X2 + X 3 + … + Xn-2 + Xn-1 ) = Xn - 1

1 + X1 + X11 + X + X22 + X + X 33 + … + X + … + Xn-2n-2 + X + Xn-1n-1 ==

XXn n – 1 – 1 XX - -

11 when X1

Page 5: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Last time we talked about unary

notation.

Page 6: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

nth Triangular Number

n = 1 + 2 + 3 + . . . + n-1 + n

= n(n+1)/2

Page 7: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

nth Square Number

n = 1 + 3 + … + 2n-1

= Sum of first n odd numbers

Page 8: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

nth Square Number

n = n + n-1

= n2

Page 9: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

(( nn))2 =2 = (( n-1n-1))2 2

++nn

(( nn))2 2 = + + . . . = + + . . . + +

nn

Page 10: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Today:

we will define sequences of

symbols that give us a unary

representation of the Natural numbers.

Page 11: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

First let us define the general

language we use to talk about

strings.

Page 12: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Strings Of Symbols.

We take the idea of symbol and sequence of symbols as primitive.

Let be any fixed finite set of symbols. is called an alphabet, or a set of symbols. Examples:

= {0,1,2,3,4}= {a,b,c,d, …, z}= all typewriter symbols.= { , , , , …, }

Page 13: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Strings over the alphabet

A string is a sequence of symbols from .

Let s and t be strings. Then st denotes the concatenation of s and t; i.e., the string obtained by the string s followed by the string t.

Now define + by these inductive rules: x 2 ) x 2 +

s,t 2 + ) st 2 +

Page 14: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Intuitively, + is the set of all finite strings that we can

make using (at least one) letters

from .

Page 15: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

The set

Define be the empty string. I.e., XY= XY for all strings X and Y.

is also called the string of length 0.

Define 0 = { }

Define * = + [ {}

Page 16: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Intuitively, * is the set of all finite

strings that we can make using letters from including

the empty string.

Page 17: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

The Natural Numbers

= { 0, 1, 2, 3, . . .}

Notice that we include

0 as a Natural number.

Page 18: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

“God Made The Natural Numbers. Everything Else Is The Work Of

Man.”

= { 0, 1, 2, 3, . . .}

Kronecker

Page 19: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Last Time: Unary Notation

1

2

3

4

Page 20: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

To handle the notation for zero, we introduce a

small variation on unary.

Page 21: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Peano Unary (PU)

0 01 S02 SS03 SSS04 SSSS05 SSSSS06 SSSSSS0

Giuseppe Peano [1889]

Page 22: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Each number is a sequence of symbols in {S, 0}+

0 01 S02 SS03 SSS04 SSSS05 SSSSS06 SSSSSS0

Page 23: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Peano Unary Representation of Natural Number

= { 0, S0, SS0, SSS0, . . .}

0 is a natural number called zero.Set notation: 0 2

If X is a natural number, then SX is a natural number called successor of X.Set notation: X 2 ) SX 2

Page 24: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Inductive Definition of “+”

= { 0, S0, SS0, SSS0, . . .}

Inductive definition of addition (+):

X, Y 2 )X + 0 = XX + SY = S(X + Y)

Page 25: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Proving “1+1=2”

Claim: S0 + S0 = SS0

Proof:S0 + S0 = S(S0 + 0) = S(S0) = SS0

X, Y 2 )X + 0 = XX + SY = S(X + Y)

Page 26: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Inductive Definition of “*”

= { 0, S0, SS0, SSS0, . . .}

Inductive definition of times (*):

X, Y 2 )X * 0 = 0X * SY = (X * Y) + X

Page 27: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Inductive Definition of “^”

= { 0, S0, SS0, SSS0, . . .}

Inductive definition of raised to the (^):

X, Y 2 )X ^ 0 = 1 (“X0 = 1”)X ^ SY = (X ^ Y) * X (“XSY = XY *

X”)

Page 28: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

= { 0, S0, SS0, SSS0, . . .}

Defining < for :

8 x,y 2 “x > y” is TRUE “y < x” is FALSE“x > y” is TRUE “y > x” is FALSE

“x+1 > 0” is TRUE“x+1 > y+1” is TRUE ) “x > y” is TRUE

Page 29: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Shortcut:

We will write 2 or 7 instead of SS0 or

SSSSSSS0.

Red numbers are Peano numbers.

Page 30: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

= { 0, 1, 2, 3, . . .}

Defining a partial minus for :

8 x,y 2 :x - 0 = xx > y )

(x+1) – (y+1) = x - y

Page 31: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

a = [a DIV b]*b + [a MOD b]

Defining DIV and MOD for :

8 a,b 2 a<b )

a DIV b = 0 a¸b>0 )

a DIV b = 1 + (a-b) DIV b

a MOD b = a – [b*(a DIV b)]

The maximum number of times b goes into a without going over.

The remainder when a is divided by b.

Page 32: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

45 = [45 DIV 10]*10 + [ 45 MOD 10]= 4*10 + 5

Defining DIV and MOD for :

8 a,b 2 a<b )

a DIV b = 0 a¸b>0 )

a DIV b = 1 + (a-b) DIV b

a MOD b = a – [b*(a DIV b)]

Page 33: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

We have defined the Peano

representation of the Naturals, with a notion of +, *, ^,

<, -, DIV, and MOD.

Page 34: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

PU takes size n+1 to represent the

number n.

Higher bases are much more compact.

Page 35: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

1000000 in Peano Unary takes one

million one symbols to write.

1000000 only takes 7 symbols to write in base 10.

Page 36: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let’s define base 10 representation of PU numbers.

Page 37: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let = {0,

1,2,3,4,5,6,7,8,9} be our symbol

alphabet.

Any string in + will be called a

decimal number.

Page 38: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let X be a decimal number. Let’s define

the length of X inductively:

length() = 0

X= aY, a2 , Y2 * ) length(X) = S(length(Y))

Page 39: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let X be decimal. Let n (unary) =

length(X).

For each unary i<n, we want to be able to talk about the ith symbol of X.

Page 40: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

The ith symbol of X.Defining rule:

X = PaS where P,S2 * and a2.

i=length(S). ) a is the ith symbol of X.

Page 41: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

For any string X, we can define its length n2 PU. For all i2 PU

s.t. i<n, we can define the ith symbol

ai.

X = an-1 an-2 …a0

Page 42: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

We define a base conversion

function, Base10 to 1

(X), to convert any decimal X to its

unary representation.

Page 43: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Initial Cases, length(X)=1

Base10 to 1 (0) = 0

Base10 to 1 (1) = S0

Base10 to 1 (2) = SS0

Base10 to 1 (3) = SSS0

……Base10 to 1 (9) = SSSSSSSSS0

Page 44: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Suppose n=length(X)>S0

For all i<n, let ai be the ith symbol of X.

Hence, X = an-1 an-2 …a0 .

Define Base10 to 1(X) =

i<n Base10 to 1(ai)*10i

where +, *, and ^ are defined over PU

Page 45: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Example X= 238

Base10 to 1 (238) =

2*100 + 3*10 + 8

Page 46: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Base10 to 1 (an-1 an-2 … a0) =

an-1*10n-1 + an-2 * 10n-2 +…+ a0 100

Page 47: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Now we want to go back the other

way.

We want to convert PU to

decimal.

Page 48: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

No Leading Zero:

Let NLZ be the set of decimal

numbers with no leading 0 (leftmost

symbol 0), or the decimal number 0.

Page 49: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

We define Base1 to 10 from PU to NLZ

It will turn out to be the inverse function

to Base10 to 1.

Page 50: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

One digit cases.

Base1 to 10(0) = “0”

Base1 to 10(S0) = “1”

Base1 to 10(SSo) =“2”

….Base1 to 10(SSSSSSSSS0) = “9”

Page 51: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Suppose X > 9

Let n be the smallest unary number such that 10n > X, 10n-1 · X.

Let d = X DIV 10n-1 [Notice that 1· d · 9]Let Y = X MOD 10n-1

Define Base1 to 10(X) 2 NLZ to be

Base1 to 10 (d) Base1 to 10(Y)

Page 52: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

For each n2 PU define its decimal representation to

beBase1 to 10 (n).

Base1 to 10 goes from PU to NLZ.

Page 53: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let’s verify that Base1 to 10 and

Base10 to 1 really are inverse functions.

We need to show X2 NLZ )Base1 to 10 (Base10 to 1 (X)) =

X.

Page 54: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Clear when X is a single digit.

Base1 to 10 (Base10 to 1 (0) ) = 0

Base1 to 10 (Base10 to 1 (1) ) = 1

Base1 to 10 (Base10 to 1 (2) ) = 2

Base1 to 10 (Base10 to 1 (3) ) = 3

Base1 to 10 (Base10 to 1 (4) ) = 4

Base1 to 10 (Base10 to 1 (5) ) = 5

Base1 to 10 (Base10 to 1 (6) ) = 6

Base1 to 10 (Base10 to 1 (7) ) = 7

Base1 to 10 (Base10 to 1 (8) ) = 8

Base1 to 10 (Base10 to 1 (9) ) = 9

Page 55: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Let X be a shortest counter-example to the

statement:

X2 NLZ )

Base1 to 10 (Base10 to 1(Z)) =Z

Page 56: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

For all Y shorter than X:

Y2 NLZ )

Base1 to 10 (Base10 to 1(Y)) = Y

Page 57: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

X2 NLZ, n=length(Z) > 1

Suppose X=an-1 an-2 … a0

Base10 to 1(X) = Y =

i<n Base10 to 1(ai)*10i

Base1 to 10 (Y) = ?

n is smallest PU s.t. 10n-1 · Y < 10n

Calculate d = Y DIV 10n-1, Z= Y MOD 10n-1

d= an-1, Z= i<n-1 Base10 to 1(ai)*10i

Output an-1 Base1 to 10 (Z) [Z shorter than X]

= an-1 an-2 … a1 a0

Contradiction of X being a counter-example.

Page 58: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

No counter-example means that

For all Y2 NLZ,Base1 to 10 (Base10 to 1 (Y)) = Y

Page 59: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

So we can move back and forth between

representing a number in PU or in NLZ.

Each string in PU corresponds to one and only one string in NLZ.

Page 60: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Base X Notation

Let be an alphabet of size X. A base X digit is any element of .

Let S = an-1, an-2, …, a0 be a sequence of base X digits.

Let BaseX to 1(S) = an-1 Xn-1 + … a2X2 + a1X + a0

S is called the base X representation of the number BaseX to 1(S).

Page 61: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

S = an-1, an-2, …, a0

represents the number: an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

Base 2 [Binary Notation]101 represents 1 (2)2 + 0 (21) + 1 (20)

Base 7015 represents 0 (7)2 + 1 (71) + 5 (70)

Page 62: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

S = an-1, an-2, …, a0

represents the number: an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

Base 2 [Binary Notation]101 represents 1 (2)2 + 0 (21) + 1 (20) =

Base 7015 represents 0 (7)2 + 1 (71) + 5 (70)=

Page 63: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Bases In Different Cultures

Sumerian-Babylonian: 10, 60, 360Egyptians: 3, 7, 10, 60Africans: 5, 10French: 10, 20English: 10, 12,20

Page 64: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Biggest n “digit” number in base X would be:

(X-1)Xn-1 + (X-1)Xn-2 +…+ (X-1)X0

Base 2111 represents 1 (2)2 + 1 (21) + 1 (20)

Base 7666 represents 6 (7)2 + 6 (71) + 6 (70)

Page 65: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Biggest n “digit” number in base X would be:

(X-1)Xn-1 + (X-1)Xn-2 +…+ (X-1)X0

Base 2111 represents 1 (2)2 + 1 (21) + 1 (20) 111 + 1 = 1000 represents 23

Thus, 111 represents 23 – 1

Base 7666 represents 6 (7)2 + 6 (71) + 6 (70)666 + 1 = 1000 represents 73

Thus, 666 represents 73 - 1

Page 66: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Biggest n “digit” number in base X would be:

S= (X-1)Xn-1 + (X-1)Xn-2 +…+ (X-1)X0

\Add 1 to get: Xn + 0 Xn-1 + … + 0 X0

Thus, S = Xn - 1

Base 2111 represents 1 (2)2 + 1 (21) + 1 (20) 111 + 1 = 1000 represents 23

Thus, 111 represents 23 – 1

Base 7666 represents 6 (7)2 + 6 (71) + 6 (70)666 + 1 = 1000 represents 73

Thus, 666 represents 73 - 1

Page 67: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Recall theGeometric Series…

1 + X1 + X11 + X + X22 + X + X 33 + … + X + … + Xn-2n-2 + X + Xn-1n-1 ==

XXn n – 1 – 1 XX - -

11

Page 68: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

(X-1)Xn-1 + (X-1)Xn-2 +…+ (X-1)X0

= Xn - 1

Proof:Factoring out (X-1), we obtain:(X-1) [Xn-1 + Xn-2 + ….. + X + 1] =

(X-1) [(Xn – 1)/(X-1)] =

Xn – 1

Page 69: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

The highest n digit number in base X.

(X-1) ( 1 + X1 + X2 + X3 + … + Xn-1 ) = Xn - 1

Base X. Let S be the sequence of n digits each of which is X-1. S is the largest number of length n that can

be represented in base X.

S = XXn n - 1- 1

Page 70: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Each of the numbers from 0 to Xn-1 is uniquely

represented by an n-digit number in base X.

We could prove this using our previous

method, but let’s do it another way.

Page 71: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Our proof introduce an unfamiliar kind of base

representation.

Page 72: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Plus/Minus Base X

An plus/minus base X digit is any integer –X < a < X

Let S = an-1, an-2, …, a0 be a sequence of plus/minus base X digits. S is said to be the plus/minus base X representation of the number:

an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

Page 73: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Does each n-digit number in plus/minus

base X represent a different integer?

Page 74: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

No. Consider plus/minus binary.

5 = 0 1 0 1 = 1 0 -1 -1

Page 75: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Humm.. So what is it good for?

Page 76: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Not every number has a unique plus/minus binary representation, but 0 does.

Page 77: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

0 has a unique n-digit plus/minus base X

representation as all 0’s.

Suppose an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 = S =0, where there is some highest k such that ak 0. Wl.o.g. assume ak > 0.

Because Xk > (X-1)(1 + X1 + X2 + X 3 + … + Xk-1)no sequence of digits from ak-1 to a0 can represent a number as small as –Xk

Hence S 0. Contradiction.

Page 78: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Each of the numbers from 0 to Xn-1 is uniquely represented by an n-

digit number in base X.

We already know that n–digits will represent something between 0 and Xn – 1.

Suppose two distinct sequences represent the same number:

an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 =bn-1 Xn-1 + bn-2 Xn-2 + . . . + b0 X0

The difference of the two would be an plus/minus base X representation of 0, but it would have a non-zero digit. Contradiction.

Page 79: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Each of the numbers from 0 to Xn-1 is uniquely represented by an n-

digit number in base X.

n digits represent up to Xn – 1n-1 digits represents up to Xn-1 - 1

Let k be a number: Xn-1 ≤ k ≤ Xn - 1 So k can be represented with n digits.

For all k: logxk = n – 1

So k uses logxk + 1 digits.

Page 80: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Fundamental Theorem For Base X:

Each of the numbers from 0 to Xn-1 is uniquely represented by an n-

digit number in base X.

k uses logxk + 1 digits in base X.

Page 81: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

n has length n in unary, but has

length log2n + 1 in

binary.

Unary is exponentially longer

than binary.

Page 82: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Multiplication

The Egyptians used decimal numbers but

multiplied and divided in

binary

Page 83: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Multiplication a times bby repeated doubling

b has some n-bit representation: bn..b0

Starting with a, repeatedly double largest so far to obtain: a, 2a, 4a, …., 2na

Sum together all 2ka where bk = 1

Page 84: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Multiplication 15 times 5by repeated doubling

5 has some 3-bit representation: 101

Starting with 15, repeatedly double largest so far to obtain: 15, 30, 60

Sum together all 2k(15) where bk = 1:

15 + 60 = 75

Page 85: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Why does that work?

b = b020 + b121 + b222 + … + bn2n

ab = b020a + b121a + b222a + … + bn2na

If bk is 1 then 2ka is in the sum.

Otherwise that term will be 0.

Page 86: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Wait! How did the Egyptians do the part

where they converted b to binary?

Page 87: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

They used repeated halving to do base conversion. Consider …

Page 88: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Base Conversion

Output stream will print right to left.Input X. Repeat until X=0{

If X is even then Output O; Otherwise {X:=X-1; Output 1}

X:=X/2}

Page 89: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Base Conversion

Output stream will print right to left.Input X. Repeat until X=0{

If X is even then Output O; Otherwise Output 1

X:= X/2}

Page 90: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

010101 1

Page 91: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

01010 1

Page 92: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

01010 01

Page 93: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

0101 01

Page 94: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

0101 101

Page 95: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Start the algorithm

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

010 101

Page 96: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

And Keep Going until 0

Repeat until X=0{ If X is even then Output O;

Otherwise Output 1;X:= X/2

}

0 010101

Page 97: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Sometimes the Egyptian combined the base conversion by halving and the multiplication by doubling into one algorithm

Page 98: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Rhind Papyrus (1650 BC)70*13

70140280560

13 * 7063 * 3501 * 910

Page 99: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Rhind Papyrus (1650 BC)70*13

70140280560

13 * 7063 * 3501 * 910

Binary for 13 is 1101 = 23 + 22 + 20

70*13 = 70*23 + 70*22 + 70*20

Page 100: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Rhind Papyrus (1650 BC)

173468136

1

2 *

4

8 *

184 48 14

Page 101: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Rhind Papyrus (1650 BC)

173468136

1

2 *

4

8 *

184 48 14

184 = 17*8 + 17*2 + 14184/17 = 10 with remainder 14

Page 102: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

This method is called “Egyptian Multiplication/Division” or “Russian Peasant Multiplication/Division”.

Page 103: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Wow. Those Russian peasants were pretty

smart.

Page 104: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Standard Binary Multiplication= Egyptian Multiplication

X101

* * * * * * * *

* * * * * * * * * * * * * * *

** * * * * * * * * * *

Page 105: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Egyptian Base 3

We have definedBase 3: Each digit can be 0, 1, or 2Plus/Minus Base 3 uses -2, -1, 0, 1, 2

Here is a new one:Egyptian Base 3 uses -1, 0, 1

Example: 1 -1 -1 = 9 - 3 - 1 = 5

Page 106: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Unique Representation Theorem for Egyptian Base 3

No integer has 2 distinct, n-digit, Egyptian base-3 representations. We

can represent all integers from -(3n-1)/2 to (3n-1)/2

Proof; If so, their difference would be a non-trivial plus/minus base 3 representation of 0. Contradiction.Highest number = 1111…1 = (3n-1)/2

Lowest number = -1-1-1-1…-1 = -(3n-1)/2

Page 107: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

Unique Representation Theorem for Egyptian Base 3

No integer has 2 distinct, n-digit, Egyptian base-3 representations. We

can represent all integers from -(3n-1)/2 to (3n-1)/2

3n, n-digit, base 3 representations of the numbers from 0 to 3n – 1

Subtract 111111…111 = (3n – 1)/2 from each to get an Egyptian base 3 representation of all the numbers from -(3n-1)/2 to (3n-1)/2.

Page 108: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

How could this be Egyptian? Historically, negative numbers first appear in the writings

of the Hindu mathematician

Brahmagupta (628 AD).

Page 109: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.
Page 110: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

One weight for each power of 3. Left = “negative”. Right =

“positive”

Page 111: Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.

References

The Book Of Numbers, by J. Conway and R. Guy

History of Mathematics, Histories of Problems, by The Inter-IREM Commission