79
Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2005 Lecture 5 Sept 13, 2005 Carnegie Mellon University 15 15 a a

Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Embed Size (px)

Citation preview

Page 1: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Ancient Wisdom:On Raising A Number To A

Power

Great Theoretical Ideas In Computer Science

Anupam Gupta

CS 15-251 Fall 2005

Lecture 5 Sept 13, 2005 Carnegie Mellon University

1515

aa

Page 2: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Egyptian Multiplication

The Egyptians used decimal numbers but

multiplied and divided in

binary

Page 3: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Egyptian Multiplication a times bby repeated doubling

b has some n-bit representation: bn-1bn-2…b1b0

Starting with a, repeatedly double largest number so far to obtain: a, 2a, 4a, …., 2n-1a

Sum together all the 2ka where bk = 1

Page 4: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Why does that work?

b = b020 + b121 + b222 + … + bn-12n-1

ab = b020a + b121a + b222a + … + bn-12n-1a

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

Otherwise that term will be 0.

Page 5: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Wait! How did the Egyptians do

the part where they converted b to binary?

Page 6: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

They used repeated halving to do base conversion. Consider …

Page 7: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Egyptian Base Conversion

Output stream will print right to left.

Input X;repeat { if (X is even)

then print 0; else

{X := X-1; print 1;}

X := X/2 ;} until X=0;

Page 8: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

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

Page 9: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Rhind Papyrus (1650 BC)70*13

70140280560

13 * 7063 * 3501 * 910

Page 10: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

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 11: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

173468136

12 *48 *

184 48 14

Rhind Papyrus (1650 BC)Division: 184/17

Page 12: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

173468136

12 *48 *

184 48 14

Rhind Papyrus (1650 BC)Division: 184/17

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

Page 13: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

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

Page 14: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Wow. Those Russian peasants were pretty

smart.

Page 15: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Standard Binary Multiplication= Egyptian Multiplication

× 1101

* * * * * * * *

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

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

Page 16: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

We can view numbers in many different, but

corresponding ways.

Our story so far…

Representation:Representation:Understand the relationship betweenUnderstand the relationship betweendifferent representations of the samedifferent representations of the same

information or ideainformation or idea

1

2

3

Page 17: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Induction is how we define and manipulate mathematical ideas.

Inductionhas many guises.Master their interrelationship.

•Formal Arguments

•Loop Invariants

•Recursion

•Algorithm Design

•Recurrences

Our story so far…

Page 18: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Let’s Articulate A New One:

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 19: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Even very simple

computational problems

can be surprisingly

subtle.

Page 20: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Compiler Translation

A compiler must translate a high level language (e.g., C) with complex operations (e.g., exponentiation) into a lower level language (e.g., assembly) that can only support simpler operations (e.g., multiplication).

Page 21: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

b:=a8

b:=a*ab:=b*ab:=b*ab:=b*ab:=b*ab:=b*ab:=b*a

b:=a*ab:=b*bb:=b*b

This method costs only 3 multiplications. The

savings are significant if b:=a8 is executed often.

Page 22: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

General Version

Given a constant n, how do we implement

b:=an with the fewest number

of multiplications?

Page 23: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Powering By Repeated Multiplication

Input: a, n

Output: A sequence starting with a, A sequence starting with a, ending with aending with ann, such that , such that each entry other than the each entry other than the first is the product of two first is the product of two previous entries.previous entries.

Page 24: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Example

Input: a,5

Output: a, a2, a3, a4, a5

orOutput: a, a2, a3, a5

orOutput: a, a2, a4, a5

Page 25: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Definition of M(n)

M(n) = The minimum number ofmultiplications required to produce an from a by repeated multiplication

Page 26: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

What is M(n)? Can we calculate it exactly? Can we approximate

it?

Exemplification:Exemplification:Try out a problem orTry out a problem or

solution on small examples. solution on small examples.

Page 27: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Some Very Small Examples

•What is M(1)?– M(1) = 0 [a]

•What is M(0)?– M(0) is not clear how to define

•What is M(2)?– M(2) = 1 [a, a2]

Page 28: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(8) = ?a, a2, a4, a8 is one way to make a8 in 3 multiplications.

What does this tell us about the value of M(8)?

Page 29: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(8) = ?a, a2, a4, a8 is one way to make a8 in 3 multiplications.

What does this tell us about the value of M(8)?

M( )8 3Upper Bound

Page 30: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Exhaustive Search. There are only two sequences with 2 multiplications. Neither of them make 8:

a, a2, a3 & a, a2, a4

? ≤ M(8) ≤ 3

3 ≤ M(8)

Page 31: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Upper Bound

Lower Bound

M(8) = 3

3 ≤ M(8) ≤ 3

Page 32: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Applying Two Ideas

Representation:Representation:Understand the relationship betweenUnderstand the relationship betweendifferent representations of the samedifferent representations of the same

information or ideainformation or idea

1

2

3

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 33: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

What is the more essential representation of M(n)?

((( )))

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Representation:Representation:Understand the relationship betweenUnderstand the relationship betweendifferent representations of the samedifferent representations of the same

information or ideainformation or idea

1

2

3

Page 34: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

The “a” is a red herring.

ax × ay is ax+y

Everything besides the exponent is inessential. This

should be viewed as a problem of repeated addition, rather than

repeated multiplication.

Page 35: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Addition Chains

M(n) = Number of stages required to make n, where we start at 1 and in each subsequent stage we add two previously constructed numbers.

Page 36: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Examples

Addition Chain for 8:1 2 3 5 8

Minimal Addition Chain for 8:1 2 4 8

Page 37: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Addition Chains Are A Simpler Way To Represent The Original Problem

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=Representation:Representation:

Understand the relationship betweenUnderstand the relationship betweendifferent representations of the samedifferent representations of the same

information or ideainformation or idea

1

2

3

Page 38: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(30) = ?

1515

aa

Page 39: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Some Addition Chains For 30

1 2 4 8 16 24 28 30

1 2 4 5 10 20 30

1 2 3 5 10 15 30

1 2 4 8 10 20 30

Page 40: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

? ≤ M(30) ≤ 6

? ≤ M(n) ≤ ?

Page 41: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Binary Representation

Let Bn be the number of “1”s in the binary representation of n.

E.g.: B5 = 2 since 101 = binary representation of 5

Proposition: Bn ≤ b log2 (n) c + 1

(It is at most the number of bits in the binary representation of n.)

Page 42: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Binary MethodRepeated Squaring MethodRepeated Doubling Method

Phase I (Repeated Doubling)For

Add largest so far to itself(1, 2, 4, 8, 16, . . . )

Phase II (Make n from bits and pieces)Expand n in binary to see how n is the sum of Bn powers of 2. Use Bn - 1 stages to

make nfrom the powers of 2 created in phase I

log2 n stages:

Total cost: b log2 nc + Bn -1

Page 43: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Binary Method Applied To 30Binary

30 11110

Phase I1 12 104 1008 100016 10000

Phase II: 6 14 30 (Cost: 7 additions)

Page 44: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Rhind Papyrus (1650 BC)What is 30 times 5?

1 52 104 208 4016 8024 12028 14030 150

30 by a chain of 7:

1 2 4 8 16 24 28 30

Repeated doubling is the same as the Egyptian binary multiplication

Page 45: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

1 52 104 208 4010 5020 10030 150

30 by a chain of 6:

1 2 4 8 10 20 30

Rhind Papyrus (1650 BC)Actually used faster chain for 30*5.

Page 46: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

A shortest addition chain for n gives a shortest method for the Egyptian approach to multiplying by the number n.

The fastest scribes would seek to know M(n) for commonly arising values of n.

The Egyptian Connection

Page 47: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M n n B nn( ) log log 2 21 2

Page 48: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

We saw that applying ABSTRACTION to the

PROBLEM simplifies the issue.

PROBLEM = Raising A Number To A Power.

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 49: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

What about applying ABSTRACTION to the

SOLUTION ?

Let SOLUTION be the Repeated Squaring Algorithm (RQA).

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 50: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

What features did our solution (RQA) actually

make use of?

For example, does the RQA require the

underlying objects to be numbers?

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 51: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

The repeated squaring method works for

modular arithmetic and

for raising a matrix to a power.

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 52: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

The repeated squaring method works for any

notion of “multiplication” that is associative.

(a*b)*c = a*(b*c)ak is well defined

ax * ay = ax+y

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 53: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Generalization

Solution

Always ask yourself what your solution actually requires.

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 54: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

? ≤ M(30) ≤ 6

? ≤ M(n) ≤ 2 log2 n

Page 55: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

A Lower Bound Idea

You can’t make any number bigger than 2n in n steps.

1 2 4 8 16 32 64 . . .

or is this a failure of

imagination?

Page 56: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Let Sk be the statement that no k stage addition chain will contain a number greater than 2k

Base case: k=0. S0 is true since no chain can exceed 20 after 0 stages.

8 k > 0, Sk ) Sk + 1

At stage k+1 we add two numbers from the previous stage. From Sk we know that they both are bounded by 2k. Hence, their sum is bounded by 2k+1. No number greater than 2k+1 can be present by stage k+1.

Page 57: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Proof By Invariant(Induction)

Invariant: All the numbers created by stage n are less than or equal to 2n.

The invariant is true at the start.

Suppose we are at stage k. If the invariant is true, then the two numbers we decide to sum for stage k+1 are ≤ 2k and hence create a number less than or equal to 2k+1. The invariant is thus true at stage k+1.

Page 58: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

I n fact, n n M( ) log ( ) 2

M n n( ) log ( ) 2

Change Of Variable

All numbers obtainable in m stages are bounded by 2m. Let m = log2(n).

Thus, all numbers obtainable in log2(n) stages are bounded by n.

Page 59: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

? ≤ M(30) ≤ 6

log2 n ≤ M(n) ≤ 2 log2 n

