7 Number Theoretic Algorithms

Design and Analysis of Algorithms · Unit 7 · 5 hrs

Number Theoretic Algorithms

Exam-focused notes for Number Theoretic Algorithms (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

Number Theoretic Algorithms in short

Number theoretic algorithms underlie modern cryptography and hashing: this unit covers the divisibility notation the rest of the topic is written in, Euclid's algorithm and its extended form, how to solve modular linear equations and simultaneous congruences, and a fast randomized way to test primality.

Number Theoretic Algorithms: notes

Number Theoretic Notations, Euclid's and Extended Euclid's Algorithms and their Analysis

Divisibility. The notation $d \mid a$ ("$d$ divides $a$") means $a = kd$ for some integer $k$, and then $d$ is called a divisor of $a$ and $a$ a multiple of $d$. A divisor of $a$ satisfies $1 \le d \le |a|$ (for $a \ne 0$). An integer $a > 1$ whose only divisors are $1$ and itself is prime; an integer $a>1$ that is not prime is composite; $1$ is neither, and nor is $0$ or any negative integer. If $d \mid a$ and $d \mid b$, $d$ is a common divisor of $a$ and $b$, and the greatest common divisor $\gcd(a,b)$ is the largest such $d$ (with $\gcd(0,0)=0$ by convention). Two integers with $\gcd(a,b)=1$ are relatively prime.

Euclid's algorithm computes $\gcd(a,b)$ using the identity $\gcd(a,b) = \gcd(b, a \bmod b)$, with $\gcd(a,0)=a$:

int euclidGcd(int a, int b) {
    if (b == 0) return a;
    return euclidGcd(b, a % b);
}

Correctness rests on the fact that any common divisor of $a$ and $b$ is also a common divisor of $b$ and $a \bmod b$, and vice versa, since $a = qb + (a \bmod b)$ for the quotient $q$; hence the two pairs have exactly the same set of common divisors, so in particular the same greatest one. Analysis: each recursive call replaces $(a,b)$ with $(b, a \bmod b)$, and it is a classical fact (tied to consecutive Fibonacci numbers being the slowest-shrinking case) that $a \bmod b < a/2$ within two steps, so the arguments shrink geometrically and the recursion depth, hence the running time, is $O(\log(\min(a,b)))$. Tracing $\gcd(30,21)$: $\gcd(30,21) = \gcd(21, 30 \bmod 21) = \gcd(21,9) = \gcd(9, 21\bmod9) = \gcd(9,3) = \gcd(3, 9\bmod3) = \gcd(3,0) = 3$.

What this unit covers

  • Number Theoretic Notations, Euclid's and Extended Euclid's Algorithms and their Analysis
  • Solving Modular Linear Equations, Chinese Remainder Theorem, Primility Testing: Miller-Rabin Randomized Primility Test and their Analysis

Number Theoretic Notations, Euclid's and Extended Euclid's Algorithms and their Analysis

2082 · 5 marks

Using Extended Euclidean Algorithm, find the GCD of 12 and 16. [5]

Show solved answer

Extended Euclidean Algorithm: GCD of 12 and 16

Given

  • First integer: $a = 16$ (larger)
  • Second integer: $b = 12$

Goal: Find $\gcd(16, 12)$ and integers $x, y$ such that $16x + 12y = \gcd(16,12)$.


Solution

Forward Pass (Euclidean Algorithm)

StepDivisionQuotient $q$Remainder $r$
1$16 = 12 \times 1 + 4$14
2$12 = 4 \times 3 + 0$30

The last non-zero remainder is $4$, so:

$$\gcd(16, 12) = 4$$

Back-Substitution (Extended Part)

Express the GCD as a linear combination $16x + 12y = 4$.

From Step 1:

$$4 = 16 - 12 \times 1$$

Rearranging:

$$4 = 16 \times (1) + 12 \times (-1)$$

Therefore:

$$x = 1, \quad y = -1$$

Verification

$$16 \times (1) + 12 \times (-1) = 16 - 12 = 4 \checkmark$$

Also confirming GCD independently: divisors of 12 are ${1,2,3,4,6,12}$, divisors of 16 are ${1,2,4,8,16}$; the greatest common divisor is indeed $4$.


Result

ResultValue
$\gcd(12, 16)$$4$
Coefficient $x$ (for 16)$1$
Coefficient $y$ (for 12)$-1$
Bezout identity$16(1) + 12(-1) = 4$
Open this answer on its own page →

2080 · 5 marks

Why extended euclidean algorithm is used? Write down its algorithm and analyze its complexity. [5]

The Extended Euclidean Algorithm is used to find not only the Greatest Common Divisor (GCD) of two integers a and b, but also the integer coefficients x and y such that: ax + by = gcd(a, b) This is based on Bezout's Identity. It is widely used in: - Computing modular multiplicative inverses (esse...

Open this answer on its own page →

Solving Modular Linear Equations, Chinese Remainder Theorem, Primility Testing

2081 · 5 marks

Solve the following linear equation using Chinese Remainder Theorem. x = 1 MOD 3,x = 2 MOD 5,x = 0 MOD 7 [5]

$$x \equiv 1 \pmod{3}, \quad x \equiv 2 \pmod{5}, \quad x \equiv 0 \pmod{7}$$ $i$ remainder $ai$ modulus $mi$ --------- 1 1 3 2 2 5 3 0 7 Check coprimality: $\gcd(3,5)=\gcd(5,7)=\gcd(3,7)=1$. Moduli are pairwise coprime, so CRT gives a unique solution mod $M$. Product of moduli: $$M = 3 \times 5 ...

Open this answer on its own page →

2079 · 5 marks

Solve the following linear congruences using Chinese Remainder Theorem.x≡1(mod2),x≡3(mod5),x≡6(mod7)x \equiv 1 \pmod{2},\quad x \equiv 3 \pmod{5},\quad x \equiv 6 \pmod{7}x≡1(mod2),x≡3(mod5),x≡6(mod7) [5]

$$x \equiv 1 \pmod{2}, \quad x \equiv 3 \pmod{5}, \quad x \equiv 6 \pmod{7}$$ Residues: $a1 = 1,\ a2 = 3,\ a3 = 6$ Moduli: $m1 = 2,\ m2 = 5,\ m3 = 7$ The moduli are pairwise coprime, so a unique solution exists modulo their product. --- $$M = 2 \times 5 \times 7 = 70$$ --- $$M1 = 70/2 = 35, \quad...

Open this answer on its own page →