Docsity

Number Theory Algorithm, Lecture Notes - Computer Science, Study notes of Number Theory

Partial preview of the text

Download Number Theory Algorithm, Lecture Notes - Computer Science and more Study notes Number Theory in PDF only on Docsity!

Number Theory Algorithms

Zeph Grunschlag

Agenda

Euclidean Algorithm for GCD Number Systems  Decimal numbers (base-10)  Binary numbers (base-2)  One’s complement  Two’s complement  General base-b number systems Arithmetic Algorithms  Addition  Multiplication  Subtraction 1’s and 2’s complement

Euclidean Algorithm.

Example

gcd(33,77):

Step r = x mod y x y

0 -^33

Euclidean Algorithm.

Example

gcd(33,77):

Step r = x mod y x y

0 -^33

33 mod 77 = 33

Euclidean Algorithm.

Example

gcd(33,77):

Step r = x mod y x y

0 -^33

33 mod 77 = 33

77 mod 33 = 11

33 mod 11 = 0

Euclidean Algorithm.

Example

gcd(244,117):

Step r = x mod y x y

0 -^244

Euclidean Algorithm.

Example

gcd(244,117):

Step r = x mod y x y

0 -^244

1 244 mod 117 = 10 117 10 2 117 mod 10 = 7 10 7

Euclidean Algorithm.

Example

gcd(244,117):

Step r = x mod y x y

0 -^244

1 244 mod 117 = 10 117 10 2 117 mod 10 = 7 10 7 3 10 mod 7 = 3 7 3

Euclidean Algorithm.

Example

gcd(244,117):

By definition  244 and 117 are rel. prime.

Step r = x mod y x y

0 -^244

1 244 mod 117 = 10 117 10 2 117 mod 10 = 7 10 7 3 10 mod 7 = 3 7 3 4 7 mod 3 = 1 3 1 5 3 mod 1=0 1 0

Euclidean Algorithm

Correctness

The reason that Euclidean algorithm

works is gcd(x,y ) is not changed from

line to line. If x ’ , y ’ denote the next

values of x , y then:

gcd(x ’ ,y ’ ) = gcd(y, x mod y)

= gcd(y, x + qy) (the useful fact)

= gcd(y, x ) (subtract y -multiple)

= gcd(x,y)

Euclidean Algorithm

Running Time

integer euclid(m, n)

x = m, y = n

while( y > 0)

r = x mod y

x = y

y = r

return x

O (1) + ?  ( O (1) + O (1)

  • O (1)
  • O (1) )
  • O (1) =?  O(1)

Where “?” is the number of while-loop iterations.

Assuming mod operation is O (1):

Euclidean Algorithm

Running Time

Facts: (x ’ = next value of x, etc. )

  1. x can only be less than y at very beginning of algorithm - once x > y, x ’ = y > y ’ = x mod y
  2. When x > y, two iterations of while loop

guarantee that new x is < ½ original x

- because x ’’ = y ’ = x mod y. Two cases: I. y > ½ x  x mod y = x – y < ½ x II. y ≤ ½ x  x mod y < y ≤ ½ x

Euclidean Algorithm

Running Time

After 2m+1 steps, x < original_x / 2^ m

A: While-loop exits when y is 0, which is

right before “would have” gotten x =

0. Exiting while-loop happens when

2 m^ > original_x, so definitely by:

m = log 2 ( original_x )

Therefore running time of algorithm is:

O(2m+1) = O(m) = O (log 2 (max(a,b)) )

Euclidean Algorithm

Running Time

Measuring input size in terms of n = number of

digits of max(a,b):

n = (log 10 (max(a,b)) ) = (log 2 (max(a,b)) )

Therefore running time of algorithm is:

O(log 2 (max(a,b)) ) = O(n)

(assumed naively that mod is an O(1)

operation, so estimate only holds for fixed- size integers such as int’s and long’s)