Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS 15-251 Spring 2003 Lecture 2Jan 16, 2003Carnegie Mellon University.

Slides:



Advertisements
Similar presentations
Lecture 2 Based on Chapter 1, Weiss. Mathematical Foundation Series and summation: ……. N = N(N+1)/2 (arithmetic series) 1 + r+ r 2 + r 3 +………r.
Advertisements

Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
Grade School Again: A Parallel Perspective Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 17Mar 16, 2004Carnegie.
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall 2005 Lecture 5Sept 13, 2005Carnegie.
Number Theory and Cryptography
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 16March 4,
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Chapter Chapter Goals Know the different types of numbers Describe positional notation.
Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 3Jan 20, 2004Carnegie Mellon University.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Chapter 2 Real Numbers and Complex Numbers. What is a number? What qualifies a mathematical object to be identified as some type of number? Exactly what.
4.1 Polynomial Functions Objectives: Define a polynomial.
Great Theoretical Ideas in Computer Science.
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 18March.
Data Representation Number Systems.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Slide 5-1 Copyright © 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
(CSC 102) Discrete Structures Lecture 10.
Simple Data Type Representation and conversion of numbers
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.
Number Systems - Part II
MATH 224 – Discrete Mathematics
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Decimal Number System We are used to the decimal number system which is a positional number system The decimal number 4386 represents the value: 41000.
IT253: Computer Organization
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
COMPSCI 102 Introduction to Discrete Mathematics.
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Integer Representation for People Computer Organization and Assembly Language: Module 3.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,
Unary and Binary Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall 2006 Lecture 4Sept 7, 2006Carnegie Mellon University.
Complexity 20-1 Complexity Andrei Bulatov Parallel Arithmetic.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Phys 4330 Digital ElectronicsBinary System Digital circuits process signals that contain just two voltage levels or states, labeled logic "0" and logic.
COMPSCI 102 Discrete Mathematics for Computer Science.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Logic Design Dr. Oliver Faust.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
CSC 331: DIGITAL LOGIC DESIGN COURSE LECTURER: E. Y. BAAGYERE. CONTACT: LECTURE TIME: 15:40 – 17:45 hrs. VENUE: SP-LAB.
Remarks on Fast Exp (4/2) How do we measure how fast any algorithm is? Definition. The complexity of an algorithm is a measure of the approximate number.
Great Theoretical Ideas in Computer Science.
Grade School Again: A Parallel Perspective CS Lecture 7.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Digital logic COMP214  Lecture 2 Dr. Sarah M.Eljack Chapter 1 1.
Unary and Binary Great Theoretical Ideas In Computer Science Anupam GuptaCS Fall 2005 Lecture 4Sept 8, 2005Carnegie Mellon University.
Introduction To Number Systems
Great Theoretical Ideas in Computer Science
Modeling with Recurrence Relations
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Unary, Binary, and Beyond
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Great Theoretical Ideas in Computer Science
Great Theoretical Ideas in Computer Science
Great Theoretical Ideas in Computer Science
Ancient Wisdom: On Raising A Number To A Power
Ancient Wisdom: On Raising A Number To A Power
Digital Logic & Design Lecture 02.
Great Theoretical Ideas In Computer Science
Chapter 2: Number Systems
Great Theoretical Ideas In Computer Science
Presentation transcript:

Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2003 Lecture 2Jan 16, 2003Carnegie Mellon University

Last time we talked about unary notation.

n th Triangular Number  n = n-1 + n = n(n+1)/2

n th Square Number  n = … + 2n-1 = Sum of first n odd numbers

n th Square Number  n =  n +  n-1 = n 2

(  n ) 2 = (  n-1 ) 2 + n (  n ) 2 = n

Unary takes size n to represent the number n. Higher bases give a logarithmically more compact representation.

Base X, X>1 A base X digit is any integer 0 · a < X Let S = a n-1, a n-2, …, a 0 be a sequence of base X digits. S is said to be the base X representation of the number: a n-1 X n-1 + a n-2 X n a 0 X 0

Base 2 = BinaryBase 10 = Decimal A base X digit is any integer 0 · a < X Let S = a n-1, a n-2, …, a 0 be a sequence of base X digits. S is said to be the base X representation of the number: a n-1 X n-1 + a n-2 X n a 0 X 0

Sequence S = a n-1, a n-2, …, a 0 represents the number: a n-1 X n-1 + a n-2 X n a 0 X 0 Base represents 1 (2) (2 1 ) + 1 (2 0 ) Base represents 0 (7) (7 1 ) + 5 (7 0 )

Sequence S = a n-1, a n-2, …, a 0 represents the number: a n-1 X n-1 + a n-2 X n a 0 X 0 Base represents 1 (2) (2 1 ) + 1 (2 0 ) = Base represents 0 (7) (7 1 ) + 5 (7 0 ) =

