1. Gyanastra
  2. Algorithms
  3. UGC NET: Selected and Advanced Algorithms
  4. Number Theoretic Algorithms
Free lesson, no account needed 10 min read 22 practice questions after it
Asked in past papers3 times
  • GATE CSE2004
  • ISRO2018
  • UGC NET2019

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: a∣ba\mid b means there exists an integer kk with b=akb=ak. If a∣ba\mid b, then aa is a divisor of bb.
  • Greatest common divisor: gcd⁡(a,b)\gcd(a,b) is the largest positive integer dividing both aa and bb. Two integers are coprime or relatively prime if gcd⁡(a,b)=1\gcd(a,b)=1.
  • Euclid’s algorithm repeatedly replaces (a,b)(a,b) by (b,a mod b)(b,a\bmod b) until the second number is 00. Then the first number is gcd⁡(a,b)\gcd(a,b).
  • Lamé’s theorem: for a>b>0a>b>0, the number of division steps in Euclid’s algorithm is at most five times the number of decimal digits of bb. The worst case occurs for consecutive Fibonacci numbers: FkF_k and Fk+1F_{k+1}.
  • Extended Euclidean algorithm returns not only d=gcd⁡(a,b)d=\gcd(a,b) but also integers x,yx,y such that ax+by=da x+b y=d. These coefficients are called Bézout coefficients.
  • Modular congruence: a≡b(modn)a\equiv b\pmod n means n∣(a−b)n\mid(a-b). Arithmetic modulo nn uses remainders 0,1,…,n−10,1,\dots,n-1.
  • Multiplicative inverse modulo nn: an integer aa has an inverse a−1a^{-1} modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1. When it exists, the inverse is unique modulo nn.
  • Euler’s totient function ϕ(n)\phi(n) counts the integers kk with 1≤k≤n1\le k\le n and gcd⁡(k,n)=1\gcd(k,n)=1. For n>1n>1, ϕ(n)\phi(n) is the size of Zn∗Z_n^*, the set of residue classes coprime to nn.
  • Euler’s theorem: if gcd⁡(a,n)=1\gcd(a,n)=1, then aϕ(n)≡1(modn)a^{\phi(n)}\equiv1\pmod n.
  • Fermat’s little theorem: if pp is prime and gcd⁡(a,p)=1\gcd(a,p)=1, then ap−1≡1(modp)a^{p-1}\equiv1\pmod p. Equivalently, ap≡a(modp)a^p\equiv a\pmod p for every integer aa.
  • Carmichael numbers are composite nn such that an−1≡1(modn)a^{n-1}\equiv1\pmod n for every aa coprime to nn. The smallest Carmichael number is 561=3⋅11⋅17561=3\cdot11\cdot17. 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 kk random bases, the error probability is at most 4−k4^{-k}.
  • Chinese Remainder Theorem (CRT): if n1,…,nrn_1,\dots,n_r are pairwise coprime, a system x≡ai(modni)x\equiv a_i\pmod{n_i} has a unique solution modulo N=n1n2⋯nrN=n_1n_2\cdots n_r.
  • RSA uses n=pqn=pq, ϕ(n)=(p−1)(q−1)\phi(n)=(p-1)(q-1), a public exponent ee with gcd⁡(e,ϕ(n))=1\gcd(e,\phi(n))=1, and private exponent d≡e−1(modϕ(n))d\equiv e^{-1}\pmod{\phi(n)}.
  • Double hashing uses a secondary hash function for the probe step: h(k,i)=(h1(k)+i h2(k)) mod mh(k,i)=(h_1(k)+i\,h_2(k))\bmod m. For full table coverage, h2(k)h_2(k) should be coprime to mm.

How it works

  1. Euclid’s GCD algorithm.
    Assume a≥b>0a\ge b>0. Recompute a mod ba\bmod b. Since remainders strictly decrease, the process stops. Correctness follows from gcd⁡(a,b)=gcd⁡(b,a mod b).\gcd(a,b)=\gcd(b,a\bmod b). Algorithm:

    EUCLID(a, b):
        while b != 0:
            t = b
            b = a mod b
            a = t
        return a
    

    The number of divisions is O(log⁡min⁡(a,b))O(\log\min(a,b)). On Fibonacci inputs the division count is largest, which is Lamé’s theorem.

  2. 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 x,yx,y satisfy ax+by=da x+b y=d.

  3. Modular inverse.
    To find a−1 mod na^{-1}\bmod n, run extended Euclid on a,na,n. It gives ax+ny=1a x+n y=1 exactly when gcd⁡(a,n)=1\gcd(a,n)=1. Then x mod nx\bmod n is the inverse. If the gcd is not 11, no inverse exists.

  4. Binary modular exponentiation.
    Follow the CLRS algorithm. Let bb have binary bits bk…b0b_k\dots b_0, with β=k+1\beta=k+1 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 d
    

    CLRS performs one squaring for every bit of bb, and one multiplication by aa for every 11-bit of bb. Thus the total number of modular multiplications is β+popcount⁡(b)\beta+\operatorname{popcount}(b). If a variant skips the leading 11-bit, the count is (β−1)+(popcount⁡(b)−1)(\beta-1)+(\operatorname{popcount}(b)-1). State the convention before counting.

  5. Primality testing.

    • Trial division: test divisors up to n\sqrt n. If none divide nn, then nn is prime.
    • Fermat test: choose aa with gcd⁡(a,n)=1\gcd(a,n)=1. If an−1≢1(modn)a^{n-1}\not\equiv1\pmod n, then nn is composite. If it is 11, nn is only a probable prime; Carmichael numbers fool this test.
    • Miller–Rabin: write n−1=2sdn-1=2^s d, where dd is odd. Choose a base aa. Compute ad mod na^d\bmod n. If it is 11 or −1 mod n-1\bmod n, declare probable prime. Otherwise repeatedly square at most s−1s-1 times; if some value becomes −1 mod n-1\bmod n, declare probable prime.
      If not, nn is composite. A prime always passes; a composite passes with probability at most 1/41/4 per random base.
  6. Chinese Remainder Theorem.
    For pairwise coprime moduli nin_i, set N=∏niN=\prod n_i, Mi=N/niM_i=N/n_i, and yi=Mi−1 mod niy_i=M_i^{-1}\bmod n_i. Then x=∑iaiMiyi(modN).x=\sum_i a_iM_iy_i\pmod N. The solution is unique modulo NN.

  7. RSA key generation and use.
    Choose distinct large primes p,qp,q; compute n=pqn=pq and ϕ(n)=(p−1)(q−1)\phi(n)=(p-1)(q-1). Choose ee with 1<e<ϕ(n)1<e<\phi(n) and gcd⁡(e,ϕ(n))=1\gcd(e,\phi(n))=1. Compute d=e−1 mod ϕ(n)d=e^{-1}\bmod\phi(n). Public key is (e,n)(e,n); private key is (d,n)(d,n). Encryption is C=Me mod nC=M^e\bmod n; decryption is M=Cd mod nM=C^d\bmod n.

Figure. Notice that p and q determine n and phi; e must be coprime to phi, and d is its inverse mod phi. Public (e,n) encrypts C = M^e mod n, while private (d,n) decrypts M = C^d mod n.

Formulas

  • Euclid step: gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a,b)=\gcd(b,a\bmod b), with gcd⁡(a,0)=a\gcd(a,0)=a.
  • Bézout identity: ax+by=gcd⁡(a,b)a x+b y=\gcd(a,b).
  • Congruence: a≡b(modn)  ⟺  n∣(a−b)a\equiv b\pmod n \iff n\mid(a-b).
  • Inverse: aa−1≡1(modn)a a^{-1}\equiv1\pmod n, exists iff gcd⁡(a,n)=1\gcd(a,n)=1.
  • Euler phi: ϕ(n)=n∏p∣n(1−1p)\phi(n)=n\prod_{p\mid n}\left(1-\frac1p\right). Also ϕ(p)=p−1\phi(p)=p-1, ϕ(pe)=pe−pe−1\phi(p^e)=p^e-p^{e-1}, and ϕ(pq)=(p−1)(q−1)\phi(pq)=(p-1)(q-1) for distinct primes.
  • Euler’s theorem: gcd⁡(a,n)=1⇒aϕ(n)≡1(modn)\gcd(a,n)=1\Rightarrow a^{\phi(n)}\equiv1\pmod n.
  • Fermat’s little theorem: pp prime, gcd⁡(a,p)=1⇒ap−1≡1(modp)\gcd(a,p)=1\Rightarrow a^{p-1}\equiv1\pmod p; and ap≡a(modp)a^p\equiv a\pmod p for all integers aa.
  • CRT: for pairwise coprime nin_i, x≡ai(modni)x\equiv a_i\pmod{n_i} has unique solution modulo N=∏niN=\prod n_i.
  • RSA: n=pqn=pq, ϕ(n)=(p−1)(q−1)\phi(n)=(p-1)(q-1), gcd⁡(e,ϕ(n))=1\gcd(e,\phi(n))=1, d≡e−1(modϕ(n))d\equiv e^{-1}\pmod{\phi(n)}, C≡Me(modn)C\equiv M^e\pmod n, M≡Cd(modn)M\equiv C^d\pmod n.
  • Double hashing: h(k,i)=(h1(k)+i h2(k)) mod mh(k,i)=(h_1(k)+i\,h_2(k))\bmod m, i=0,1,…,m−1i=0,1,\dots,m-1.
  • CLRS modular exponentiation count: β+popcount⁡(b)\beta+\operatorname{popcount}(b) modular multiplications, where β=⌊log⁡2b⌋+1\beta=\lfloor\log_2 b\rfloor+1.

