unary and binary

87
Unary and Binary Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2005 Lecture 4 Sept 8, 2005 Carnegie Mellon University

Upload: ping

Post on 12-Jan-2016

56 views

Category:

Documents


1 download

DESCRIPTION

Unary and Binary. Your Ancient Heritage Let’s take a historical view on abstract representations. Mathematical Prehistory: 30,000 BC. Paleolithic peoples in Europe record unary numbers on bones. 1 represented by 1 mark 2 represented by 2 marks 3 represented by 3 marks - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unary and Binary

Unary and Binary

Great Theoretical Ideas In Computer Science

Anupam Gupta

CS 15-251 Fall 2005

Lecture 4 Sept 8, 2005 Carnegie Mellon University

Page 2: Unary and Binary

Your Ancient Heritage

Let’s take a historical view on

abstract representations.

Page 3: Unary and Binary

Mathematical Prehistory:30,000 BC

Paleolithic peoples in Europe record unary numbers on bones.

1 represented by 1 mark2 represented by 2 marks3 represented by 3 marks4 represented by 4 marks…

Page 4: Unary and Binary

Prehistoric Unary

1

2

3

4

Page 5: Unary and Binary

PowerPoint Unary

1

2

3

4

Page 6: Unary and Binary

Hang on a minute!

Isn’t unary a bit literal as a representation? Does it deserve to be

viewed as an “abstract” representation?

Page 7: Unary and Binary

In fact, it is important to respect the status of each representation, no matter

how primitive.

Unary is a perfect object lesson.

Page 8: Unary and Binary

Consider the problem of finding a formula for the sum of the first n

numbers.

We already used induction to verify that the answer is ½n(n+1)

Page 9: Unary and Binary

Consider the problem of finding a formula for the sum of the first n

numbers.

First, we will give the standard high school

algebra proof…

Page 10: Unary and Binary

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

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

2