Biggest n “digit” number in base X would be: (X-1)X n-1 + (X-1)X n …(X-1)X 0 Base represents 1 (2) (2 1 ) + 1 (2 0 ) Base represents 6 (7) (7 1 ) + 6 (7 0 )

Biggest n “digit” number in base X would be: (X-1)X n-1 + (X-1)X n …(X-1)X 0 Base represents 1 (2) (2 1 ) + 1 (2 0 ) = 1000 represents 2 3 Thus, 111 represents 2 3 – 1 Base represents 6 (7) (7 1 ) + 6 (7 0 ) = 1000 represents 7 3 Thus, 666 represents

Biggest n “digit” number in base X would be: S= (X-1)X n-1 + (X-1)X n …(X-1)X 0 \ Add 1 to get: X n + 0 X n-1 + … + 0 X 0 Thus, S = X n - 1 Base represents 1 (2) (2 1 ) + 1 (2 0 ) = 1000 represents 2 3 Thus, 111 represents 2 3 – 1 Base represents 6 (7) (7 1 ) + 6 (7 0 ) = 1000 represents 7 3 Thus, 666 represents

(X-1)X n-1 + (X-1)X n …(X-1)X 0 = X n - 1 Factoring out (x-1). We obtain: (X-1) (X n-1 + X n-2 + ….. +X + 1) = X n – 1 Dividing by X-1 (since X is not equal to 1): (X n-1 + X n-2 + ….. +X + 1) = (X n – 1) / (X -1) Or. (1 + X + X 2 + … + X n-1 ) = (X n – 1) / (X -1)

This is a super important sum called the Geometric Series (or Sum). Let’s prove this again, a different way X 1 + X 2 + X 3 + … + X n-2 + X n-1 = 1 + X 1 + X 2 + X 3 + … + X n-2 + X n-1 = X n – 1 X - 1 X - 1

An Important Calculation (X-1) ( 1 + X 1 + X 2 + X 3 + … + X n-2 + X n-1 ) = X 1 + X 2 + X 3 + … + X n-1 + X n X 1 - X 2 - X 3 - … - X n-2 – X n-1 = X n = X n - 1

The Geometric Series (X-1) ( 1 + X 1 + X 2 + X 3 + … + X n-1 ) = X n X 1 + X 2 + X 3 + … + X n-2 + X n-1 = 1 + X 1 + X 2 + X 3 + … + X n-2 + X n-1 = X n – 1 X - 1 X - 1

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

The highest n digit number in base X. (X-1) ( 1 + X 1 + X 2 + X 3 + … + X n-1 ) = X n - 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. X n - 1 S = X n - 1

Notice that we have not yet shown that each base-b number has a unique representation. In service of this, we will introduce a variation of base-b called “plus minus base-b”.

Plus/Minus Base X An plus/minus base X digit is any integer –X < a < X Let S = a n-1, a n-2, …, a 0 be a sequence of plus/minus base X digits. S is said to be the plus/minus base X representation of the number: a n-1 X n-1 + a n-2 X n a 0 X 0

Does each n-digit number in plus/minus base X represent a different integer?

No. Consider plus/minus binary. 5 = =

Humm.. So what is it good for?

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

0 has a unique n-digit plus/minus base X representation as all 0’s. Suppose a n-1 X n-1 + a n-2 X n a 0 X 0 = S =0, where there is some highest k such that a k  0. Wl.o.g. assume a k > 0. Because X k > (X-1)(1 + X 1 + X 2 + X 3 + … + X k-1 ) no sequence of digits from a k-1 to a 0 can represent a number as small as –X k Hence S  0. Contradiction.

Each of the numbers from 0 to X n -1 is uniquely represented by an n-digit number in base X. We already know that n–digits will represent something between 0 and X n – 1. Suppose two distinct sequences represent the same number: a n-1 X n-1 + a n-2 X n a 0 X 0 = b n-1 X n-1 + b n-2 X n b 0 X 0 The difference of the two would be an plus/minus base X representation of 0, but it would have a non-zero digit. Contradiction.

Each of the numbers from 0 to X n -1 is uniquely represented by an n-digit number in base X. n digits represent up to X n – 1 n-1 digits represents up to X n Let k be a number: X n-1 ≤ k ≤ X n - 1 So k can be represented with n digits. For all k:  log x k  = n – 1 So k uses  log x k  + 1 digits.

Fundamental Theorem For Base X: Each of the numbers from 0 to X n -1 is uniquely represented by an n-digit number in base X. k uses  log x k  + 1 digits in base X.

