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

Prof. Zeph Grunschlag, Computer Science, Number Theory Algorithms, Euclidean Algorithm for GCD, Decimal numbers, Binary numbers, One’s complement, Two’s complement, Arithmetic Algorithms, Columbia, Lecture Notes

Typology: Study notes

2010/2011

Uploaded on 11/05/2011

yorket
yorket 🇺🇸

4.4

(38)

276 documents

1 / 86

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Number Theory Algorithms
Zeph Grunschlag
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56

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)