Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Elementary Number Theory

Pelatnas 1 TOKI 2017


Turfa Auliarachman (TOKI 2015)
Ahmad Zaky (TOMI 2010-2012)
Table of Contents
• Modular Arithmetic
• Prime Numbers
• Multiplicative Functions
• Euclidean Algorithm
• Diophantine Equations
Modular Arithmetic
• 𝑎 ≡ 𝑏 mod 𝑚 iff 𝑎 − 𝑏 is divisible by 𝑚

• If 𝑎 ≡ 𝑐 and 𝑏 ≡ 𝑑 (mod 𝑚), then


𝑎 ± 𝑏 ≡ 𝑐 ± 𝑑 (mod 𝑚)
𝑎 × 𝑏 ≡ 𝑐 × 𝑑 (mod 𝑚)
𝑎𝑘 ≡ 𝑐 𝑘 (mod 𝑚) for all integers 𝑘
𝑎
≡ ? (mod 𝑚)
𝑏
Prime Number
• Positive integers which have exactly 2 positive
divisors

• 2, 3, 5, 7, ...
Primality Testing
• Obvious 𝑂( 𝑛) loop

• Sieve Eratosthenes (𝑂 𝑛 log log 𝑛 )

– It is fast; 186ms for 𝑛 = 107


Theorems Involving Primes
• Fermat Little Theorem
𝑎𝑝 ≡ 𝑎 (mod 𝑝)
𝑎𝑝−1 ≡ 1 (mod 𝑝) for 𝑎 ≠ 0 (mod 𝑝)

• Wilson’s Theorem
𝑛 − 1 ! ≡ −1 (mod 𝑛) if and only if 𝑛 isprime
Multiplicative Functions
• 𝑓: 𝑁 → 𝑁 such that 𝑓 𝑥𝑦 = 𝑓 𝑥 𝑓(𝑦) for all
relatively prime numbers 𝑥 and 𝑦 (i.e.
gcd 𝑥, 𝑦 = 1)

• Obvious example: 𝑓 𝑥 = 𝑥
Sigma, Tau, Phi
• 𝜎(𝑥) denotes the sum of positive divisors of 𝑥
• 𝜏(𝑥) denotes the number of positive divisors
of 𝑥
• 𝜙 𝑥 (also called Euler totient function)
denotes the number of 𝑖 such that 𝑖 ≤ 𝑥 and
gcd 𝑖, 𝑥 = 1

• All of them are multiplicative functions


Sigma, Tau, Phi
𝑒1 𝑒2 𝑒𝑘
• Let 𝑥 = 𝑝1 𝑝2 … 𝑝𝑘
𝑒𝑖 +1
𝑝𝑖 −1
• 𝜎 𝑥 = ς𝑘𝑖=1
𝑝𝑖 −1

• 𝜏 𝑥 = ς𝑘𝑖=1 𝑒𝑖 + 1
1
• 𝜙 𝑥 = 𝑥 ς𝑘𝑖=1 1−
𝑝𝑖
Euler’s Theorem
• 𝑎𝜙(𝑛) ≡ 1 mod 𝑛 if gcd 𝑎, 𝑛 = 1
Euclidean Algorithm
• gcd 𝑎, 𝑏 = gcd 𝑏, 𝑎 − 𝑏
Extended Euclidean Algorithm
• Find 𝑥 and 𝑦 such that 𝑎𝑥 + 𝑏𝑦 = gcd(𝑎, 𝑏)
• Computes sequence of remainders and
quotients:
– 𝑟0 = 𝑎
– 𝑟1 = 𝑏
–…
– 𝑟𝑖+1 = 𝑟𝑖−1 − 𝑞𝑖 𝑟𝑖 and 0 ≤ 𝑟𝑖+1 < |𝑟𝑖 |
Extended Euclidean Algorithm
• Also maintain two other sequences 𝑠𝑖 and 𝑡𝑖
𝑠0 = 1 𝑠1 = 0
𝑡0 = 0 𝑡1 = 1

𝑠𝑖+1 = 𝑠𝑖−1 − 𝑞𝑖 𝑠𝑖
𝑡𝑖+1 = 𝑡𝑖−1 − 𝑞𝑖 𝑡𝑖

• We stop when 𝑟𝑘+1 = 0


gcd 𝑎, 𝑏 = 𝑟𝑘 = 𝑎𝑠𝑘 + 𝑏𝑡𝑘
Extended Euclidean Algorithm
Multiplicative Inverse
𝑎
• = 𝑎 ∗? (mod 𝑝)
𝑏

• Extended Euclid
Find 𝑥 and 𝑦 such that 𝑥𝑝 + 𝑦𝑏 = 1
?= 𝑦
Linear Diophantine Equation
• Find 𝑥 and 𝑦 satisfying 𝑎𝑥 + 𝑏𝑦 = 𝑐

• Solution exists iff 𝑐 is divisible by gcd(𝑎, 𝑏)

• If 𝑥0 , 𝑦0 is a solution, then
𝑦 𝑥
𝑥0 + 𝑘 gcd 𝑥,𝑦 , 𝑦0 − 𝑘 gcd 𝑥,𝑦 is
Problem: Benefit (UVa11889)
• Given 𝑎 and 𝑐, find the smallest positive
integer 𝑏 such that 𝑙𝑐𝑚 𝑎, 𝑏 = 𝑐

𝑎, 𝑐 ≤ 107
Problem: Permasalahan DP (P1 2014)
• If 𝑓(𝑎, 𝑏) is known, then so is 𝑓(𝑎 × 𝑘, 𝑏 × 𝑘)
• How many values of 𝑓 should be calculated to
find all 𝑓 𝑎, 𝑏 , 1 ≤ 𝑎, 𝑏 ≤ 𝑛?
SPOJ GCDEX
G=0;
for(i = 1 ; i < N ; i++)
for(j = i+1 ; j <= N ; j++)
G+=gcd(i,j);

Cari nilai G, 1 < N < 1000001

You might also like