Page 60: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Theorem: 2i is the largest number that can be made in i stages, and

can only be made by repeated doubling

Base i = 0 is clear.

To make anything as big as 2i requires having some X as big as 2i-1 in i-1 stages.

By I.H., we must have all the powers of 2 up to 2i-1 at stage i-1. Hence, we can only double 2i-1 at stage i.

The theorem follows.

Page 61: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

5 < M(30)

Suppose that M(30)=5. At the last stage, we added two numbers x1 and x2 to get 30.

Without loss of generality (WLOG), we assume that x1 x2.

Thus, x1 15

By doubling bound, x1 16

But x1 can’t be 16 since there is only one way to make 16 in 4 stages and it does not make 14 along the way.

Thus, x1 = 15 and M(15)=4

Page 62: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Suppose M(15) = 4

At stage 3, a number bigger than 7.5, but not more than 8 must have existed. There is only one sequence that gets 8 in 3 additions: 1 2 4 8

That sequence does not make 7 along the way and hence there is nothing to add to 8 to make 15 at the next stage.

Thus, M(15) > 4. CONTRADICTION.

Page 63: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(30)=6

Page 64: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(30) = 6

log2 n ≤ M(n) ≤ 2 log2 n

Page 65: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Rhind Papyrus (1650 BC)

1 52 104 208 4010 5020 10030 150

30 = 1 2 4 8 10 20 30

Page 66: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Factoring Bound

M(a × b) ≤M(a) + M(b)

Proof:• Construct a in M(a) additions• Using a as a unit follow a construction

method for b using M(b) additions. In other words, each time the construction of b refers to a number y, use the number ay instead.

Page 67: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Example

45 = 5 * 9M(5)=3 [1 2 4 5]M(9)=4 [1 2 4 8 9 ]M(45) 3+4 [1 2 4 5 10 20 40 45]

Page 68: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Corollary (Using Induction)

M(a1a2a3…an) ≤M(a1)+M(a2)+…+M(an)

Proof: For n=1 the bound clearly holds. Assume it has been shown for up to n-1. Now apply theorem using a= a1a2a3…an-1 and b=an to obtain:

M(a1a2a3…an) ≤M(a1a2a3…an-1) + M(an)

By inductive assumption, M(a1a2a3…an-1) ≤M(a1) + M(a2) + … + M(an-1)

Page 69: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

More Corollaries

Corollary: M(ak) kM(a)

Corollary: M(p p p p

M(p + M(p + p

1 2 3 n

1 2 n

1 2 3

1 2

n

nM

)

) ) ( )

Does equality hold?

Page 70: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

M(33) < M(3) + M(11)

M(3) = 2 [1 2 3]M(11)= 5 [1 2 3 5 10 11]

M(3) + M(11) = 7

M(33) = 6 [1 2 4 8 16 32 33]

The conjecture of equality fails. There have been many nice conjectures. . . .

Page 71: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Conjecture: M(2n) = M(n) +1(A. Goulard)

A fastest way to an even number is to make half that number and then double it.

Proof given in 1895 by E. de Jonquieres in L’Intermediere Des Mathematiques, volume 2, pages 125-126FALSE!

M(191)=M(382)=11

Furthermore, there are

infinitely many such examples.

Page 72: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Open Problem

Is there an n such that:M(2n) < M(n)

Page 73: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Conjecture

Each stage might as well consist of adding the largest number so far to one of the other numbers.

First Counter-example: 12,50912,509[1 2 4 8 16 17 32 64 128 256 512 1024 1041 2082 4164 8328 8345 12509]

Page 74: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Open Problem

Prove or disprove the Scholz-Brauer Conjecture:

M(2n-1) n - 1 + Bn

(The bound that follows from this lecture is too weak: M(2n-1) 2n - 1)

Page 75: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

High Level Point

Don’t underestimate “simple” problems. Some “simple” mysteries have endured for thousand of years.

Page 76: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Study Bee

Egyptian Multiplication

Raising To A PowerMinimal Addition ChainLower and Upper Bounds

RQA [Repeated Squaring Algorithm]RQA works for ANY binary operator

Page 77: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Study Bee

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Representation:Representation:Understand the relationship betweenUnderstand the relationship betweendifferent representations of the samedifferent representations of the same

information or ideainformation or idea

1

2

3

Inductionhas many guises.Master their interrelationship.

•Formal Arguments

•Loop Invariants

•Recursion

•Algorithm Design

•Recurrences

Exemplification:Exemplification:Try out a problem orTry out a problem or

solution on small examples. solution on small examples.

Page 78: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

Study Bee

Solution

GENERALIZE

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problem or solutionfeatures of a problem or solution

=

Page 79: Ancient Wisdom: On Raising A Number To A Power Great Theoretical Ideas In Computer Science Anupam GuptaCS 15-251 Fall 2005 Lecture 5Sept 13, 2005Carnegie

REFERENCES

The Art Of Computer Programming, Vol 2, pp. 444 – 466, by Donald Knuth