1 Section 2.5 Integers and Algorithms
2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S): –divide L by S to obtain: L = S * c + r where c is some constant and r is the remainder –next, find gcd(S,r) using the same method –continue as long as r > 0; the last r > 0 is the gcd
3 Example Find gcd(45, 12) gcd(45, 12) = gcd (12, 9) because 45 = 12 * gcd(12, 9) = gcd (9, 3) because 12 = 9 * gcd(9, 3) = 3 because 9 = 3 * Therefore gcd(45, 12) = 3
4 Example Find gcd(271, 83) gcd(271, 83) = gcd(83, 22) because 271 = 83 * gcd(83, 22) = gcd(22, 17) because 83 = 22 * gcd(22, 17) = gcd(17, 5) because 22 = 17 * gcd(17, 5) = gcd(5, 2) because 17 = 5 * gcd(5, 2) = 1 because 5 = 2 * Since 1 is the last non-zero remainder, gcd(271, 83) = 1 and 271 and 83 are relatively prime
5 More on Euclidean Algorithm Euclidean algorithm is based on this lemma: –Let a = bq + r where a, b, q and r are integers –Then gcd(a,b) = gcd(b,r) C++ code for Euclidean Algorithm: int gcd(int a, int b) { int remainder; while (b != 0) { remainder = a % b; a = b; b = remainder; } return a; }
6 Representation of Integers We are used to expressing integers as base 10, or decimal numbers: –Each digit represents a number multiplied by power of 10, with the rightmost digit being multiplied by 10 0 –The sum of the digits represents the value –For example, 472 = 4(10 2 )+7(10 1 )+2(10 0 )
7 Representation of Integers Any base can be used, with the same method employed Computers typically use base 2 (binary), base 8 (octal) and base 16 (hexadecimal) to represent integers
8 Binary Representation Digits are 0s and 1s The value of a binary expansion of a number is the sum of all digits multiplied by the power of 2 represented by their position, with the rightmost digit being position 0 and the leftmost nth position being position n-1
9 Binary Example = 1* * * * * * *2 6 = = 95
10 Hexadecimal Representation Digits range and A.. F –A = 10, B = 11, … F = 15 Hex to decimal conversion example: 14A0E = 14* * * * *16 4 = = 84,494
11 Conversion from hex to binary, and vice-versa Each hex digit represents 4 binary digits, or bits, since 16 = 2 4 Table below shows conversion: HexBinHexBinHexBinHexBin D A1010E B1011F C1100
12 Algorithm for base b expansion of integer n Divide n by b, obtaining quotient & remainder: n = bq 0 + a 0 where 0 <= a 0 < b remainder (a 0 ) is rightmost digit in base b expansion Divide q 0 by b, obtaining: q 0 = bq 1 + a 1 (0 <= a 1 < b) a 1 is second digit from right in base b expansion Continue successive divisions until we obtain a q = 0
13 Example Find octal (base 8) expansion of = 8 * (rightmost digit) 593 =8 * = 8 * = 8 * = 8 * 0 + 1(leftmost digit) Result is
14 C++ for base expansion algorithm (for base <= 10) int baseExpand(int n, int b) { int k = 0, digit, expansion = 0; while (n != 0) { digit = n % b; n = n / b; expansion = expansion + digit * pow(10,k); k++; } return expansion; }
15 Binary Addition Suppose a & b are binary numbers; they can be represented as: a = (a n-1 a n-2 … a 1 a 0 ) b = (b n-1 b n-2 … b 1 b 0 ) where n is the number of digits - note that this is string notation, not indicative of multiplication
16 Binary Addition To add a and b, start with rightmost bits: a 0 + b 0 = s 0 + c 0 * 2 where s is the sum and c is the carry (which may be 0) Proceed to next bit, adding previous carry: a 1 + b 1 + c 0 = s 1 + c 1 * 2
17 Binary Addition Continue process until you reach the last bit of either number: a n-1 + b n-1 + c n-2 = s n-1 + c n-1 * 2 If the carry is not 0, it will be the leading bit of the result If the numbers are not the same length, the carry would be added to the next bit of the longer number, and the carry from this would be added to the next bit, etc.
18 Binary Addition Example Let a = 1110, b = 1001; find a + b a 0 = 0, b 0 = 1 a 0 + b 0 = 1 + 2*0 1 a 1 = 1, b 1 = 0, c 0 = 0 a 1 + b 1 + c 0 = 1 + 2*0 1 a 2 = 1, b 2 = 0, c 1 = 0 a 2 + b 2 + c 1 = 1 + 2*0 1 a 3 = 1, b 3 = 1, c 2 = 0 a 3 + b 3 + c 2 = *0 0 a 4 = 0, b 4 = 0, c 3 = 1 a 4 + b 4 + c 3 =
19 Pseudocode for Addition Algorithm Procedure add (input: positive integers a & b) carry = 0 for (counter = 0; counter < n; counter++) temp = a counter + b counter + carry/2 sum counter = a counter + b counter + carry – 2 * temp carry = temp sum n = carry
20 Multiplication of Binary Integers Suppose we have 2 n-bit integers a and b; by distributive law, we have: n-1 n-1 ab = a * b j 2 j = a(b j 2 j ) j=0 j=0 Note: ab j = a if b j =1, ab j = 0 if b j = 0 Multiply by 2 means shift left and add 0 at end of expansion
21 Multiplication Example 1110 * shift 0000shift 0000shift 1110add
22 Pseudocode for multiplication algorithm Procedure multiply(inputs: a and b, binary expansions of integers with n digits) for (counter = 0; counter < n; counter++) if b counter == 1 ppcounter = a shifted counter spaces (partial product counter) else ppcounter = 0 product = 0 for (counter = 0; counter < n; counter++) product = product + ppcounter
23 Section 2.5 Integers and Algorithms -ends-