+ All Categories
Home > Documents > Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If...

Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If...

Date post: 13-Jul-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
58
START RECORDING
Transcript
Page 1: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

START

RECORDING

Page 2: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Mod ArithmeticCMSC250

Page 3: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Modular Arithmetic

Page 4: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Page 5: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Page 6: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Properties of congruence

Page 7: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Properties of congruence

Page 8: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Properties of congruence

Page 9: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Properties of congruence

Page 10: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Proof with modular arithmetic

Page 11: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More proofs

Page 12: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More proofs

Page 13: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More Proofs Using Mod•

Page 14: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More Proofs Using Mod•

Page 15: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More Proofs Using Mod

by properties of congruence, we have that 𝑛2 ≡ 12 ≡ 1 (𝑚𝑜𝑑 2). Done.

Page 16: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

More Proofs Using Mod

by properties of congruence, we have that 𝑛2 ≡ 12 ≡ 1 (𝑚𝑜𝑑 2). Done.

Page 17: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Algorithms on divisibility

1. Modular Exponentiation (Repeated Squaring)2. Greatest Common Divisor (GCD)

Page 18: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Basic assumptions

• 𝑎 + 𝑏 and 𝑎 ⋅ 𝑏 have unit cost• This is not true if 𝑎, 𝑏 are too large

Page 19: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem

How fast can we compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 𝑛,𝑚 ∈ ℕ ?

Page 20: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem

How fast can we compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 𝑛,𝑚 ∈ ℕ ?

1. Obviously, we can compute 𝑎𝑛 = 𝑎 × 𝑎 ×⋯× 𝑎 and mod that large

number by 𝑚. 𝑛 𝑡𝑖𝑚𝑒𝑠

Page 21: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem

How fast can we compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 𝑛,𝑚 ∈ ℕ ?

1. Obviously, we can compute 𝑎𝑛 = 𝑎 × 𝑎 ×⋯× 𝑎 and mod that large

number by 𝑚.

• Problems:• Arithmetic overflow in computation of 𝑎𝑛

• Modding a large quantity is tough on the FPU

𝑛 𝑡𝑖𝑚𝑒𝑠

Page 22: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem, second approach

2. We could start computing 𝑎 × 𝑎 ×⋯× 𝑎 until the product becomes larger than 𝑚, reduce and repeat until we’re done.

Page 23: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem, second approach

2. We could start computing 𝑎 × 𝑎 ×⋯× 𝑎 until the product becomes larger than 𝑚, reduce and repeat until we’re done.

• Problems:• Arithmetic overflow in computation of 𝑎𝑛

• Modding a large quantity is tough on the FPU

Page 24: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem, second approach

2. We could start computing 𝑎 × 𝑎 ×⋯× 𝑎 until the product becomes larger than 𝑚, reduce and repeat until we’re done.

• Problems:• Arithmetic overflow in computation of 𝑎𝑛

• Modding a large quantity is tough on the FPU

• Additionally, we have another nice property…

Page 25: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem

• How fast can we compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 𝑛,𝑚 ∈ ℕ ?

We always need 𝑛steps

We can do it in

roughly 𝑛 steps

We can do it in roughly

log𝑛 stepsSomething Else

Page 26: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

First problem

• How fast can we compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 𝑛,𝑚 ∈ ℕ ?

We always need 𝑛steps

We can do it in

roughly 𝑛 steps

We can do it in roughly

log𝑛 stepsSomething Else

Page 27: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

Page 28: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

Page 29: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

Page 30: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

Page 31: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

3. 323≡ 32

2 2≡ 812 ≡ 27

Page 32: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

3. 323≡ 32

2 2≡ 812 ≡ 27

4. 324≡ 32

3 2≡ 272 ≡ 36

Page 33: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

3. 323≡ 32

2 2≡ 812 ≡ 27

4. 324≡ 32

3 2≡ 272 ≡ 36

5. 325≡ 32

4 2≡ 362 ≡ 9

