{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

A list of n encodings of rational numbers and the

Info icon This preview shows pages 16–18. Sign up to view the full content.

View Full Document Right Arrow Icon
a list of n encodings of rational numbers, and the matrix as a list of n encodings of rows. It is clear that other coding schemes are possible, giving rise to different definitions of input 11
Image of page 16

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

View Full Document Right Arrow Icon
length. For example, we could encode inputs in some base other than 2 (but not unary!) or use a different alphabet. However, such an alternative encoding scheme would change the definition of input length by at most a constant multiplicative factor, and so would not affect the notion of a polynomial-time algorithm. We stress that algorithms may use data structures for representing mathematical objects that look quite different from whatever encoding scheme one might choose. 3.2 Basic Integer Arithmetic We will need algorithms to manipulate integers of arbitrary length. Since such integers will exceed the word-size of actual machines, we represent large integers as vectors of digits to some base B , along with a bit indicating the sign. Thus, for x Z , we write x = ± ( k - 1 X i =0 x i B i ) = ± ( x k - 1 · · · x 1 x 0 ) B , where 0 x i < B for 0 i < k , and usually, we shall have x k - 1 6 = 0. The integer x will be represented in memory as a data structure consisting of a vector of digits and a sign-bit. For our purposes, we shall consider B to be a constant, and moreover, a power of 2. The choice of B as a power of 2 allows us to extract an arbitrary bit in the binary representation of a number in time O (1). We discuss basic arithmetic algorithms for positive integers; they can be very easily adapted to deal with signed integers. All of these algorithms can be implemented directly in a programming language that provides a “built-in” signed integer type that can represent all integers whose absolute value is less than B 2 , and that provides the basic arithmetic operations (addition, subtraction, multiplication, integer division). So, for example, using the C programming language’s int type on a typical 32-bit computer, we could take B = 2 15 . The resulting algorithms would be reasonably efficient, but not nearly as efficient as algorithms that are much more carefully implemented, and which take advantage of low-level “assembly language” codes specific to a particular machine’s architecture (e.g., the GNU Multi-Precision library GMP, available as http://www.swox.com/gmp ). Suppose we have two positive integers a and b , represented with k and base- B digits, re- spectively, with k . So we have a = ( a k - 1 · · · a 0 ) B and b = ( b - 1 · · · b 0 ) B . Then using the standard “paper-and-pencil” method (adapted from base-10 to base- B , of course), we can compute the base- B representation of a + b or a - b in time O ( k ). Using the standard paper-and-pencil technique, we can compute the k + 1 digit product a · b 0 in time O ( k ). We can then compute the k + digit product c = a · b as follows: c 0 for i 0 to - 1 do c c + B i · ab i As each loop-iteration of this algorithm takes time O ( k ), the total running-time is O ( k‘ ).
Image of page 17
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern