Number Theory - webdocs.cs.ualberta.ca · Number Theory Zachary Friggstad Programming Club Meeting....

Post on 31-Aug-2019

4 views 0 download

Transcript of Number Theory - webdocs.cs.ualberta.ca · Number Theory Zachary Friggstad Programming Club Meeting....

Number Theory

Zachary Friggstad

Programming Club Meeting

Outline

• Factoring

• Sieve

• Multiplicative Functions

• Greatest Common Divisors

• Applications

• Chinese Remainder Theorem

Throughout, problems to try are highlighted. Some are just routineimplementations of the algorithm recently discussed, some require abit of thought.

Advice

Number theory is a fundamental and very beautiful topic! But it ishard to visualize things, especially in the context of algorithms (unlikegraphs and geometry).

Even some standard things like running-time bounds are hard toestablish.

Trace some algorithms by hand on small examples to see what theyare doing! This helped me a lot.

Factoring

Theorem (Fundamental Theorem of Arithmetic)

Every integer n ≥ 2 can be uniquely expressed in the formpa11 · . . . · p

akk where p1 ≤ . . . ≤ pk are primes and ai ≥ 1 are integers.

We usually just try trial division to factor an integer n:

• Find the smallest integer p ≥ 2 dividing n.

• Divide it out (it must be a prime) and repeat.

Speedup: just try values p ≤√n, if anything remains it is a prime

since n cannot have two prime divisors >√n.

map<int , int> pr imes ;

for ( int p = 2 ; p∗p <= n ; ++p)while ( n%p == 0) {

++pr imes [ p ] ;n /= p ;

}if ( n > 1) ++pr imes [ n ] ;

for ( auto& x : p r imes ) {//x.first is a prime dividing n

//x.second is the number of times it divides n

}

Running Time: O(√n)

Problem to try:UVa 583 - Prime Factors

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Sieve: Find all primes ≤ n.

• Write all numbers from 2 to n.

• Find the smallest number p not highlighted.

• Highlight it and cross off larger multiples.

2, 3, 4, 5, 6, 7, 8, 9, 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

2, 3, 6 4, 5, 6 6, 7, 6 8, 6 9, 6 10

Crossed out numbers are multiples of smaller numbers: not prime.

Highlighted numbers are not multiples of smaller numbers: prime.

Speedup1) Only go up to

√n, anything not highlighted or crossed out must

be a prime since any composite number is divisible by a prime ≤√n.

Note: This is only a practical speedup, not an asymptotic speedup.

vec to r<int> pr imes ( n+1);for ( int i = 2 ; i <= n ; ++i ) p r imes [ i ] = i ;

for ( int p = 2 ; p∗p <= n ; ++p)if ( p r imes [ p ] == p) //if p is not crossed off yet

//then cross off multiples of p

for ( int q = 2∗p ; q <= n ; q += p)pr imes [ q ] = p ;

//now p is a prime if and only if primes[p] == p

//if p is composite , then primes[p] is a prime divisor of p

Running TimeThe inner loop iterates

∑p≤n prime

np = O(n log log n) times.

Speedup1) Only go up to

√n, anything not highlighted or crossed out must

be a prime since any composite number is divisible by a prime ≤√n.

Note: This is only a practical speedup, not an asymptotic speedup.

vec to r<int> pr imes ( n+1);for ( int i = 2 ; i <= n ; ++i ) p r imes [ i ] = i ;

for ( int p = 2 ; p∗p <= n ; ++p)if ( p r imes [ p ] == p) //if p is not crossed off yet

//then cross off multiples of p

for ( int q = 2∗p ; q <= n ; q += p)pr imes [ q ] = p ;

//now p is a prime if and only if primes[p] == p

//if p is composite , then primes[p] is a prime divisor of p

Running TimeThe inner loop iterates

∑p≤n prime

np = O(n log log n) times.

Speedup1) Only go up to

√n, anything not highlighted or crossed out must

be a prime since any composite number is divisible by a prime ≤√n.

Note: This is only a practical speedup, not an asymptotic speedup.

vec to r<int> pr imes ( n+1);for ( int i = 2 ; i <= n ; ++i ) p r imes [ i ] = i ;

for ( int p = 2 ; p∗p <= n ; ++p)if ( p r imes [ p ] == p) //if p is not crossed off yet

//then cross off multiples of p

for ( int q = 2∗p ; q <= n ; q += p)pr imes [ q ] = p ;

//now p is a prime if and only if primes[p] == p

//if p is composite , then primes[p] is a prime divisor of p

Running TimeThe inner loop iterates

∑p≤n prime

np = O(n log log n) times.

Multiplicative Functions

DefinitionA multiplicative function is a function f : Z≥1 → R satisfyingf (a · b) = f (a) · f (b) whenever gcd(a, b) = 1.

Examples:

• φ(n) = number of integers 1 ≤ k ≤ n with gcd(n, k) = 1.

• τ(n) = number of distinct positive divisors of n

• σ(n) = sum of all positive divisors of n

• µ(n) = 0 if p2|n for some p, otherwise is (−1)k where k is thenumber of distinct prime divisors of n.

Let f be multiplicative. If you can easily compute f (pa) for primes pand a ≥ 1, then you can compute f (n) for all n by factoring:

if n = pa11 · . . . · pakk then f (n) = f (pa11 ) · . . . · f (pakk ).

Examples:

• φ(pa) = pa−1 · (p − 1)

• τ(pa) = a + 1

• σ(pa) = 1 + p + p2 + . . .+ pa = pa+1−1p−1

• µ(pa) =

{−1 if a = 10 if a ≥ 2

Let f be multiplicative. If you can easily compute f (pa) for primes pand a ≥ 1, then you can compute f (n) for all n by factoring:

if n = pa11 · . . . · pakk then f (n) = f (pa11 ) · . . . · f (pakk ).

Examples:

• φ(pa) = pa−1 · (p − 1)

• τ(pa) = a + 1

• σ(pa) = 1 + p + p2 + . . .+ pa = pa+1−1p−1

• µ(pa) =

{−1 if a = 10 if a ≥ 2

Can conveniently compute f (n) for all values up to n with a sieve.Idea

• For each 2 ≤ k ≤ n, compute a prime divisor of k with a sieve.• Initialize f (1) = 1.• For some k ≥ 2, let p|k with multiplicity a.• Compute f (n) = f (n/pa) · f (pa).

Example

vec to r<int> pr imes ( n+1);//suppose we sieved so primes[p] is a prime divisor of p

vec to r<int> s igma (n+1);s igma [ 1 ] = 1 ;for ( int k = 2 ; k <= n ; ++k ) {

int s = 1 , p = pr imes [ k ] , m = k ;while (m % p == 0) {

m /= p ;s = s ∗p + 1 ;

} //invariant: s = sigma(p^i) after i iterations

s igma [ k ] = s ∗ s igma [m]}

Can conveniently compute f (n) for all values up to n with a sieve.Idea

• For each 2 ≤ k ≤ n, compute a prime divisor of k with a sieve.• Initialize f (1) = 1.• For some k ≥ 2, let p|k with multiplicity a.• Compute f (n) = f (n/pa) · f (pa).

Example

vec to r<int> pr imes ( n+1);//suppose we sieved so primes[p] is a prime divisor of p

vec to r<int> s igma (n+1);s igma [ 1 ] = 1 ;for ( int k = 2 ; k <= n ; ++k ) {

int s = 1 , p = pr imes [ k ] , m = k ;while (m % p == 0) {

m /= p ;s = s ∗p + 1 ;

} //invariant: s = sigma(p^i) after i iterations

s igma [ k ] = s ∗ s igma [m]}

Problems to try:UVa 10042 - Smith NumbersUVa 10738 - Riemann vs. MertensUVa 294 - Divisors

One more thought:Can factor in O(

√n/ log n) time with the following trick.

• Sieve all primes up to√n in O(

√n log log n) time.

• Do trial division up to√n to factor n, but iterate only over the

primes you sieved.

I’ve never needed this improvement, but it’s good to keep in mind.

Greatest Common Divisors

gcd(a, b) for integers a, b ≥ 0 is the largest integer d such that d |aand d |b.

Note gcd(a, 0) = a if a ≥ 1.Standard convention: gcd(0, 0) = 0.

Observationgcd(a, b) = gcd(a− b, b) if a ≥ bBecause anything that divides a and b also divides a± b.

Accelerated Subtractiongcd(a, b) = gcd(a mod b, b) (even if a < b)Because a mod b is obtained by repeatedly subtracting b from a.

Greatest Common Divisors

gcd(a, b) for integers a, b ≥ 0 is the largest integer d such that d |aand d |b.

Note gcd(a, 0) = a if a ≥ 1.Standard convention: gcd(0, 0) = 0.

Observationgcd(a, b) = gcd(a− b, b) if a ≥ bBecause anything that divides a and b also divides a± b.

Accelerated Subtractiongcd(a, b) = gcd(a mod b, b) (even if a < b)Because a mod b is obtained by repeatedly subtracting b from a.

Greatest Common Divisors

gcd(a, b) for integers a, b ≥ 0 is the largest integer d such that d |aand d |b.

Note gcd(a, 0) = a if a ≥ 1.Standard convention: gcd(0, 0) = 0.

Observationgcd(a, b) = gcd(a− b, b) if a ≥ bBecause anything that divides a and b also divides a± b.

Accelerated Subtractiongcd(a, b) = gcd(a mod b, b) (even if a < b)Because a mod b is obtained by repeatedly subtracting b from a.

Euclid’s algorithm to compute gcd(a, b).• If b = 0 then the answer is a.• Otherwise, the answer is gcd(b, a mod b) (even if a ≤ b).

int gcd ( int a , int b ) { return b ? gcd (b , a%b ) : a ; }

Running time: O(log a + log b) because a mod b ≤ a/2 if a ≥ b.

Quick Proof: Obvious if b ≤ a/2 since a mod b < b.Otherwise a mod b = a− b ≤ a/2.

Least Common MultipleFind the smallest integer m that is a common multiple of positiveintegers a, b.

Simply put: lcm(a, b) = a·bgcd(a,b) .

int lcm ( int a , int b ) { return a/gcd ( a , b )∗b ; }//division before multiplication may avoid overflow

Euclid’s algorithm to compute gcd(a, b).• If b = 0 then the answer is a.• Otherwise, the answer is gcd(b, a mod b) (even if a ≤ b).

int gcd ( int a , int b ) { return b ? gcd (b , a%b ) : a ; }

Running time: O(log a + log b) because a mod b ≤ a/2 if a ≥ b.

Quick Proof: Obvious if b ≤ a/2 since a mod b < b.Otherwise a mod b = a− b ≤ a/2.

Least Common MultipleFind the smallest integer m that is a common multiple of positiveintegers a, b.

Simply put: lcm(a, b) = a·bgcd(a,b) .

int lcm ( int a , int b ) { return a/gcd ( a , b )∗b ; }//division before multiplication may avoid overflow

Euclid’s algorithm to compute gcd(a, b).• If b = 0 then the answer is a.• Otherwise, the answer is gcd(b, a mod b) (even if a ≤ b).

int gcd ( int a , int b ) { return b ? gcd (b , a%b ) : a ; }

Running time: O(log a + log b) because a mod b ≤ a/2 if a ≥ b.

Quick Proof: Obvious if b ≤ a/2 since a mod b < b.Otherwise a mod b = a− b ≤ a/2.

Least Common MultipleFind the smallest integer m that is a common multiple of positiveintegers a, b.

Simply put: lcm(a, b) = a·bgcd(a,b) .

int lcm ( int a , int b ) { return a/gcd ( a , b )∗b ; }//division before multiplication may avoid overflow

Euclid’s algorithm to compute gcd(a, b).• If b = 0 then the answer is a.• Otherwise, the answer is gcd(b, a mod b) (even if a ≤ b).

int gcd ( int a , int b ) { return b ? gcd (b , a%b ) : a ; }

Running time: O(log a + log b) because a mod b ≤ a/2 if a ≥ b.

Quick Proof: Obvious if b ≤ a/2 since a mod b < b.Otherwise a mod b = a− b ≤ a/2.

Least Common MultipleFind the smallest integer m that is a common multiple of positiveintegers a, b.

Simply put: lcm(a, b) = a·bgcd(a,b) .

int lcm ( int a , int b ) { return a/gcd ( a , b )∗b ; }//division before multiplication may avoid overflow

Euclid’s algorithm to compute gcd(a, b).• If b = 0 then the answer is a.• Otherwise, the answer is gcd(b, a mod b) (even if a ≤ b).

int gcd ( int a , int b ) { return b ? gcd (b , a%b ) : a ; }

Running time: O(log a + log b) because a mod b ≤ a/2 if a ≥ b.

Quick Proof: Obvious if b ≤ a/2 since a mod b < b.Otherwise a mod b = a− b ≤ a/2.

Least Common MultipleFind the smallest integer m that is a common multiple of positiveintegers a, b.

Simply put: lcm(a, b) = a·bgcd(a,b) .

int lcm ( int a , int b ) { return a/gcd ( a , b )∗b ; }//division before multiplication may avoid overflow

Extended Euclidean Algorithm

Given integers a, b ≥ 0, for any other integers c , d we have thatgcd(a, b) divides ac + bd .

QuestionCan we find integers c , d such that ac + bd = gcd(a, b).

AnswerYes, and the Extended Euclidean Algorithm finds them.

Define a sequence of tuples (ri , si , ti ) for 0 ≤ i inductively as follows.

• r0 = a, r1 = b• s0 = 1, s1 = 0• t0 = 0, t1 = 1

Invariant, for any i will maintain a · si + b · ti = ri . True for i = 0, 1.

Inductively for i ≥ 2• qi = bri−2/ri−1c (quotient)• ri = ri−2 − qi · ri−1 (remainder) same as ri = ri−2 mod ri−1• si = si−2 − qi · si−1• ti = ti−2 − qi · ti−1

The r0, r1, r2, . . . sequence is just following Euclid’s gcd algorithm.

Consequence of the InvariantsLet j be the first index where rj = 0. Then

gcd(a, b) = rj−1 = sj−1 · a + tj−1 · b.

Define a sequence of tuples (ri , si , ti ) for 0 ≤ i inductively as follows.

• r0 = a, r1 = b• s0 = 1, s1 = 0• t0 = 0, t1 = 1

Invariant, for any i will maintain a · si + b · ti = ri . True for i = 0, 1.

Inductively for i ≥ 2• qi = bri−2/ri−1c (quotient)• ri = ri−2 − qi · ri−1 (remainder) same as ri = ri−2 mod ri−1• si = si−2 − qi · si−1• ti = ti−2 − qi · ti−1

The r0, r1, r2, . . . sequence is just following Euclid’s gcd algorithm.

Consequence of the InvariantsLet j be the first index where rj = 0. Then

gcd(a, b) = rj−1 = sj−1 · a + tj−1 · b.

Define a sequence of tuples (ri , si , ti ) for 0 ≤ i inductively as follows.

• r0 = a, r1 = b• s0 = 1, s1 = 0• t0 = 0, t1 = 1

Invariant, for any i will maintain a · si + b · ti = ri . True for i = 0, 1.

Inductively for i ≥ 2• qi = bri−2/ri−1c (quotient)• ri = ri−2 − qi · ri−1 (remainder) same as ri = ri−2 mod ri−1• si = si−2 − qi · si−1• ti = ti−2 − qi · ti−1

The r0, r1, r2, . . . sequence is just following Euclid’s gcd algorithm.

Consequence of the InvariantsLet j be the first index where rj = 0. Then

gcd(a, b) = rj−1 = sj−1 · a + tj−1 · b.

Example

Find x , y such that 21x + 27y = gcd(21, 27) = 3.

i qi ri si ti

0 − 21 1 0

1 − 27 0 1

2 0 21 1 0

3 1 6 −1 1

4 3 3 4 −3

5 2 0 −10 13

Therefore 3 = gcd(21, 27) = 21 · 4 + 27 · (−3).i.e. x = 4, y = −3

typedef pa i r<int , int> p i i ; //#include utility

void update ( p i i& p , int q ) {p = p i i ( p . second , p . f i r s t − q∗p . second ) ;

}

//returns gcd(r.first , r.second) and p is set so

//gcd(r.first , r.second) = p.first*r.first+p.second*r.second

int gcdex ( p i i r , p i i& p ) {p i i s ( 1 , 0 ) , t ( 0 , 1 ) ;while ( r . second ) {

int q = r . f i r s t / r . second ;update ( r , q ) ;update ( s , q ) ;update ( t , q ) ;

}p = p i i ( s . f i r s t , t . f i r s t ) ;return r . f i r s t ;

} //can prove |p.first| <= r.second , |p.second| <= r.first

p i i p ;int g = gcdex ( p i i ( a , b ) , p ) ;//now g = gcd(a,b) = a*p.first + b*p.second

Problem to try:UVa 10104 - Euclide Problem

Neat fact: the coefficients in the pair p you get from the ExtendedEuclidean algorithm discussed earlier will work, no modificationnecessary.

Applications

Modular InversesRecall a ≡ b mod m means m|(a− b).

Given a ∈ Z and m > 0 find b such that a · b ≡ 1 mod m.

Cleanup: We usually like to think of 0 ≤ a < m. If a ≥ m, then justcompute a mod m. If a < m, then we have to be more careful.

ISO Standard: In c++, if a < 0 and m > 0, then a % m is the“negative reminder closest to 0”.

Example: -17 % 5 == -2.

//assumes m > 0, returns the residue of a mod m in [0, m-1]

int sa fe mod ( int a , int m) { return ( a%m + m)%m; }

Applications

Modular InversesRecall a ≡ b mod m means m|(a− b).

Given a ∈ Z and m > 0 find b such that a · b ≡ 1 mod m.

Cleanup: We usually like to think of 0 ≤ a < m. If a ≥ m, then justcompute a mod m. If a < m, then we have to be more careful.

ISO Standard: In c++, if a < 0 and m > 0, then a % m is the“negative reminder closest to 0”.

Example: -17 % 5 == -2.

//assumes m > 0, returns the residue of a mod m in [0, m-1]

int sa fe mod ( int a , int m) { return ( a%m + m)%m; }

Applications

Modular InversesRecall a ≡ b mod m means m|(a− b).

Given a ∈ Z and m > 0 find b such that a · b ≡ 1 mod m.

Cleanup: We usually like to think of 0 ≤ a < m. If a ≥ m, then justcompute a mod m. If a < m, then we have to be more careful.

ISO Standard: In c++, if a < 0 and m > 0, then a % m is the“negative reminder closest to 0”.

Example: -17 % 5 == -2.

//assumes m > 0, returns the residue of a mod m in [0, m-1]

int sa fe mod ( int a , int m) { return ( a%m + m)%m; }

Applications

Modular InversesRecall a ≡ b mod m means m|(a− b).

Given a ∈ Z and m > 0 find b such that a · b ≡ 1 mod m.

Cleanup: We usually like to think of 0 ≤ a < m. If a ≥ m, then justcompute a mod m. If a < m, then we have to be more careful.

ISO Standard: In c++, if a < 0 and m > 0, then a % m is the“negative reminder closest to 0”.

Example: -17 % 5 == -2.

//assumes m > 0, returns the residue of a mod m in [0, m-1]

int sa fe mod ( int a , int m) { return ( a%m + m)%m; }

Recall, we are finding b such that a · b ≡ 1 mod m where m > 0.

If gcd(a,m) > 1, impossible.

Otherwise, use Euclid’s extended algorithm to find c , d such that

a · c + m · d = gcd(a,m) = 1.

So a · c ≡ 1 mod m.

//assumes m > 0, returns an integer b in [1, m-1] such that

// a * b equiv 1 mod m

int modinv ( int a , int m) {a = safe mod ( a , m) ; //ensure a >= 0

p i i p ;a s s e r t ( gcdex ( p i i ( a ,m) , p ) == 1 ) ;return sa fe mod ( p . f i r s t , m) ;

}

Recall, we are finding b such that a · b ≡ 1 mod m where m > 0.

If gcd(a,m) > 1, impossible.

Otherwise, use Euclid’s extended algorithm to find c , d such that

a · c + m · d = gcd(a,m) = 1.

So a · c ≡ 1 mod m.

//assumes m > 0, returns an integer b in [1, m-1] such that

// a * b equiv 1 mod m

int modinv ( int a , int m) {a = safe mod ( a , m) ; //ensure a >= 0

p i i p ;a s s e r t ( gcdex ( p i i ( a ,m) , p ) == 1 ) ;return sa fe mod ( p . f i r s t , m) ;

}

Recall, we are finding b such that a · b ≡ 1 mod m where m > 0.

If gcd(a,m) > 1, impossible.

Otherwise, use Euclid’s extended algorithm to find c , d such that

a · c + m · d = gcd(a,m) = 1.

So a · c ≡ 1 mod m.

//assumes m > 0, returns an integer b in [1, m-1] such that

// a * b equiv 1 mod m

int modinv ( int a , int m) {a = safe mod ( a , m) ; //ensure a >= 0

p i i p ;a s s e r t ( gcdex ( p i i ( a ,m) , p ) == 1 ) ;return sa fe mod ( p . f i r s t , m) ;

}

Problem to try:UVa 11174 - Stand in a Line

Neat Fact:If p 6 |a then ap−2 is the inverse of a modulo p, so we can computea−1 with fast exponentiation as well!. This is by Fermat’s littleTheorem, briefly discussed near the end of these slides.

But I encourage you to solve the above problem using the gcdapproach to get practice with it.

Linear Diophantine Equations

Given integers a, b, d , find integers x , y such that ax + by = d .

Idea: Find x ′, y ′ with ax ′ + by ′ = gcd(a, d). Scale x ′, y ′ byd/ gcd(a, b). Some fussing to handle negatives.

p i i l i n d i o p ( int a , int b , int d ) {p i i p ;int g ;g = gcdex ( p i i ( abs ( a ) , abs ( b ) ) , p ) ;a s s e r t ( d % g == 0 ) ; //impossible if d%g != 0

//now abs(a)*p.first + abs(b)*p.second == g

if ( a < 0) p . f i r s t = −p . f i r s t ;if ( b < 0) p . second = −p . second ;p . f i r s t ∗= d/g ;p . second ∗= d/g ;

return p ;}

Linear Diophantine Equations

Given integers a, b, d , find integers x , y such that ax + by = d .

Idea: Find x ′, y ′ with ax ′ + by ′ = gcd(a, d). Scale x ′, y ′ byd/ gcd(a, b). Some fussing to handle negatives.

p i i l i n d i o p ( int a , int b , int d ) {p i i p ;int g ;g = gcdex ( p i i ( abs ( a ) , abs ( b ) ) , p ) ;a s s e r t ( d % g == 0 ) ; //impossible if d%g != 0

//now abs(a)*p.first + abs(b)*p.second == g

if ( a < 0) p . f i r s t = −p . f i r s t ;if ( b < 0) p . second = −p . second ;p . f i r s t ∗= d/g ;p . second ∗= d/g ;

return p ;}

Linear Diophantine Equations

Given integers a, b, d , find integers x , y such that ax + by = d .

Idea: Find x ′, y ′ with ax ′ + by ′ = gcd(a, d). Scale x ′, y ′ byd/ gcd(a, b). Some fussing to handle negatives.

p i i l i n d i o p ( int a , int b , int d ) {p i i p ;int g ;g = gcdex ( p i i ( abs ( a ) , abs ( b ) ) , p ) ;a s s e r t ( d % g == 0 ) ; //impossible if d%g != 0

//now abs(a)*p.first + abs(b)*p.second == g

if ( a < 0) p . f i r s t = −p . f i r s t ;if ( b < 0) p . second = −p . second ;p . f i r s t ∗= d/g ;p . second ∗= d/g ;

return p ;}

Problem to try:UVa 10090 - Marbles

Note:The function lin diop only finds some solution x , y toa · x + b · y = d .However, given one such solution and letting g = gcd(a, b),

x(t) := x + t · bg

y(t) := y − t · ag

parameterizes all solutions as t ranges over integers. Use thisparameterization to find the “min-cost” solution.

Chinese Remaindering

If a is an integer such that a ≡ 4 mod 15 then we know a ≡ 1 mod 3and a ≡ 4 mod 5.

That is, 15|(a− 4) means surely 3|(a− 1) and 5|(a− 4).

What about the other way around? Given “target remainders” x , ymodulo 3 and 5 respectively, is there some integer a such thata ≡ x mod 3 and a ≡ y mod 5?

More generally

TheoremLet m, n be such that gcd(m, n) = 1. Then for any integers x , y thereexists an integer a such that a ≡ x mod m and a ≡ y mod n.

Chinese Remaindering

If a is an integer such that a ≡ 4 mod 15 then we know a ≡ 1 mod 3and a ≡ 4 mod 5.

That is, 15|(a− 4) means surely 3|(a− 1) and 5|(a− 4).

What about the other way around? Given “target remainders” x , ymodulo 3 and 5 respectively, is there some integer a such thata ≡ x mod 3 and a ≡ y mod 5?

More generally

TheoremLet m, n be such that gcd(m, n) = 1. Then for any integers x , y thereexists an integer a such that a ≡ x mod m and a ≡ y mod n.

Chinese Remaindering

If a is an integer such that a ≡ 4 mod 15 then we know a ≡ 1 mod 3and a ≡ 4 mod 5.

That is, 15|(a− 4) means surely 3|(a− 1) and 5|(a− 4).

What about the other way around? Given “target remainders” x , ymodulo 3 and 5 respectively, is there some integer a such thata ≡ x mod 3 and a ≡ y mod 5?

More generally

TheoremLet m, n be such that gcd(m, n) = 1. Then for any integers x , y thereexists an integer a such that a ≡ x mod m and a ≡ y mod n.

Idea: let m, n be the moduli and x , y the target remainders.

As gcd(m, n) = 1, compute integers m′, n′ such thatm ·m′ ≡ 1 mod n and n · n′ ≡ 1 mod m.

The answer is just x ·m ·m′ + y · n · n′ (try reducing mod m and n tosee why).

//assumes m,n > 0

//returns 0 <= a < m*n congruent to x mod m and y mod n

int chrem ( int x , m, int y , int n ) {int mi = modinv (m, n ) , n i = modinv (n , m) ;return sa fe mod ( x∗m∗mi + y∗n∗ ni , m∗n ) ;

}

More generally, given moduli m1, . . . ,mn > 0 and target remaindersx1, . . . , xn find an integer a such that a ≡ xi mod mi for each i .

Assumption: gcd(mi ,mj) = 1 for any i 6= j .

Base Case: if n = 1 just return a = x .Inductive Step

• Inductively construct b congruent to xi mod mi for i ≤ j .

• Solve the case n = 2 to find a congruent to b mod∏j

i=1mi andcongruent to xj+1 mod mj+1.

int ch r em mu l t i ( int ∗x , int ∗m, int n ) {int a = x [ 0 ] , mm = m[ 0 ] ;for ( int j = 0 ; j+1 < n ; ++j ) {

a = chrem (a , mm, x [ j +1] , m[ j +1 ] ) ;mm ∗= m[ j +1] ;

}return a ;

}

More generally, given moduli m1, . . . ,mn > 0 and target remaindersx1, . . . , xn find an integer a such that a ≡ xi mod mi for each i .

Assumption: gcd(mi ,mj) = 1 for any i 6= j .

Base Case: if n = 1 just return a = x .

Inductive Step

• Inductively construct b congruent to xi mod mi for i ≤ j .

• Solve the case n = 2 to find a congruent to b mod∏j

i=1mi andcongruent to xj+1 mod mj+1.

int ch r em mu l t i ( int ∗x , int ∗m, int n ) {int a = x [ 0 ] , mm = m[ 0 ] ;for ( int j = 0 ; j+1 < n ; ++j ) {

a = chrem (a , mm, x [ j +1] , m[ j +1 ] ) ;mm ∗= m[ j +1] ;

}return a ;

}

More generally, given moduli m1, . . . ,mn > 0 and target remaindersx1, . . . , xn find an integer a such that a ≡ xi mod mi for each i .

Assumption: gcd(mi ,mj) = 1 for any i 6= j .

Base Case: if n = 1 just return a = x .Inductive Step

• Inductively construct b congruent to xi mod mi for i ≤ j .

• Solve the case n = 2 to find a congruent to b mod∏j

i=1mi andcongruent to xj+1 mod mj+1.

int ch r em mu l t i ( int ∗x , int ∗m, int n ) {int a = x [ 0 ] , mm = m[ 0 ] ;for ( int j = 0 ; j+1 < n ; ++j ) {

a = chrem (a , mm, x [ j +1] , m[ j +1 ] ) ;mm ∗= m[ j +1] ;

}return a ;

}

More generally, given moduli m1, . . . ,mn > 0 and target remaindersx1, . . . , xn find an integer a such that a ≡ xi mod mi for each i .

Assumption: gcd(mi ,mj) = 1 for any i 6= j .

Base Case: if n = 1 just return a = x .Inductive Step

• Inductively construct b congruent to xi mod mi for i ≤ j .

• Solve the case n = 2 to find a congruent to b mod∏j

i=1mi andcongruent to xj+1 mod mj+1.

int ch r em mu l t i ( int ∗x , int ∗m, int n ) {int a = x [ 0 ] , mm = m[ 0 ] ;for ( int j = 0 ; j+1 < n ; ++j ) {

a = chrem (a , mm, x [ j +1] , m[ j +1 ] ) ;mm ∗= m[ j +1] ;

}return a ;

}

Problems to Try:Open Kattis - chineseremainderOpen Kattis - generalchineseremainder

We didn’t discuss how to solve the latter. There is a solution iffgcd(m, n)|(a− b).

• Can you see why this is necessary?

• Now generalize the “equation” we wrote to solve therelatively-prime modulus case.

Quadratic Residues

Say a is a quadratic residue mod m if b2 ≡ a mod m for some b.

In general, this is hard to determine for a given a,m (would yieldefficient factoring algorithms).

Easier if m is a prime.

Theorem (Euler’s Criterion)

Let a, p be integers with p prime. Then a is a quadratic residue mod

m if either p|a or ap−12 ≡ 1 mod p.

Use modular fast exponentiation to determine this (next meeting).

Very Interesting: If a is a quadratic residue mod p, find such aninteger b. This can be done efficiently, but is non-trivial.

Quadratic Residues

Say a is a quadratic residue mod m if b2 ≡ a mod m for some b.

In general, this is hard to determine for a given a,m (would yieldefficient factoring algorithms).

Easier if m is a prime.

Theorem (Euler’s Criterion)

Let a, p be integers with p prime. Then a is a quadratic residue mod

m if either p|a or ap−12 ≡ 1 mod p.

Use modular fast exponentiation to determine this (next meeting).

Very Interesting: If a is a quadratic residue mod p, find such aninteger b. This can be done efficiently, but is non-trivial.

Quadratic Residues

Say a is a quadratic residue mod m if b2 ≡ a mod m for some b.

In general, this is hard to determine for a given a,m (would yieldefficient factoring algorithms).

Easier if m is a prime.

Theorem (Euler’s Criterion)

Let a, p be integers with p prime. Then a is a quadratic residue mod

m if either p|a or ap−12 ≡ 1 mod p.

Use modular fast exponentiation to determine this (next meeting).

Very Interesting: If a is a quadratic residue mod p, find such aninteger b. This can be done efficiently, but is non-trivial.

Quadratic Residues

Say a is a quadratic residue mod m if b2 ≡ a mod m for some b.

In general, this is hard to determine for a given a,m (would yieldefficient factoring algorithms).

Easier if m is a prime.

Theorem (Euler’s Criterion)

Let a, p be integers with p prime. Then a is a quadratic residue mod

m if either p|a or ap−12 ≡ 1 mod p.

Use modular fast exponentiation to determine this (next meeting).

Very Interesting: If a is a quadratic residue mod p, find such aninteger b. This can be done efficiently, but is non-trivial.

Quadratic Residues

Say a is a quadratic residue mod m if b2 ≡ a mod m for some b.

In general, this is hard to determine for a given a,m (would yieldefficient factoring algorithms).

Easier if m is a prime.

Theorem (Euler’s Criterion)

Let a, p be integers with p prime. Then a is a quadratic residue mod

m if either p|a or ap−12 ≡ 1 mod p.

Use modular fast exponentiation to determine this (next meeting).

Very Interesting: If a is a quadratic residue mod p, find such aninteger b. This can be done efficiently, but is non-trivial.

Problem to Try:Open Kattis - quadres

The description is interesting, but a lot more complicated than itneeds to be :)

Tips

Primes are very dense: the number of primes ≤ n is ∼ nln n .

They also do not follow any obvious pattern. This is sometimeshelpful in heuristic reasoning about why an algorithm runs quickly.

Theorem (Euler’s Theorem)

Let a,m ≥ 1 be integers with gcd(a,m) = 1. Then aφ(m) ≡ 1 mod m.

Special case: Fermat’s little theorem, ap−1 ≡ 1 mod p.

Theorem (Wilson’s Theorem)

For a prime p, (p − 2)! ≡ 1 mod p.

Missing TopicsChinese remaindering when moduli are not relatively prime. Eitherthere is no solution or it is unique modulo the least-common multipleof all moduli.

Discrete logarithms, finding integer solutions for integer quadraticequations.

Finding integer solutions to a system of integer linear equations.

Next LectureTricks in Combinatorics and Arithmetic.

UVa - 11064 Number Theory (link)

Just compute φ(n)− 1, the number of values 1 < m < n that arerelatively prime to n.

n can be very big, so we can’t sieve.

Factor n =∏

i paii and compute φ(paii ) = pai−1i (p − 1) explicitly.

Treat n = 0, 1 with care!

UVa - 11064 Number Theory (link)

Just compute φ(n)− 1, the number of values 1 < m < n that arerelatively prime to n.

n can be very big, so we can’t sieve.

Factor n =∏

i paii and compute φ(paii ) = pai−1i (p − 1) explicitly.

Treat n = 0, 1 with care!

UVa - 11064 Number Theory (link)

Just compute φ(n)− 1, the number of values 1 < m < n that arerelatively prime to n.

n can be very big, so we can’t sieve.

Factor n =∏

i paii and compute φ(paii ) = pai−1i (p − 1) explicitly.

Treat n = 0, 1 with care!

UVa - 11064 Number Theory (link)

Just compute φ(n)− 1, the number of values 1 < m < n that arerelatively prime to n.

n can be very big, so we can’t sieve.

Factor n =∏

i paii and compute φ(paii ) = pai−1i (p − 1) explicitly.

Treat n = 0, 1 with care!

UVa - 11064 Number Theory (link)

Just compute φ(n)− 1, the number of values 1 < m < n that arerelatively prime to n.

n can be very big, so we can’t sieve.

Factor n =∏

i paii and compute φ(paii ) = pai−1i (p − 1) explicitly.

Treat n = 0, 1 with care!

UVa - 543 Goldbach’s Conjecture (link)

Well, Goldbach’s conjecture isn’t disproven yet so it is unlikely acorrect solution will not find a solution!

Heuristic Reasoning: Primes are dense (about nln n primes are ≤ n)

and don’t follow any simple pattern so for many primes p ≤ n itshould be that n − p is also a prime.

So just run through all p in increasing order and check that p, n − pare primes, it won’t take long before you get a hit. Sieve beforehandto make this check efficient.

While the worst case per input seems to be Θ(n), you will never seethis!

UVa - 543 Goldbach’s Conjecture (link)

Well, Goldbach’s conjecture isn’t disproven yet so it is unlikely acorrect solution will not find a solution!

Heuristic Reasoning: Primes are dense (about nln n primes are ≤ n)

and don’t follow any simple pattern so for many primes p ≤ n itshould be that n − p is also a prime.

So just run through all p in increasing order and check that p, n − pare primes, it won’t take long before you get a hit. Sieve beforehandto make this check efficient.

While the worst case per input seems to be Θ(n), you will never seethis!

UVa - 543 Goldbach’s Conjecture (link)

Well, Goldbach’s conjecture isn’t disproven yet so it is unlikely acorrect solution will not find a solution!

Heuristic Reasoning: Primes are dense (about nln n primes are ≤ n)

and don’t follow any simple pattern so for many primes p ≤ n itshould be that n − p is also a prime.

So just run through all p in increasing order and check that p, n − pare primes, it won’t take long before you get a hit. Sieve beforehandto make this check efficient.

While the worst case per input seems to be Θ(n), you will never seethis!

UVa - 543 Goldbach’s Conjecture (link)

Well, Goldbach’s conjecture isn’t disproven yet so it is unlikely acorrect solution will not find a solution!

Heuristic Reasoning: Primes are dense (about nln n primes are ≤ n)

and don’t follow any simple pattern so for many primes p ≤ n itshould be that n − p is also a prime.

So just run through all p in increasing order and check that p, n − pare primes, it won’t take long before you get a hit. Sieve beforehandto make this check efficient.

While the worst case per input seems to be Θ(n), you will never seethis!

UVa - 543 Goldbach’s Conjecture (link)

Well, Goldbach’s conjecture isn’t disproven yet so it is unlikely acorrect solution will not find a solution!

Heuristic Reasoning: Primes are dense (about nln n primes are ≤ n)

and don’t follow any simple pattern so for many primes p ≤ n itshould be that n − p is also a prime.

So just run through all p in increasing order and check that p, n − pare primes, it won’t take long before you get a hit. Sieve beforehandto make this check efficient.

While the worst case per input seems to be Θ(n), you will never seethis!

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:

Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10831 Gerg’s Cake (link)

Suppose we cut the cake into n × n squares and each present partyguest gets d pieces.

So n2 = d · p + a.

We already discussed what to do:Quadratic Residues. Just use Euler’s criteria.

Be careful about overflow in the calculations.

UVa - 10692 Huge Mods (link)

We want to compute aaa3 .

. .

21 mod m.

Let k := aa3. ..

2 mod φ(m).

If gcd(a1,m) = 1, it suffices to recursively compute k mod φ(m) andthen ak mod m.

In general, if k ≥ 14 then a[(k−14) mod φ(m)]+14 mod m suffices:Reason: because 14 ≥ log2m so any p|(a1,m) has p14 ≡ 0 mod m.

So write an ad-hoc function to check if k ≥ 14 or not. If not, thenjust explicitly calculate ak mod m. If so, do as above.

UVa - 10692 Huge Mods (link)

We want to compute aaa3 .

. .

21 mod m.

Let k := aa3. ..

2 mod φ(m).

If gcd(a1,m) = 1, it suffices to recursively compute k mod φ(m) andthen ak mod m.

In general, if k ≥ 14 then a[(k−14) mod φ(m)]+14 mod m suffices:Reason: because 14 ≥ log2m so any p|(a1,m) has p14 ≡ 0 mod m.

So write an ad-hoc function to check if k ≥ 14 or not. If not, thenjust explicitly calculate ak mod m. If so, do as above.

UVa - 10692 Huge Mods (link)

We want to compute aaa3 .

. .

21 mod m.

Let k := aa3. ..

2 mod φ(m).

If gcd(a1,m) = 1, it suffices to recursively compute k mod φ(m) andthen ak mod m.

In general, if k ≥ 14 then a[(k−14) mod φ(m)]+14 mod m suffices:Reason: because 14 ≥ log2m so any p|(a1,m) has p14 ≡ 0 mod m.

So write an ad-hoc function to check if k ≥ 14 or not. If not, thenjust explicitly calculate ak mod m. If so, do as above.

UVa - 10692 Huge Mods (link)

We want to compute aaa3 .

. .

21 mod m.

Let k := aa3. ..

2 mod φ(m).

If gcd(a1,m) = 1, it suffices to recursively compute k mod φ(m) andthen ak mod m.

In general, if k ≥ 14 then a[(k−14) mod φ(m)]+14 mod m suffices:Reason: because 14 ≥ log2m so any p|(a1,m) has p14 ≡ 0 mod m.

So write an ad-hoc function to check if k ≥ 14 or not. If not, thenjust explicitly calculate ak mod m. If so, do as above.

UVa - 10692 Huge Mods (link)

We want to compute aaa3 .

. .

21 mod m.

Let k := aa3. ..

2 mod φ(m).

If gcd(a1,m) = 1, it suffices to recursively compute k mod φ(m) andthen ak mod m.

In general, if k ≥ 14 then a[(k−14) mod φ(m)]+14 mod m suffices:Reason: because 14 ≥ log2m so any p|(a1,m) has p14 ≡ 0 mod m.

So write an ad-hoc function to check if k ≥ 14 or not. If not, thenjust explicitly calculate ak mod m. If so, do as above.