Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Integers and Algorithms Representations of integers

Similar presentations


Presentation on theme: "Chapter Integers and Algorithms Representations of integers"— Presentation transcript:

1 Chapter 3 3.6 Integers and Algorithms Representations of integers
Algorithms for integer operations Modular Exponentiation The Euclidean Algorithm

2 Representations of integers
Theorem 1: Let b be a positive integer greater than 1. Then if n is a positive integer, it can be expressed uniquely in the form where k is a nonnegative integer, a0, a1, …,ak are nonnegative integers less than b, and ak ≠0.

3 Example 1: What is the decimal expansion of the integer that has ( )2 as its binary expansion? Example 2: What is the decimal expansion of the hexadecimal expansion of (2AE0B)16 ?

4 Example 3: Find the base 8, or octal, expansion of (12345)10
Example 4: Find the hexadecimal expansion of (177130)10?

5 Algorithm 1: Construction Base b Expansions
procedure base b expansion(n:positive integer) q: = n k: =0 while q ≠ 0 begin ak : =q mod b q: = k: =k+1 end {the base b expansion of n is (ak a1 a0)b}

6

7 Algorithms for integer operations
Algorithm 2: Addition of Integers Procedure add(a , b:positive integers) {the binary expansions of a and b are (an a1 a0)2 and (bn b1 b0)2 respectively} c : =0 for j: =0 to n-1 Begin d : = sj : = aj+bj+c-2d c : =d end sn:=c {the binary expansion of the sum if (sn sn s1 s0)2 }

8 Algorithms for integer operations
Example 7: Add a=(1110)2 and b=(1011)2.

9 Algorithms for integer operations
Algorithm 3 : Multiplying Integers procedure multiply(a, b : positive integers) {the binary expansions of and b are(an a1 a0)2 and (bn b1 b0)2 respectively} for j:=0 to n-1 Begin if bj =1 then cj=a shifted j places else cj:=0 end {c0 c cn-1 are the partial products} p :=0 for j:=0 to n-1 p: = p +cj {p is the value of ab}

10 Algorithms for integer operations
Example 9: Find the product of a= (110)2 and b=(101)2

11 Algorithms for integer operations
Algorithm 4 : Computing div and mod procedure division algorithm(a :integers ,d: positive integer) q: =0 r: =|a| while r≧d begin r := r-d q :=q+1 end if a<0 then if r=0 then q:=-q else r := d-r q := -(q+1) {q = a div d is the quotient, r = a mod d is the remainder}

12 Modular Exponentiation
In cryptography it is important to be able to find bn mod m efficiently, where b, n and m and large integers. It’s impractical to first compute bn and then find its remainder when divided by m because bn will be a huge number. Instead, we can use an algorithm that employ expansion of the exponent n , say n = (ak a1 a0)2 . Before we present this algorithm, we illustrate its basic idea. We will explain how to use the binary expansion of n to compute bn .First , note that

13 Modular Exponentiation
To compute bn , we find the values of b, b2,(b2)2=b4, (b4)2=b8, , We multiply the terms in this list, where aj=1 . This gives us For example, to compute 311 we first note that 11 = (1011)2, so that 311= By successively squaring, we find that 32=9, 34=81, 38=6561. Consequently,311=383231=6561*9*3= 177,147

14 Modular Exponentiation
Algorithm 5: Modular Exponentiation procedure modular exponentiation(b:integer , n=(ak a1 a0)2 ,m: positive integer) x: = 1 power := b mod m for i=0 to k-1 begin if ai =1 then x :=(x*power) mod m power :=(power*power) mod m End {x equals bn mod m} Example 11: Use Algorithm 5 to find 3644 mod 645.

15 The Euclidean Algorithm
Lemma 1: Let a=bq+r ,where a, b, q, and r are integers. Then gcd(a,b)=gcd(b,r). Algorithm 6: The Euclidean Algorithm procedure gcd(a.b:integers) x: = a y: = b while y0 begin r := x mod y x := y y := r end {gcd(a,b) is x}

16 The Euclidean Algorithm
Example 12: Find the GCD of 414 and 662 using the Euclidean Algorithm.


Download ppt "Chapter Integers and Algorithms Representations of integers"

Similar presentations


Ads by Google