Worked examples

Example 1: Euclid and extended Euclid.
Find gcd⁡(48,18)\gcd(48,18) and write it as 48x+18y48x+18y. 48=2⋅18+12,18=1⋅12+6,12=2⋅6+0.48=2\cdot18+12,\quad 18=1\cdot12+6,\quad 12=2\cdot6+0. So gcd⁡(48,18)=6\gcd(48,18)=6. Back-substitute: 6=18−12=18−(48−2⋅18)=3⋅18−48.6=18-12=18-(48-2\cdot18)=3\cdot18-48. Thus 48(−1)+18(3)=648(-1)+18(3)=6. So x=−1, y=3x=-1,\ y=3.

Example 2: Euler totient.
Compute ϕ(36)\phi(36). Since 36=22⋅3236=2^2\cdot3^2, ϕ(36)=36(1−12)(1−13)=36⋅12⋅23=12.\phi(36)=36\left(1-\frac12\right)\left(1-\frac13\right) =36\cdot\frac12\cdot\frac23=12. The 12 coprime residues are 1,5,7,11,13,17,19,23,25,29,31,351,5,7,11,13,17,19,23,25,29,31,35.

Example 3: modular exponentiation and CRT.
Compute 717 mod 5617^{17}\bmod561. Since 561=3⋅11⋅17561=3\cdot11\cdot17:

  • Modulo 33: 7≡17\equiv1, so 717≡17^{17}\equiv1.
  • Modulo 1111: 710≡17^{10}\equiv1, and 17≡7(mod10)17\equiv7\pmod{10}. Now 72≡57^2\equiv5, 74≡37^4\equiv3, so 77≡3⋅5⋅7≡105≡6(mod11)7^7\equiv3\cdot5\cdot7\equiv105\equiv6\pmod{11}.
  • Modulo 1717: 716≡17^{16}\equiv1, so 717≡7(mod17)7^{17}\equiv7\pmod{17}.

Solve x≡1(mod3)x\equiv1\pmod3, x≡6(mod11)x\equiv6\pmod{11}, x≡7(mod17)x\equiv7\pmod{17}. From the first two, x=28+33tx=28+33t. Modulo 1717: 28+33t≡7(mod17)⇒11+16t≡7⇒−t≡13⇒t≡4.28+33t\equiv7\pmod{17} \Rightarrow 11+16t\equiv7 \Rightarrow -t\equiv13 \Rightarrow t\equiv4. Hence x=28+33⋅4=160x=28+33\cdot4=160. So 717 mod 561=1607^{17}\bmod561=160.

Example 4: RSA numeric two-step.
Let p=5p=5, q=11q=11, e=3e=3, and message M=8M=8. Then n=55,ϕ(n)=4⋅10=40.n=55,\quad \phi(n)=4\cdot10=40. Find dd with 3d≡1(mod40)3d\equiv1\pmod{40}. Since 3⋅27=81≡13\cdot27=81\equiv1, d=27d=27.
Encryption: C=83 mod 55=512 mod 55=512−495=17.C=8^3\bmod55=512\bmod55=512-495=17. So the ciphertext is 1717. Decryption check: 1727 mod 5517^{27}\bmod55. Repeated squaring gives 1716≡1617^{16}\equiv16, 178≡2617^8\equiv26, 172≡1417^2\equiv14, 17≡17(mod55)17\equiv17\pmod{55}. Therefore 1727=171617817217≡16⋅26⋅14⋅17≡8(mod55).17^{27}=17^{16}17^8 17^2 17 \equiv16\cdot26\cdot14\cdot17\equiv8\pmod{55}. The plaintext returns as 88.

Example 5: statement-type.
State whether each is true or false.

  1. For prime pp, ap≡a(modp)a^p\equiv a\pmod p for every integer aa. True; this is Fermat’s little theorem in the “all integers” form.
  2. If an−1≡1(modn)a^{n-1}\equiv1\pmod n for all aa coprime to nn, then nn is prime. False; Carmichael number 561561 is composite and satisfies this for all coprime aa.
  3. An inverse of aa modulo nn exists exactly when gcd⁡(a,n)=1\gcd(a,n)=1. True.

