This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 1 Algorithms with numbers One of the main themes of this chapter is the dramatic contrast between two ancient problems that at first seem very similar: Factoring: Given a number N , express it as a product of its prime factors. Primality: Given a number N , determine whether it is a prime. Factoring is hard. Despite centuries of effort by some of the world’s smartest mathemati- cians and computer scientists, the fastest methods for factoring a number N take time expo- nential in the number of bits of N . On the other hand, we shall soon see that we can efficiently test whether N is prime! And (it gets even more interesting) this strange disparity between the two intimately related problems, one very hard and the other very easy, lies at the heart of the technology that enables secure communication in today’s global information environment. En route to these insights, we need to develop algorithms for a variety of computational tasks involving numbers. We begin with basic arithmetic, an especially appropriate starting point because, as we know, the word algorithms originally applied only to methods for these problems. 1.1 Basic arithmetic 1.1.1 Addition We were so young when we learned the standard technique for addition that we would scarcely have thought to ask why it works. But let’s go back now and take a closer look. It is a basic property of decimal numbers that The sum of any three single-digit numbers is at most two digits long. Quick check: the sum is at most 9 + 9 + 9 = 27 , two digits long. In fact, this rule holds not just in decimal but in any base b ≥ 2 (Exercise 1.1). In binary, for instance, the maximum possible sum of three single-bit numbers is 3, which is a 2-bit number. 21 22 Algorithms Bases and logs Naturally, there is nothing special about the number 10—we just happen to have 10 fingers, and so 10 was an obvious place to pause and take counting to the next level. The Mayans developed a similar positional system based on the number 20 (no shoes, see?). And of course today computers represent numbers in binary. How many digits are needed to represent the number N ≥ in base b ? Let’s see—with k digits in base b we can express numbers up to b k- 1 ; for instance, in decimal, three digits get us all the way up to 999 = 10 3- 1 . By solving for k , we find that d log b ( N + 1) e digits (about log b N digits, give or take 1) are needed to write N in base b . How much does the size of a number change when we change bases? Recall the rule for converting logarithms from base a to base b : log b N = (log a N ) / (log a b ) . So the size of integer N in base a is the same as its size in base b , times a constant factor log a b . In big- O notation, therefore, the base is irrelevant, and we write the size simply as O (log N ) . When we do not specify a base, as we almost never will, we mean log 2 N ....
View Full Document
This document was uploaded on 08/10/2011.
- Spring '11