CSE390D Notes for Monday, 10/14/24
terminology:
if gcd(a, b) = 1, we say a and b are relatively prime
the integers a1, a2, ..., an are pairwise relatively prime if
gcd(ai, aj) = 1 whenever 1 <= i < j <= n
proof of Fermat's Little Theorem:
consider a, 2a, 3a, ..., (p-1)a with gcd(p, a) = 1
properties of this set of numbers:
p-1 of them
none in the 0 congruence class (p and a are relatively prime)
claim:
no two in the same congruence class
proof:
proof by contradiction
suppose x * a = y * a for 1 <= x < y <= p-1
then (x * a) - (y * a) = p * k for some integer k
a * (x - y) = p * k
a and p are relatively prime, so p does not divide a
by the fundamental theorem of arithmetic, p | (x - y)
this is impossible because (x - y) < p
qed
look at their product two ways
the product: (a)(2a)(3a)...((p-1)a)
the product of one number from each congruence class (use canonincal
values): (p-1)!
(a)(2a)(3a)...((p-1)a) == (p-1)! (mod p)
a^(p-1) * (p-1)! == (p-1)! (mod p)
a^(p-1) == 1 (mod p)
why isn't this division by zero? because all of 2 through p-1 are
not factors of p (this is where it's important that p is prime)
-------------------------------------------------------------------------------
Then write modpow in Python
how to write modpow, computing
a ^ b mod n
start with:
def modpow(a, b, n):
result = 1
while b > 0:
result = result * a
b = b - 1
return result % n
need to mod intermediate results:
result = (result * a) % n
now optimize for even exponents:
# invariant: result * a^b = (original a) ^ (original b)
while b > 0:
if b % 2 == 0:
b = b / 2
a = a * a
else:
result = (result * a) % m
b = b - 1
still slow...need to % the square
a = a * a % n
# computes a^b (mod m)
def modpow(a, b, m):
result = 1
# invariant result * a^b == (original a)^(original b) (mod m)
while (b > 0):
if b % 2 == 0:
b = b / 2
a = a * a % m
else:
result = result * a % m
b = b - 1
return result % m
-------------------------------------------------------------------------------
talked about Java isProbablePrime, primality testing
Look at Prime.java
-------------------------------------------------------------------------------
example modpow computation...what is:
778 ^ 648 (mod 11)
== 8 ^ 648
== (64) ^ 324
== (9) ^ 324
== (81) ^ 162
== 4 ^ ^ 162
== 16 ^ 81
== 5 * 81
== 5 * 5^80
== 5 * 25^40
== 5 * 3^40
== 5 * 9^20
== 5 * 81^10
== 5 * 4^10
== 5 * 16^5
== 5 * 5^5
== 5 * 5 * 5^4
= 3 * 5^4
= 3 * 25^2
= 3 * 3^2
= 3 * 9
= 27
= 5
Stuart Reges
Last modified: Mon Oct 14 13:20:38 PDT 2024