Comparison

ConceptMain conditionConclusionCommon trap
Fermat’s little theorempp prime, gcd⁡(a,p)=1\gcd(a,p)=1ap−1≡1(modp)a^{p-1}\equiv1\pmod pap−1a^{p-1} needs coprimality; ap≡aa^p\equiv a holds for all aa
Euler’s theoremgcd⁡(a,n)=1\gcd(a,n)=1aϕ(n)≡1(modn)a^{\phi(n)}\equiv1\pmod nIt does not say all aa work
Carmichael numberComposite nnFermat test can pass for all coprime basesPassing Fermat does not prove prime
Trial divisionTest divisors up to n\sqrt nDeterministic primalityTesting only up to n/2n/2 is wasteful, not wrong but slower
AlgorithmReturnsComplexityUse
Euclid GCDgcd⁡(a,b)\gcd(a,b)O(log⁡min⁡(a,b))O(\log\min(a,b)) divisionsBasic gcd
Extended Euclidgcd⁡(a,b)\gcd(a,b) and x,yx,y with ax+by=dax+by=dSame orderModular inverse, CRT
Binary modular exponentiationab mod na^b\bmod nO(log⁡b)O(\log b) modular multiplicationsRSA, primality tests
CRTUnique solution modulo productDepends on moduli arithmeticSpeeding RSA and solving congruences
TermFormula or factExample
ϕ(p)\phi(p)p−1p-1ϕ(7)=6\phi(7)=6
ϕ(pq)\phi(pq)(p−1)(q−1)(p-1)(q-1)ϕ(5⋅11)=40\phi(5\cdot11)=40
Double hashing probeh1(k)+ih2(k) mod mh_1(k)+i h_2(k)\bmod mStep size is h2(k)h_2(k), not ii alone
RSA public exponentgcd⁡(e,ϕ(n))=1\gcd(e,\phi(n))=1e=3e=3, ϕ=40\phi=40 works

Revision summary

  • Modular inverse exists iff gcd⁡(a,n)=1\gcd(a,n)=1; otherwise do not try to compute it.
  • Fermat uses exponent p−1p-1 only when gcd⁡(a,p)=1\gcd(a,p)=1; ap≡aa^p\equiv a holds for all aa.
  • Euler’s theorem generalizes Fermat from prime modulus to any modulus, but only for coprime aa.
  • The smallest Carmichael number is 561561, 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 β\beta squarings plus one multiplication for each 11-bit of the exponent.
  • CRT needs pairwise coprime moduli and gives uniqueness modulo their product.
  • In RSA, ee is coprime to ϕ(n)\phi(n), not to nn.
  • Double hashing uses i⋅h2(k)i\cdot h_2(k), not h2(k)+ih_2(k)+i.
  • Miller–Rabin is one-sided: primes never fail, composites may pass a round.
  • Trial division checks up to n\sqrt n, not up to nn.
  • ϕ(36)=12\phi(36)=12 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 ii multiplies the second hash value inside the modulus.
  • Statement I/II questions test Fermat’s little theorem, Euler’s theorem, and the difference between ap−1≡1a^{p-1}\equiv1 and ap≡aa^p\equiv a.
  • 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 ϕ(pq)\phi(pq), 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 dd or ciphertext; first compute nn and ϕ(n)\phi(n), then find the inverse of ee, then exponentiate.
  • Modular exponentiation can be combined with CRT, as in computing a large power modulo a composite nn.
  • Traps include using ee coprime to nn instead of ϕ(n)\phi(n), forgetting the condition gcd⁡(a,n)=1\gcd(a,n)=1, 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:

  1. Run Euclid’s algorithm on any pair and state the gcd and number of division steps.
  2. Use extended Euclid to write gcd⁡(a,b)=ax+by\gcd(a,b)=ax+by and find a modular inverse.
  3. Decide whether a given aa has an inverse modulo nn, and compute it when it exists.
  4. Compute ϕ(n)\phi(n) from prime factorization for numbers such as 3636, pqp q, and pep^e.
  5. Apply Fermat’s little theorem and Euler’s theorem correctly, including coprimality conditions.
  6. Identify why 561561 is a Carmichael number and why it matters for primality testing.
  7. Trace Miller–Rabin for a small nn and explain its one-sided error.
  8. Solve a two-congruence CRT system and then a three-congruence system.
  9. Perform RSA key generation, encryption and decryption for small primes.
  10. Count modular multiplications in CLRS binary exponentiation and trace the algorithm on exponents such as 1313.

Attempt the PARIKSHA sub-topic test first, then the subject test.

Pages 1–2 of 2