Page 34: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).

1. 321≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

3. 323≡ 32

2 2≡ 812 ≡ 27

4. 324≡ 32

3 2≡ 272 ≡ 36

5. 325≡ 32

4 2≡ 362 ≡ 9

6. 326≡ 9 2 ≡ 81

Page 35: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 364 𝑚𝑜𝑑 99 in log2 64 = 6 steps.

• All ≡ are ≡ (mod 99).1. 32

1≡ 9

2. 322≡ 32 2 ≡ 92 ≡ 81

3. 323≡ 32

2 2≡ 812 ≡ 27

4. 324≡ 32

3 2≡ 272 ≡ 36

5. 325≡ 32

4 2≡ 362 ≡ 9

6. 326≡ 9 2 ≡ 81

• Aha! 364 = 326≡ 81

Page 36: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Good news, bad news

• Good news: By using repeated squaring, can compute 𝑎2ℓ𝑚𝑜𝑑 𝑚

quickly (roughly ℓ = log2 2ℓ steps)

• Bad news: What if our exponent is not a power of 2?

Page 37: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example

• Computing 327 𝑚𝑜𝑑 99 with the same method

• All ≡ are ≡ (mod 99).• 31 ≡ 3

• 32 ≡ 9

• 322≡ 32 2 ≡ 92 ≡ 81

• 323≡ 32

2 2≡ 812 ≡ 27

• 324≡ 32

3 2≡ 272 ≡ 36

• 327 = 316 × 38 × 32 × 31 ≡ 36 × 27 × 9 × 3

Page 38: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Example (contd.)

• To avoid large numbers, reduce product as you go:

• 327 = 316 × 38 × 32 × 31 ≡ 36 × 27 × 9 × 3 ≡

36 × 27 × 9 × 3 ≡ 81 × 27 ≡ 9

Page 39: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Exercise

• Solve the following for 𝑟 please!

534 ≡ 𝑟 ( 𝑚𝑜𝑑 117)

Page 40: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Algorithm to compute 𝑎𝑛 𝑚𝑜𝑑 𝑚 in log𝑛 steps

• Step 1: Write 𝑛 = 2𝑞1 + 2𝑞2 +⋯+ 2𝑞𝑟, 𝑞1< 𝑞2 < ⋯ < 𝑞𝑟

• Step 2: Note that 𝑎𝑛 = 𝑎2𝑞1+2𝑞2+⋯+2𝑞𝑟 = 𝑎2

𝑞1 ×⋯× 𝑎2𝑞𝑟

• Step 3: Use repeated squaring to compute:

𝑎20, 𝑎2

1, 𝑎2

2, … , 𝑎2

𝑞𝑟𝑚𝑜𝑑 𝑚

using 𝑎2𝑖+1

≡ 𝑎2𝑖 2

𝑚𝑜𝑑 𝑚

• Step 4: Compute 𝑎2𝑞1 ×⋯× 𝑎2

𝑞𝑟mod m reducing when necessary

to avoid large numbers

Page 41: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

The key step

• The key step is Step #3: Use repeated squaring to compute:

𝑎20, 𝑎2

1, 𝑎2

2, … , 𝑎2

𝑞𝑟𝑚𝑜𝑑 𝑚

using 𝑎2𝑖+1

≡ 𝑎2𝑖 2

𝑚𝑜𝑑 𝑚

• When computing 𝑎2𝑖+1

mod m, already have computed 𝑎2𝑖 2

𝑚𝑜𝑑 𝑚

• Note that all numbers are below 𝑚 because we reduce mod m every step of the way

• So 𝑎2𝑖 2

is unit cost and anything mod m is also unit cost!

Page 42: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

Page 43: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

• What is the GCD of…• 10 and 15?

Page 44: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

• What is the GCD of…• 10 and 15? 5

• 12 and 90?

Page 45: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

• What is the GCD of…• 10 and 15? 5

• 12 and 90? 6