n has length n in unary, but has length  log 2 n  + 1 in binary. Unary is exponentially longer than binary.

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

Egyptian Multiplication The Egyptians used decimal numbers but multiplied and divided in binary

Egyptian Multiplication a times b by repeated doubling b has some n-bit representation: b n..b 0 Starting with a, repeatedly double largest so far to obtain: a, 2a, 4a, …., 2 n a Sum together all 2 k a where b k = 1

Egyptian Multiplication 15 times 5 by 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 2 k (15) where b k = 1: = 75

Why does that work? b = b b b … + b n 2 n ab = b a + b a + b a + … + b n 2 n a If b k is 1 then 2 k a is in the sum. Otherwise that term will be 0.

Wait! How did the Egyptians do the part where they converted b to binary?

They used repeated halving to do base conversion. Consider …

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 }

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  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

Start the algorithm Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

And Keep Going until 0 Repeat until X=0 { If X is even then Output O; Otherwise Output 1; X:=  X/2  }

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

Rhind Papyrus (1650 BC) 70* * *350 1 *910

Rhind Papyrus (1650 BC) 70* * *350 1 *910 Binary for 13 is 1101 = *13 = 70* * *2 0

Rhind Papyrus (1650 BC) * 4 8 *

Rhind Papyrus (1650 BC) * 4 8 * = 17*8 + 17* /17 = 10 with remainder 14

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

Wow. Those Russian peasants were pretty smart.

Standard Binary Multiplication = Egyptian Multiplication X 101 * * * * * * * * * * * * * * * * * * * * * * *

Egyptian Base 3 We have defined Base 3: Each digit can be 0, 1, or 2 Plus/Minus Base 3 uses -2, -1, 0, 1, 2 Here is a new one: Egyptian Base 3 uses -1, 0, 1 Example: = = 5

Unique Representation Theorem for Egyptian Base 3 No integer has 2 distinct, n-digit, Egyptian base-3 representations. We can represent all integers from -(3 n -1)/2 to (3 n -1)/2 Proof; If so, their difference would be a non-trivial plus/minus base 3 representation of 0. Contradiction. Highest number = 1111…1 = (3 n -1)/2 Lowest number = …-1 = - (3 n -1)/2

Unique Representation Theorem for Egyptian Base 3 No integer has 2 distinct, n-digit, Egyptian base-3 representations. We can represent all integers from -(3 n -1)/2 to (3 n -1)/2 3 n, n-digit, base 3 representations of the numbers from 0 to 3 n – 1 Subtract …111 = (3 n – 1)/2 from each to get an Egyptian base 3 representation of all the numbers from -(3 n -1)/2 to (3 n -1)/2.

How could this be Egyptian? Historically, negative numbers first appear in the writings of the Hindu mathematician Brahmagupta (628 AD).

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

How to add 2 n-bit numbers. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** ** ** ** ** *** *** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** ** ** *** *** **** **** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** ** ** *** *** **** **** **** **** ** ** ** ** ** ** ** ** +

*** *** ** ** ** ** *** *** **** **** **** **** **** **** ** ** ** ** ** ** ** ** +

*** *** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** * * * * + * ** *

How to add 2 n-bit numbers. *** *** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** * * * * + * ** * Let k be the maximum time that it takes you to do Time < kn is proportional to n

The time grow linearly with input size. # of bits in numbers timetime kn

If n people agree to help you add two n bit numbers, it is not obvious that they can finish faster than if you had done it yourself.

Is it possible to add two n bit numbers in less than linear parallel-time? Darn those carries.

We will look at parallel binary addition in a later lecture. For now, let me show you a super parallel plus/minus binary addition.

Plus/minus binary means base 2 allowing digits to be from {-1, 0, 1}. We can call each digit a “trit”.

n people can add 2, n-trit, plus/minus binary numbers in constant time!

An Addition Party to Add to

An Addition Party Invite n people to add two n-trit numbers Assign one person to each trit position

An Addition Party Each person should add the two input trits in their possession. Problem: 2 and -2 are not allowed in the final answer.

Pass Left If you have a 1 or a 2 subtract 2 from yourself and pass a 1 to the left. (Nobody keeps more than 0) Add in anything that is given to you from the right. (Nobody has more than a 1) 1

After passing left There will never again be any 2s as everyone had at most 0 and received at most 1 more

Passing left again If you have a -1 or -2 add 2 to yourself and pass a -1 to the left (Nobody keeps less than 0)

After passing left again No -2s anymore either. Everyone kept at least 0 and received At most -1.

=

Caution: Parties and Algorithms Do not Mix

Later we will see how this is used inside all arithmetic chips.

References The Book Of Numbers, by J. Conway and R. Guy History of Mathematics, Histories of Problems, by The Inter-IREM Commission