Algorithms_Part2

# Algorithms_Part2 - Chapter 1 Algorithms with numbers One of...

This preview shows pages 1–3. Sign up to view the full content.

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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 0 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 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern