Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching Modular arithmetic Today: Exponentiation Multiplicative Inverses Extended Euclid’s algorithm CMPSC 360 11/29/2018
Modular exponentiation Naive algorithm to compute 𝑥 𝑦 mod 𝑚. mod-exp-naive(𝒙,𝒚,𝒎) Input: integer 𝒙, natural number 𝒚, positive integer 𝒎. if 𝒚=𝟎 then return 1 else 𝒛= mod-exp-naive(𝒙,𝒚−𝟏,𝒎) return (𝒛⋅𝒙 mod 𝒎) 11/29/2018
I-clicker problem (frequency: BC) If 𝑦 has 𝑏 bits, how many multiplications mod 𝑚 are performed when we compute 𝑥 𝑦 mod 𝑚 by the naive algorithm? Find the largest correct bound: At least √𝑏. At least 𝑏. At least 2𝑏. At least 𝑏 2 . At least 2 𝑏−1 . 11/29/2018
Modular exponentiation Uses repeated squaring to compute 𝑥 𝑦 mod 𝑚. mod-exp(𝒙,𝒚,𝒎) Input: integer 𝒙, natural number 𝒚, positive integer 𝒎. if 𝒚=𝟎 then return 1 else 𝒛= mod-exp(𝒙,𝒚 div 𝟐,𝒎) if 𝒚 mod 2 =𝟎 then return (𝒛⋅𝒛 mod 𝒎) else return (𝒛⋅𝒛⋅𝒙 mod 𝒎) 11/29/2018
I-clicker problem (frequency: BC) If 𝑦 has 𝑏 bits, how many multiplications mod 𝑚 are performed when we compute 𝑥 𝑦 mod 𝑚 by repeated squaring? Find the smallest correct bound: At most √𝑏. At most 𝑏. At most 2𝑏. At most 𝑏 2 . At most 2 𝑏−1 . 11/29/2018
Definitions If 𝑥⋅𝑦≡1 (mod 𝑚) then 𝑦 is a multiplicative inverse of 𝑥 modulo 𝑚. The greatest common divisor of natural numbers 𝑥 and 𝑦, denoted gcd(𝑥,𝑦), is the largest natural number that divides both. If gcd(𝑥,𝑦)=1 then 𝑥 and 𝑦 are relatively prime (also called coprime). 11/29/2018