1)(n n S

Page 11: Unary and Binary

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

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Let’s restate this argument using a UNARY representation

Algebraic argument

Page 12: Unary and Binary

1 2 . . . . . . . . n

= number of white dots.1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Page 13: Unary and Binary

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

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

1 2 . . . . . . . . n

= number of white dots

= number of yellow dots

n . . . . . . . 2 1

Page 14: Unary and Binary

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

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

n+1 n+1 n+1 n+1 n+1

= number of white dots

= number of yellow dots

n

n

n

n

n

n

There are n(n+1) dots in the grid

Page 15: Unary and Binary

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

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

n+1 n+1 n+1 n+1 n+1

= number of white dots

= number of yellow dots

n

n

n

n

n

n

2

1)(n n S

Page 16: Unary and Binary

Very convincing! The unary representation

brings out the geometry of the

problem and makes each step look very

natural.

By the way, my name is Bonzo. And you are?

Page 17: Unary and Binary

Odette.

Yes, Bonzo. Let’s take it

even further…

Page 18: Unary and Binary

nth Triangular Number

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

= n(n+1)/2

Page 19: Unary and Binary

nth Square Number

n = n + n-1

= n2

Page 20: Unary and Binary

Breaking a square up in a new way.

Page 21: Unary and Binary

Breaking a square up in a new way.

1

Page 22: Unary and Binary

Breaking a square up in a new way.

1 + 3

Page 23: Unary and Binary

Breaking a square up in a new way.

1 + 3 + 5

Page 24: Unary and Binary

Breaking a square up in a new way.

1 + 3 + 5 + 7

Page 25: Unary and Binary

Breaking a square up in a new way.

1 + 3 + 5 + 7 + 9

Page 26: Unary and Binary

The sum of the first 5 odd numbers is 5 squared

1 + 3 + 5 + 7 + 9 = 52

Page 27: Unary and Binary

The sum of the first n odd

numbers is n squared.

Pythagoras

Page 28: Unary and Binary

Here is an alternative dot

proof of the same sum….

Page 29: Unary and Binary

nth Square Number

n = n + n-1

= n2

Page 30: Unary and Binary

nth Square Number

n = n + n-1

= n2

Page 31: Unary and Binary

Look at the columns!

n = n + n-1

Page 32: Unary and Binary

Look at the columns!

n = n + n-1

= Sum of first n odd numbers.

Page 33: Unary and Binary

High School Notation

n + n-1 =

1 + 2 + 3 + 4 ...

+ 1 + 2 + 3 + 4 + 5 ...

1 + 3 + 5 + 7 + 9 …

Sum of odd numbers

Page 34: Unary and Binary

High School Notation

n + n-1 =

1 + 2 + 3 + 4 ...

+ 1 + 2 + 3 + 4 + 5 ...

1 + 3 + 5 + 7 + 9 …

Sum of odd numbers

Page 35: Unary and Binary

Check the next one out…

Page 36: Unary and Binary

( n-1)2

n-1n-1 nn

nn

nnnn + + nnn-1n-1

= n (= n (n n + + n-1n-1))

= n = n nn

== nn

New shaded area =

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

++nn

area of new square is ( n)2

Page 37: Unary and Binary

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

++nn

(( nn))2 2 = = 11 + + 22 + … + + … +

nn

Page 38: Unary and Binary

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

++nn

((nn))2 2 = 1= 133 + 2 + 233 + 3 + 333 + … + + … + nn33

= [ = [ n(n+1)/2n(n+1)/2 ] ]22

Page 39: Unary and Binary

Can you find a formula for the sum

of the first n squares?

The Babylonians

needed this sum to compute the number

of blocks in their pyramids.

Page 40: Unary and Binary

The ancients grappled with problems of

abstraction in representation and

reasoning.

Let’s look back to the dawn of symbols…

Page 41: Unary and Binary

Sumerians [modern Iraq]

Page 42: Unary and Binary

Sumerians [modern Iraq]

8000 BC Sumerian tokens use multiplesymbols to represent numbers

3100 BC Develop Cuneiform writing

2000 BC Sumerian tablet demonstrates:

base 10 notation (no zero)solving linear equationssimple quadratic equations

Biblical timing: Abraham born in the Sumerian city of Ur

Page 43: Unary and Binary

Babylonians absorb Sumerians

1900 BC Sumerian/Babylonian TabletSum of first n numbersSum of first n squares“Pythagorean Theorem”“Pythagorean Triplets”, e.g., 3-4-5some bivariate equations

1600 BC Babylonian TabletTake square rootsSolve system of n linear

equations

Page 44: Unary and Binary

6000 BC Multiple symbols for numbers

3300 BC Developed Hieroglyphics

1850 BC Moscow Papyrus Volume of truncated pyramid

1650 BC Rhind Papyrus [Ahmes/Ahmose]Binary Multiplication/Division Sum of 1 to nSquare rootsLinear equations

Biblical timing: Joseph is Governor of Egypt.

Egyptians

Page 45: Unary and Binary

Harrappans [Indus Valley Culture]Pakistan/India

3500 BC Perhaps the first writing system?!

2000 BC Had a uniform decimal system ofweights and measures

Page 46: Unary and Binary

China

1200 BC Independent writing systemSurprisingly late.

1200 BC I Ching [Book of changes]Binary system developed to do

numerology.

Page 47: Unary and Binary

Rhind Papyrus: Scribe Ahmes was the Martin Gardener of his day!

Page 48: Unary and Binary

Rhind Papyrus had 87 Problems.

A man has seven houses,Each house contains seven cats,Each cat has killed seven mice,Each mouse had eaten seven ears of spelt,Each ear had seven grains on it.What is the total of all of these?

Sum of first five powers of 7

Page 49: Unary and Binary

We’ll use this fundamental sum again

and again:

The Geometric Series

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

XXn n – 1 – 1 XX - -

11

Page 50: Unary and Binary

A Frequently Arising Calculation

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

= X1 + X2 + X3 + … + Xn-1 + Xn

- 1 - X1 - X2 - X3 - … - Xn-2 – Xn-1

= - 1 +Xn

= Xn - 1

Page 51: Unary and Binary

Action Shot: Mult by X is a SHIFT

X ( 1 + X1 + X2 + X3 + . . . . . . + Xn-2 + Xn-1 )

= X ( X0 + X1 + X2 + X3 + . . . . . . + Xn-2 + Xn-1 )

= X1 + X2 + X3 + X4 + . . . . . . + Xn-1 + Xn

Page 52: Unary and Binary

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 53: Unary and Binary

The Geometric Series for X=2

1 + 21 +22 + 23 + … + 2n-1 = 2n -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 54: Unary and Binary

The Geometric Series for X=3

1 + 31 + 32 + 33 + … + 3n-1 = (3n -1)/2

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 55: Unary and Binary

The Geometric Series for X=½

1 + ½1 + ½2 + ½3 + … ½n-1 = (½n -1)/(-½) = 2-(½)n-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 56: Unary and Binary

Numbers and their properties can be represented as

strings of symbols.

Page 57: Unary and Binary

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 58: Unary and Binary

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 59: Unary and Binary

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

make using (at least one) letters

from .

Page 60: Unary and Binary

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 * = + [ {} = + [ 0

Page 61: Unary and Binary

Intuitively, * is the set of all finite

strings that we can make using letters

from including the empty string.

Page 62: Unary and Binary

Let DIGITS = {0,1,2,3,4,5,6,7,8,9}

be a symbol alphabet.

Any string in DIGITS+ will be called a

decimal number.

Page 63: Unary and Binary

Let BITS = {0,1} be a symbol alphabet.

Any string in BITS+ will be called a binary number.

Page 64: Unary and Binary

Let ROCK = {S} be a symbol alphabet.

Any string in ROCK+ will be called a unary number.

Page 65: Unary and Binary

Let BASE-X = {0,1,2,…,X-1}

be a symbol alphabet.

Any string in BASE-X+ will be called a

base-X number.

Page 66: Unary and Binary

We need to specify the map between sets of sequences

and numbers.

Page 67: Unary and Binary

Inductively definedfunction

f: ROCK+ ->

f(S) = 1f(SX) = f(X) + 1

Page 68: Unary and Binary

Inductively defined function

f: BITS+ ->

f(0) =0; f(1) =1

If |W| > 1 then W=Xb (b2BITS)f(Xb) = 2f(X) + b

Page 69: Unary and Binary

Non-inductive representation of f:

g(an-1 an-2 … a0) =

an-1*2n-1 + an-2*2n-2 + … + a020

Page 70: Unary and Binary

Two identical maps from sequences to numbers:

f(0) = 0; f(1) =1f(Xb) = 2f(X) + b

and

f(an-1 an-2 … a0) =

an-1*2n-1 + an-2*2n-2 + … + a0 20

Page 71: Unary and Binary

The symbol a0 is called the Least Significant Bit or the

Parity Bit.

a0 = 0

iff f(an-1an-2…a0) =

an-1*2n-1 + an-2 * 2n-2 +…+ a0 20

is an even number.

Page 72: Unary and Binary

Theorem: Each natural has a binary representation.

Base Case: 0 and 1 do. Induction hypothesis: Suppose all natural numbers less than n have a binary representation.

Induction Step: Note that n=2m+b for some m<n, with b=0 or 1. Represent n as the left-shifted sequence for m

concatenated with the symbol for b.

Page 73: Unary and Binary

No Leading Zero Binary (NLZB)

A binary string that is either 0 or 1, Or has length > 1, and does not have a leading zero.

0, 1, 10, 100000001, 1110000 are in NLZB01, 000001010111, 011111111 are not.

Page 74: Unary and Binary

Theorem: Each natural has a unique NLZB representation.

Base Case: 0 and 1 do.

Induction Hypothesis: Every natural number less than n has a unique NLZB representation.

Induction Step: Suppose n=2m+b has 2 NLZB

representations W and V. Their parity bit b must be identical. Hence, m also has two distinct NLZB representations, which

contradicts the induction hypothesis. So n must have a

unique representation.

Page 75: Unary and Binary

Inductive definition is great for showing UNIQUE

representation:f(Xb) = 2f(X) + b

Let n be the smallest number reprinted by two

different binary sequences. They must have the same

parity bit, thus we can make a smaller number

that has distinct representations.

Page 76: Unary and Binary

Each natural number has a

unique representation as

a (No Leading Zeroes) Binary

number!

Page 77: Unary and Binary

BASE X representation

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

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

S = an-1 an-2 … a1 a0 represents the number:

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

Page 78: Unary and Binary

Bases In Different Cultures

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

Page 79: Unary and Binary

BASE X representation

S = ( an-1 an-2 … a1 a0 )X represents the number:

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

Largest number representable in base-X with n “digits”

= (X-1 X-1 X-1 X-1 X-1 … X-1)X

= (X-1)(Xn-1 + Xn-2 + . . . + X0)

= (Xn – 1)

Page 80: Unary and Binary

Fundamental Theorem For Binary:

Each of the numbers from 0 to 2n-1 is uniquely represented by an n-bit number in

binary.

k uses log2k + 1 digits in base 2.

Page 81: Unary and Binary

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 82: Unary and Binary

n has length n in unary, but has

length log2n + 1 in

binary.

Unary is exponentially longer

than binary.

Page 83: Unary and Binary

Other Representations:Egyptian Base 3

Conventional Base 3: Each digit can be 0, 1, or 2

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

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

We can prove a unique representation theorem

Page 84: Unary and Binary

How could this be Egyptian? Historically, negative numbers first

appear in the writings of the Hindu mathematician Brahmagupta (628 AD).

Page 85: Unary and Binary
Page 86: Unary and Binary

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

“positive”

Page 87: Unary and Binary

Study Bee

Unary and Binary Triangular Numbers Little Gauss’s Proof Dot proofs

The Geometric Series (1+x+x2 + … + xn-1) = (xn -1)/(x-1)

Base-X representations unique binary representations

proof for no-leading zero binary

k uses log2k + 1 = log2 (k+1) digits in base 2 (unary is exponentially longer than binary) largest n-bit number in base X is (Xn -1)