• 20 and 29?

Page 46: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

• What is the GCD of…• 10 and 15? 5

• 12 and 90? 6

• 20 and 29? 1 (20 and 29 are called co-prime or relatively prime)

• 153 and 181

Page 47: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Second problem: Greatest Common Divisor (GCD)

• If 𝑎, 𝑏 ∈ ℕ≠0, then the GCD of 𝑎, 𝑏 is the largest non-zero integer 𝑛

such that 𝑛 |𝑎 and 𝑛 | 𝑏

• What is the GCD of…• 10 and 15? 5

• 12 and 90? 6

• 20 and 29? 1 (20 and 29 are called co-prime or relatively prime)

• 153 and 181 1 (also co-prime)

Page 48: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Euclid’s GCD algorithm

• Recall: If 𝑎 ≡ 0 (𝑚𝑜𝑑 𝑚) and 𝑏 ≡ 0 𝑚𝑜𝑑 𝑚 , then 𝑎 − 𝑏 ≡ 0 (𝑚𝑜𝑑 𝑚)

• The GCD algorithm finds the greatest common divisor by executing this recursion (assume a > b):

𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑎, 𝑏 − 𝑎

Until its arguments are the same.

Page 49: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Greatest Common Divisor (GCD)

• Recall: If 𝑎 ≡ 0 (𝑚𝑜𝑑 𝑚) and 𝑏 ≡ 0 𝑚𝑜𝑑 𝑚 , then 𝑎 − 𝑏 ≡ 0 (𝑚𝑜𝑑 𝑚)

• The GCD algorithm finds the greatest common divisor by executing this recursion (assume a > b):

𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑎, 𝑏 − 𝑎

Until its arguments are the same.

• Question: If we implement this in a programming language, it can only be done recursively

Yes

(why)

No

(Why)

Something Else

(What)

Page 50: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Greatest Common Divisor (GCD)

• Recall: If 𝑎 ≡ 0 (𝑚𝑜𝑑 𝑚) and 𝑏 ≡ 0 𝑚𝑜𝑑 𝑚 , then 𝑎 − 𝑏 ≡ 0 (𝑚𝑜𝑑 𝑚)

• The GCD algorithm finds the greatest common divisor by executing this recursion (assume a > b):

𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑎, 𝑏 − 𝑎

Until its arguments are the same.

• Question: If we implement this in a programming language, it can only be done recursively

Yes

(why)

No

(Why)

Something Else

(What)

left = a;right = b;while(left != right){

if(left > right)left = left – right;

elseright = right - left;

}print "GCD is: " left; // Or right

Tail

recursion

Page 51: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

GCD example

