Algorithms · UGC NET: Selected and Advanced Algorithms
Number Theoretic Algorithms
Overview
Number theoretic algorithms use facts about integers—divisibility, primes, greatest common divisors and congruences—to solve computational problems efficiently.
In the UGC NET syllabus this lesson sits under Selected and Advanced Algorithms and includes Euclid’s algorithm, modular exponentiation, primality testing, the Chinese Remainder Theorem and RSA. The required background is elementary number theory: divisibility, prime numbers, modular congruence, and basic asymptotic complexity.
The notes use the standard CLRS convention for modular exponentiation and the usual definition of Euler’s totient function. These ideas also appear in problems that use modular arithmetic, such as double hashing.
Key concepts
- Divisibility: means there exists an integer with . If , then is a divisor of .
- Greatest common divisor: is the largest positive integer dividing both and . Two integers are coprime or relatively prime if .
- Euclid’s algorithm repeatedly replaces by until the second number is . Then the first number is .
- Lamé’s theorem: for , the number of division steps in Euclid’s algorithm is at most five times the number of decimal digits of . The worst case occurs for consecutive Fibonacci numbers: and .
- Extended Euclidean algorithm returns not only but also integers such that . These coefficients are called Bézout coefficients.
- Modular congruence: means . Arithmetic modulo uses remainders .
- Multiplicative inverse modulo : an integer has an inverse modulo if and only if . When it exists, the inverse is unique modulo .
- Euler’s totient function counts the integers with and . For , is the size of , the set of residue classes coprime to .
- Euler’s theorem: if , then .
- Fermat’s little theorem: if is prime and , then . Equivalently, for every integer .
- Carmichael numbers are composite such that for every coprime to . The smallest Carmichael number is . They defeat the simple Fermat primality test.
- Miller–Rabin primality test is a one-sided probabilistic test: it never rejects a prime, but a composite may pass a round. With random bases, the error probability is at most .
- Chinese Remainder Theorem (CRT): if are pairwise coprime, a system has a unique solution modulo .
- RSA uses , , a public exponent with , and private exponent .
- Double hashing uses a secondary hash function for the probe step: . For full table coverage, should be coprime to .
How it works
-
Euclid’s GCD algorithm.
Assume . Recompute . Since remainders strictly decrease, the process stops. Correctness follows from Algorithm:EUCLID(a, b): while b != 0: t = b b = a mod b a = t return aThe number of divisions is . On Fibonacci inputs the division count is largest, which is Lamé’s theorem.
-
Extended Euclidean algorithm.
It keeps track of quotients during Euclid’s algorithm and back-substitutes:EXTENDED-EUCLID(a, b): if b == 0: return (a, 1, 0) (d, x1, y1) = EXTENDED-EUCLID(b, a mod b) x = y1 y = x1 - (a // b) * y1 return (d, x, y)The returned satisfy .
-
Modular inverse.
To find , run extended Euclid on . It gives exactly when . Then is the inverse. If the gcd is not , no inverse exists. -
Binary modular exponentiation.
Follow the CLRS algorithm. Let have binary bits , with bits.MODULAR-EXPONENTIATION(a, b, n): d = 1 for i = k down to 0: d = (d * d) mod n if b_i == 1: d = (d * a) mod n return dCLRS performs one squaring for every bit of , and one multiplication by for every -bit of . Thus the total number of modular multiplications is . If a variant skips the leading -bit, the count is . State the convention before counting.
-
Primality testing.
- Trial division: test divisors up to . If none divide , then is prime.
- Fermat test: choose with . If , then is composite. If it is , is only a probable prime; Carmichael numbers fool this test.
- Miller–Rabin: write , where is odd. Choose a base . Compute . If it is or , declare probable prime. Otherwise repeatedly square at most times; if some value becomes , declare probable prime.
If not, is composite. A prime always passes; a composite passes with probability at most per random base.
-
Chinese Remainder Theorem.
For pairwise coprime moduli , set , , and . Then The solution is unique modulo . -
RSA key generation and use.
Choose distinct large primes ; compute and . Choose with and . Compute . Public key is ; private key is . Encryption is ; decryption is .
Drawing the diagram…
Formulas
- Euclid step: , with .
- Bézout identity: .
- Congruence: .
- Inverse: , exists iff .
- Euler phi: . Also , , and for distinct primes.
- Euler’s theorem: .
- Fermat’s little theorem: prime, ; and for all integers .
- CRT: for pairwise coprime , has unique solution modulo .
- RSA: , , , , , .
- Double hashing: , .
- CLRS modular exponentiation count: modular multiplications, where .
Worked examples
Example 1: Euclid and extended Euclid.
Find and write it as .
So . Back-substitute:
Thus . So .
Example 2: Euler totient.
Compute . Since ,
The 12 coprime residues are .
Example 3: modular exponentiation and CRT.
Compute . Since :
- Modulo : , so .
- Modulo : , and . Now , , so .
- Modulo : , so .
Solve , , . From the first two, . Modulo : Hence . So .
Example 4: RSA numeric two-step.
Let , , , and message . Then
Find with . Since , .
Encryption:
So the ciphertext is . Decryption check: . Repeated squaring gives , , , . Therefore
The plaintext returns as .
Example 5: statement-type.
State whether each is true or false.
- For prime , for every integer . True; this is Fermat’s little theorem in the “all integers” form.
- If for all coprime to , then is prime. False; Carmichael number is composite and satisfies this for all coprime .
- An inverse of modulo exists exactly when . True.
Comparison
| Concept | Main condition | Conclusion | Common trap |
|---|---|---|---|
| Fermat’s little theorem | prime, | needs coprimality; holds for all | |
| Euler’s theorem | It does not say all work | ||
| Carmichael number | Composite | Fermat test can pass for all coprime bases | Passing Fermat does not prove prime |
| Trial division | Test divisors up to | Deterministic primality | Testing only up to is wasteful, not wrong but slower |
| Algorithm | Returns | Complexity | Use |
|---|---|---|---|
| Euclid GCD | divisions | Basic gcd | |
| Extended Euclid | and with | Same order | Modular inverse, CRT |
| Binary modular exponentiation | modular multiplications | RSA, primality tests | |
| CRT | Unique solution modulo product | Depends on moduli arithmetic | Speeding RSA and solving congruences |
| Term | Formula or fact | Example |
|---|---|---|
| Double hashing probe | Step size is , not alone | |
| RSA public exponent | , works |
Revision summary
- Modular inverse exists iff ; otherwise do not try to compute it.
- Fermat uses exponent only when ; holds for all .
- Euler’s theorem generalizes Fermat from prime modulus to any modulus, but only for coprime .
- The smallest Carmichael number is , so one passed Fermat test does not prove primality.
- Lamé’s theorem: worst case of Euclid is Fibonacci inputs; steps are at most five times the decimal digits of the smaller number.
- CLRS modular exponentiation does squarings plus one multiplication for each -bit of the exponent.
- CRT needs pairwise coprime moduli and gives uniqueness modulo their product.
- In RSA, is coprime to , not to .
- Double hashing uses , not .
- Miller–Rabin is one-sided: primes never fail, composites may pass a round.
- Trial division checks up to , not up to .
- is a useful quick check for totient calculations.
Previous-year exam insights
- Direct formula questions ask the exact probe sequence for double hashing; check that multiplies the second hash value inside the modulus.
- Statement I/II questions test Fermat’s little theorem, Euler’s theorem, and the difference between and .
- Assertion–reason questions use Carmichael numbers to show that a composite can pass a Fermat test for coprime bases.
- Match-the-list questions pair concepts such as , modular exponentiation complexity, Euclid’s worst case, and RSA inverse conditions.
- Code-tracing questions ask for the value returned by Euclid or extended Euclid; trace the recursion or loop exactly and keep coefficient signs.
- Numeric questions give RSA parameters and ask for or ciphertext; first compute and , then find the inverse of , then exponentiate.
- Modular exponentiation can be combined with CRT, as in computing a large power modulo a composite .
- Traps include using coprime to instead of , forgetting the condition , and counting modular multiplications without fixing the CLRS convention.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Run Euclid’s algorithm on any pair and state the gcd and number of division steps.
- Use extended Euclid to write and find a modular inverse.
- Decide whether a given has an inverse modulo , and compute it when it exists.
- Compute from prime factorization for numbers such as , , and .
- Apply Fermat’s little theorem and Euler’s theorem correctly, including coprimality conditions.
- Identify why is a Carmichael number and why it matters for primality testing.
- Trace Miller–Rabin for a small and explain its one-sided error.
- Solve a two-congruence CRT system and then a three-congruence system.
- Perform RSA key generation, encryption and decryption for small primes.
- Count modular multiplications in CLRS binary exponentiation and trace the algorithm on exponents such as .
Attempt the PARIKSHA sub-topic test first, then the subject test.