1 / 79

Ancient Wisdom: On Raising A Number To A Power

15. 15. a. Ancient Wisdom: On Raising A Number To A Power. 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-1 b n-2 …b 1 b 0

hulse
Télécharger la présentation

Ancient Wisdom: On Raising A Number To A Power

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15 15 a Ancient Wisdom:On Raising A Number To A Power

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

  3. 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

  4. 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.

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

  6. They used repeated halving to do base conversion. Consider

  7. 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;

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

  9. 70 140 280 560 * 70 6 * 350 1 * 910 Rhind Papyrus (1650 BC)70*13

  10. 70 140 280 560 * 70 6 * 350 1 * 910 Rhind Papyrus (1650 BC)70*13 Binary for 13 is 1101 = 23 + 22 + 20 70*13 = 70*23 + 70*22 + 70*20

  11. 17 34 68 136 1 2 * 4 8 * Rhind Papyrus (1650 BC)Division: 184/17 184 48 14

  12. 17 34 68 136 1 2 * 4 8 * Rhind Papyrus (1650 BC)Division: 184/17 184 48 14 184 = 17*8 + 17*2 + 14 184/17 = 10 with remainder 14

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

  14. Wow. Those Russian peasants were pretty smart.

  15. Standard Binary Multiplication= Egyptian Multiplication * * * * * * * * × 1101 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  16. Our story so far… We can view numbers in many different, but corresponding ways. Representation: Representation: Understand the relationship between Understand the relationship between different representations of the same different representations of the same information or idea information or idea 1 2 3

  17. Induction has many guises. Master their interrelationship. • Formal Arguments • Loop Invariants • Recursion • Algorithm Design • Recurrences Our story so far… Induction is how we define and manipulate mathematical ideas.

  18. Abstraction : Abstraction : Abstract away the inessential Abstract away the inessential features of a problem or solution features of a problem or solution = Let’s Articulate A New One:

  19. Even very simple computational problems can be surprisingly subtle.

  20. 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).

  21. b:=a*a b:=b*a b:=b*a b:=b*a b:=b*a b:=b*a b:=b*a b:=a*a b:=b*b b:=b*b b:=a8 This method costs only 3 multiplications. The savings are significant if b:=a8 is executed often.

  22. General Version • Given a constant n, how do we implement b:=anwith the fewest number of multiplications?

  23. Powering By Repeated Multiplication • Input: a, n • Output: A sequence starting with a, ending with an, such that each entry other than the first is the product of two previous entries.

  24. Example • Input: a,5 • Output: a, a2, a3, a4, a5 • or • Output: a, a2, a3, a5 • or • Output: a, a2, a4, a5

  25. Definition of M(n) • M(n)=The minimum number ofmultiplications required to produce an from a by repeated multiplication

  26. Exemplification: Exemplification: Try out a problem or Try out a problem or solution on small examples. solution on small examples. What is M(n)? Can we calculate it exactly? Can we approximate it?

  27. 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]

  28. 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)?

  29. 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)? Upper Bound

  30. ? ≤ M(8) ≤ 3 3 ≤ M(8) Exhaustive Search. There are only two sequences with 2 multiplications. Neither of them make 8: a, a2, a3 & a, a2, a4

  31. M(8) = 3 3 ≤ M(8) ≤ 3 Lower Bound Upper Bound

  32. Abstraction : Abstraction : Abstract away the inessential Abstract away the inessential features of a problem or solution features of a problem or solution = Representation: Representation: Understand the relationship between Understand the relationship between different representations of the same different representations of the same information or idea information or idea 1 2 3 Applying Two Ideas

  33. ((( ))) Abstraction : Abstraction : Representation: Representation: Abstract away the inessential Abstract away the inessential Understand the relationship between Understand the relationship between features of a problem or solution features of a problem or solution different representations of the same different representations of the same information or idea information or idea 1 = 2 3 What is the more essential representation of M(n)?

  34. 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.

  35. 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.

  36. Examples • Addition Chain for 8: • 1 2 3 5 8 • Minimal Addition Chain for 8: • 1 2 4 8

  37. Abstraction : Abstraction : Representation: Representation: Abstract away the inessential Abstract away the inessential Understand the relationship between Understand the relationship between features of a problem or solution features of a problem or solution different representations of the same different representations of the same information or idea information or idea 1 = 2 3 Addition Chains Are A Simpler Way To Represent The Original Problem

  38. 15 15 a M(30) = ?

  39. 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

  40. ? ≤M(30) ≤ 6 ? ≤M(n) ≤ ?

  41. 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.)

  42. 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 n from the powers of 2 created in phase I Total cost: b log2 nc + Bn -1

  43. Binary Method Applied To 30 • Binary • 30 11110 • Phase I • 1 1 • 2 10 • 4 100 • 8 1000 • 16 10000 • Phase II: 6 14 30 (Cost: 7 additions)

  44. 5 10 4 20 8 40 16 80 120 28 140 30 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 Rhind Papyrus (1650 BC)What is 30 times 5?

  45. 1 5 2 10 4 20 8 40 10 50 20 100 30 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.

  46. 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

  47. Abstraction : Abstraction : Abstract away the inessential Abstract away the inessential features of a problem or solution features of a problem or solution = We saw that applying ABSTRACTION to the PROBLEM simplifies the issue. PROBLEM = Raising A Number To A Power.

  48. Abstraction : Abstraction : Abstract away the inessential Abstract away the inessential features of a problem or solution features of a problem or solution = What about applying ABSTRACTION to the SOLUTION ? Let SOLUTION be the Repeated Squaring Algorithm (RQA).

  49. Abstraction : Abstraction : Abstract away the inessential Abstract away the inessential features of a problem or solution features of a problem or solution = What features did our solution (RQA) actually make use of? For example, does the RQA require the underlying objects to be numbers?

More Related