• GCD(18, 100) =GCD(18, 100 – 18) = GCD(18, 82)=GCD(18, 82 – 18 = GCD(18, 64) = GCD(18, 64 – 18) = GCD(18, 46) = GCD(18, 46 – 18) = GCD(18, 28) =GCD(18, 28 – 18) = GCD(18, 10) =GCD(18 - 10, 10) = GCD(8, 10)=GCD(8, 10 - 8)= GCD(8, 2) =GCD(8 - 2, 2) = GCD(6, 2) =GCD(6 - 2, 2) = GCD(4, 2) = GCD(4- 2, 2) = GCD(2, 2) = 2

Page 52: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

GCD example

• GCD(18, 100) =GCD(18, 100 – 18) = GCD(18, 82)=GCD(18, 82 – 18 = GCD(18, 64) = GCD(18, 64 – 18) = GCD(18, 46) = GCD(18, 46 – 18) = GCD(18, 28) =GCD(18, 28 – 18) = GCD(18, 10) =GCD(18 - 10, 10) = GCD(8, 10)=GCD(8, 10 - 8)= GCD(8, 2) =GCD(8 - 2, 2) = GCD(6, 2) =GCD(6 - 2, 2) = GCD(4, 2) = GCD(4- 2, 2) = GCD(2, 2) = 2

Given integers 𝑎, 𝑏 with 𝑎 > 𝑏 (without loss of

generality), approximately how many steps

does this algorithm take?

a steps b steps

Something Elsea-b steps

Page 53: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

GCD example

• GCD(18, 100) =GCD(18, 100 – 18) = GCD(18, 82)=GCD(18, 82 – 18 = GCD(18, 64) = GCD(18, 64 – 18) = GCD(18, 46) = GCD(18, 46 – 18) = GCD(18, 28) =GCD(18, 28 – 18) = GCD(18, 10) =GCD(18 - 10, 10) = GCD(8, 10)=GCD(8, 10 - 8)= GCD(8, 2) =GCD(8 - 2, 2) = GCD(6, 2) =GCD(6 - 2, 2) = GCD(4, 2) = GCD(4- 2, 2) = GCD(2, 2) = 2

Given integers 𝑎, 𝑏 with 𝑎 > 𝑏 (without loss of

generality), approximately how many steps

does this algorithm take?

a steps b steps

Something Elsea-b steps

Roughly

𝑎 𝑏

Page 54: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Can we do better?

• GCD(18, 100) =GCD(18, 100 – 18) = GCD(18, 82)=GCD(18, 82 – 18 = GCD(18, 64) = GCD(18, 64 – 18) = GCD(18, 46) = GCD(18, 46 – 18) = GCD(18, 28) =GCD(18, 28 – 18) = GCD(18, 10) =GCD(18 - 10, 10) = GCD(8, 10)=GCD(8, 10 - 8)= GCD(8, 2) =GCD(8 - 2, 2) = GCD(6, 2) =GCD(6 - 2, 2) = GCD(4, 2) = GCD(4- 2, 2) = GCD(2, 2) = 2

Yes NoSomething

Else

Page 55: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

Can we do better?

• GCD(18, 100) =GCD(18, 100 – 18) = GCD(18, 82)=GCD(18, 82 – 18) = GCD(18, 64) = GCD(18, 64 – 18) = GCD(18, 46) = GCD(18, 46 – 18) = GCD(18, 28) =GCD(18, 28 – 18) = GCD(18, 10) =GCD(18 - 10, 10) = GCD(8, 10)=GCD(8, 10 - 8)= GCD(8, 2) =GCD(8 - 2, 2) = GCD(6, 2) =GCD(6 - 2, 2) = GCD(4, 2) = GCD(4- 2, 2) = GCD(2, 2) = 2

Yes NoSomething

Else

GCD(18, 100 – 5 x 18)

GCD(8 – 3 x 2, 2)

GCD(18, 100) =

GCD(18, 100 – 5 x 18) = GCD(18,

10) =

GCD(18 – 10, 10) = GCD(8, 10) =

GCD(8, 10 - 8) = GCD(8, 2) =

GCD(8 – 3 x 2, 2) = GCD(2, 2) = 2

From 10 to 4 steps!

Page 56: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

How fast is this new algorithm?

• Given non-zero integers 𝑎, 𝑏 with 𝑎 > 𝑏, roughly how many steps does this new algorithm take to compute GCD(a, b)?

𝑎 𝑏2 𝑎 Something Elselog2 𝑎

Page 57: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

• Given non-zero integers 𝑎, 𝑏 with 𝑎 > 𝑏, roughly how many steps does this new algorithm take to compute GCD(a, b)?

• In fact, it takes log𝜙 𝑎, where 𝜙 =1+ 5

2is the golden ratio.

• Proof by Gabriel Lamé in 1844, considered by some to be the first everresult in Algorithmic Complexity theory.

log2 𝑎

How fast is this new algorithm?

𝑎 𝑏2 𝑎 Something Else

Page 58: Divisibility and mod arithmetic · 2021. 1. 24. · Greatest Common Divisor (GCD) • Recall: If ≡0( )and ≡0 ,then − ≡0( ) • The GCD algorithm finds the greatest common

STOP

RECORDING


Recommended