• No results found

Mod Arithmetic CMSC250

N/A
N/A
Protected

Academic year: 2021

Share "Mod Arithmetic CMSC250"

Copied!
58
0
0

Loading.... (view fulltext now)

Full text

(1)

START

(2)

Mod Arithmetic

(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)

Proof with modular arithmetic

(11)

More proofs

(12)

More proofs

(13)

More Proofs Using Mod

(14)

More Proofs Using Mod

(15)

More Proofs Using Mod

(16)

More Proofs Using Mod

(17)

Algorithms on divisibility

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

(18)

Basic assumptions

𝑎 + 𝑏 and 𝑎 ⋅ 𝑏 have

unit cost

(19)

First problem

(20)

First problem

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

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

(21)

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

(22)

First problem, second approach

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

(23)

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 𝑎𝑛

(24)

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

(25)

First problem

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

We always need 𝑛 steps

We can do it in roughly 𝑛 steps

We can do it in roughly

(26)

First problem

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

We always need 𝑛 steps

We can do it in roughly 𝑛 steps

We can do it in roughly

(27)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

(28)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

(29)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

(30)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9

(31)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9

2. 322 ≡ 32 2 ≡ 92 ≡ 81 3. 323 ≡ 322 2 ≡ 812 ≡ 27

(32)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9

2. 322 ≡ 32 2 ≡ 92 ≡ 81 3. 323 ≡ 322 2 ≡ 812 ≡ 27

(33)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9

2. 322 ≡ 32 2 ≡ 92 ≡ 81 3. 323 ≡ 322 2 ≡ 812 ≡ 27

4. 324 ≡ 323 2 ≡ 272 ≡ 36

(34)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9 2. 322 ≡ 32 2 ≡ 92 ≡ 81 3. 323 ≡ 322 2 ≡ 812 ≡ 27 4. 324 ≡ 323 2 ≡ 272 ≡ 36 5. 325 ≡ 324 2 ≡ 362 ≡ 9 6. 326 ≡ 9 2 ≡ 81

(35)

Example

• Computing 364 𝑚𝑜𝑑 99 in log

2 64 = 6 steps. • All ≡ are ≡ (mod 99).

1. 321 ≡ 9 2. 322 ≡ 32 2 ≡ 92 ≡ 81 3. 323 ≡ 322 2 ≡ 812 ≡ 27 4. 324 ≡ 323 2 ≡ 272 ≡ 36 5. 325 ≡ 324 2 ≡ 362 ≡ 9 6. 326 ≡ 9 2 ≡ 81 • Aha! 364 = 326 ≡ 81

(36)

Good news, bad news

• Good news: By using repeated squaring, can compute 𝑎2ℓ 𝑚𝑜𝑑 𝑚 quickly (roughly ℓ = log2 2ℓ steps)

(37)

Example

• Computing 327 𝑚𝑜𝑑 99 with the same method • All ≡ are ≡ (mod 99).

• 31 3 • 32 9 • 322 ≡ 32 2 ≡ 92 ≡ 81 • 323 ≡ 322 2 ≡ 812 27 • 324 ≡ 323 2 ≡ 272 36 • 327 = 316 × 38 × 32 × 31 36 × 27 × 9 × 3

(38)

Example (contd.)

To avoid large numbers, reduce product as you go:

3

27

= 3

16

× 3

8

× 3

2

× 3

1

36

×

27

×

9

×

3

(39)

Exercise

• Solve the following for 𝑟 please!

(40)

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, 𝑎21, 𝑎22, … , 𝑎2𝑞𝑟 𝑚𝑜𝑑 𝑚 using 𝑎2𝑖+1 ≡ 𝑎2𝑖 2 𝑚𝑜𝑑 𝑚

• Step 4: Compute 𝑎2𝑞1 × ⋯ × 𝑎2𝑞𝑟 mod m reducing when necessary to avoid large numbers

(41)

The key step

• The key step is Step #3: Use repeated squaring to compute: 𝑎20, 𝑎21, 𝑎22, … , 𝑎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

(42)

Second problem: Greatest Common Divisor

(GCD)

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

(43)

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…

(44)

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

(45)

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

(46)

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)

(47)

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)

(48)

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.

(49)

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)

(50)

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; else

right = right - left; }

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

Tail

(51)

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

(52)

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 Else a-b steps

(53)

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 Else a-b steps Roughly 𝑎 𝑏

(54)

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 No Something Else

(55)

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 No Something 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!

(56)

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)?

𝑎

(57)

• 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

2 is the golden ratio.

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

log2𝑎

How fast is this new algorithm?

𝑎

(58)

STOP

References

Related documents

digits long) is split into two numbers with half the number of digits.. After s

However, NSE caused a statis- tically significant decrease in free cholesterol content of white adipose tissue cells in a group of obesity- induced IR rats (Fig. 4).. Cholesterol

South Africa, Argentina, Australia, Brazil (2007), Canada, China, Colombia, Korea, Egypt, USA, Philippines, Honduras, Japan, Malaysia, Mexico, Russia, Switzerland, Taiwan,

Lemma 4 If a positive amount of informed trading occurs in equilibrium and the cost of information production is below , then ex-ante expected wealth of the

Motivated by this work, we prove that the stochastic quasilinear viscoelastic wave equation (1.1) can blow up with positive probability or it is explosive in energy sense for p &gt; {

tors for traffic signs and automobiles), flat array (on the moon), spherical array (e.g., the Laser Geodynamic Satellite (LAGEOS)), etc. Some CCRs have an intentional

Aspen Plus provides a convenient function (Design Specs/Vary) which can iterate operating variables to meet the specification.. Add New Design Specs Add New Design

Untuk pengembangan industri kimia berbasis batubara (minsalnya pencairan dan gasifikasi batubara) lokasi yang potensial mengembangkannya adalah di Kabupaten