# Number Theory for DSA & Competitive Programming

• Last Updated : 26 Sep, 2023

Topics:

## 1. Basics:

### 1.1  Find the GCD of two number

GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.

### 1.2 Find the LCM of two number

LCM (Least Common Multiple) of two numbers is the smallest number that can be divided by both numbers.

### 1.3 Calculate the Factorial of a number

The factorial of a non-negative integer is the multiplication of all positive integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.

### 1.4 Program to find Prime factors of a number

Prime factorization is a method to find the prime factors of a given number, say a composite number

### 1.5 Find Binomial Coefficient C(n, k)

A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects more formally, the number of k-element subsets (or k-combinations) of n-elements set.

### 1.6 Program to find nth Catalan numbers

Catalan numbers are defined as mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.
The nth term in the sequence denoted Cn, is found in the following formula: \frac{(2n)!}{(n + 1)! n!)}

### 1.7 Euclid’s Lemma

Euclid’s lemma states that if a prime p divides the product of two numbers (x*y), it must divide at least one of those numbers.
For example x = 15, y = 6 and p = 5. p divides the product 15*6, it also divides 15

### 1.8 Basic and Extended Euclidean algorithms

The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.

## 2. Modular Arithmetic :

### 2.1 Euler’s Totient Function

Euler’s Totient function Φ (n) for an input n is the count of numbers in {1, 2, 3, …, n-1} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.

### 2.2 Euler’s Totient function for all numbers smaller than or equal to n

Euler’s Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
For example, Φ(4) = 2, Φ(3) = 2 and Φ(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively prime to 4, 2 numbers smaller or equal to 3 that are relatively prime to 3. And 4 numbers smaller than or equal to 5 that are relatively prime to 5.

### 2.3 Modular Exponentiation (Power in Modular Arithmetic)

Given three numbers x, y and p, compute (xy) % p.

### 2.4 Find the remainder without using the modulo operator

Given two numbers ‘num’ and ‘divisor’, find the remainder when ‘num’ is divided by ‘divisor’. The use of modulo or % operator is not allowed.

### 2.5 Modular multiplicative inverse

The modular inverse of a number refers to the modular multiplicative inverse. For any integer a such that (a, p) = 1 there exists another integer b such that ab≡ 1 (mod p). The integer b is called the multiplicative inverse of a which is denoted as b = a−1.

### 2.6 Multiplicative order

In number theory, given an integer A and a positive integer N with gcd( A, N) = 1, the multiplicative order of a modulo N is the smallest positive integer k with A^k( mod N ) = 1. ( 0 < K < N )

### 2.7 Compute nCr % p using Dynamic Programming Solution

Given three numbers n, r and p, compute the value of nCr mod p using Dynamic programming.

### 2.8 Compute nCr % p using Lucas Theorem

Given three numbers n, r and p, compute the value of nCr mod p using Lucas Theorem.

### 2.9 Compute nCr % p using Fermat Little Theorem

Given three numbers n, r and p, compute the value of nCr mod p using Fermat Little Theorem.

### 2.10 Chinese Remainder Theorem

We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find a minimum positive number x such that:

x % num[0]    =  rem[0],
x % num[1]    =  rem[1],
…………………..
x % num[k-1]  =  rem[k-1]

### 2.12 Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)

Given a number ‘n’ and a prime ‘p’, find square root of n under modulo p if it exists. It may be given that p is in the form for 4*i + 3 (OR p % 4 = 3) where i is an integer. Examples of such primes are 7, 11, 19, 23, 31, … etc,

### 2.13 Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)

Given a number ‘n’ and a prime ‘p’, find the square root of n under modulo p if it exists.

### 2.14 Modular Division

Given three positive numbers a, b and m. Compute a/b under modulo m. The task is basically to find a number c such that (b * c) % m = a % m.

### 2.15 Cyclic Redundancy Check and Modulo-2 Division

CRC or Cyclic Redundancy Check is a method of detecting accidental changes/errors in the communication channel. CRC uses Generator Polynomial which is available on both the sender and receiver side

### 2.16 Find primitive root of a prime number n modulo n

Given a prime number n, the task is to find its primitive root under modulo n. The primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in the range[0, n-2] are different. Return -1 if n is a non-prime number.

### 2.17 Euler’s criterion (Check if square root under modulo p exists)

Given a number ‘n’ and a prime p, find if the square root of n under modulo p exists or not. A number x is the square root of n under modulo p if (x*x)%p = n%p.

### 2.18 Combine Modular equations using the Chinese Remainder Theorem

Given N modular equations: A ≅ x1mod(m1) . . A ≅ xnmod(mn) Find x in the equation A ≅ xmod(m1*m2*m3..*mn) where mi is prime, or a power of a prime, and i takes values from 1 to n. The input is given as two arrays, the first being an array containing values of each xi, and the second array containing the set of values of each prime. mi Output an integer for the value of x in the final equation.

### 2.19 Multiply large integers under large modulo

Given an integer a, b, m. Find (a * b ) mod m, where a, b may be large and their direct multiplication may cause overflow. However, they are smaller than half of the maximum allowed long long int value.

### 2.20 Compute n! under modulo p

Given a large number n and a prime p, how to efficiently compute n! % p?

### 2.21 Wilson’s Theorem

Wilson’s theorem states that a natural number p > 1 is a prime number if and only if

(p – 1) ! ≡  -1   mod p
OR  (p – 1) ! ≡  (p-1) mod p

## 3. Number Theory :

### 3.1 Primality Test to check if a number is prime or not

Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of the first few prime numbers are {2, 3, 5, …}

### 3.2 Primality Test to check if a number is prime or not using Fermat Method

Primality test using Ferment Method for checking if a number is prime or not.

### 3.3 Primality Test to check if a number is prime or not using Miller–Rabin

Primality test using Miller–Rabin for checking if a number is prime or not.

### 3.4 Primality Test to check if a number is prime or not using Solovay-Strassen

Primality test using Solovay-Strassen for checking if a number is prime or not.

### 3.5 Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)

Given an integer n and a prime number p, find the largest x such that px (p raised to power x) divides n! (factorial)

### 3.6 Find if a number is Carmichael Numbers

A number n is said to be a Carmichael number if it satisfies the following modular arithmetic condition:

power(b, n-1) MOD n = 1,
for all b ranging from 1 to n such that b and
n are relatively prime, i.e, gcd(b, n) = 1

### 3.7 Find all generators of cyclic additive group under modulo n

Given a number n, find all generators of cyclic additive group under modulo n. The generator of a set {0, 1, … n-1} is an element x such that x is smaller than n, and using x (and addition operation), we can generate all elements of the set.

### 3.8 Sum of divisors of factorial of a number

Given a number n, we need to calculate the sum of divisors of factorial of the number.

### 3.9 Fermat Number

A number of the form 2x + 1 (where x > 0) is prime if and only if x is a power of 2, i.e., x = 2n

### 3.10 Sieve of Eratosthenes

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

### 3.11 Goldbach’s Conjecture

Goldbach’s conjecture is one of the oldest and best-known unsolved problems in the number theory of mathematics. Every even integer greater than 2 can be expressed as the sum of two primes.

### 3.12 Pollard’s Rho Algorithm for Prime Factorization

Given a positive integer n, and that it is composite, find a divisor of it.

## 4. Game Theory:

### 4.1 Minimax algorithm

Minimax is a kind of backtracking algorithm that is used in decision-making and game theory to find the optimal move for a player, assuming that your opponent also plays optimally. It is widely used in two-player turn-based games such as Tic-Tac-Toe, Backgammon, Mancala, Chess, etc.

### 4.2 Nim Game problem

Given a number of piles in which each pile contains some number of stones/coins. In each turn, a player can choose only one pile and remove any number of stones (at least one) from that pile. The player who cannot move is considered to lose the game (i.e., one who takes the last stone is the winner).

### 4.3 Sprague – Grundy Theorem

Suppose there is a composite game (more than one sub-game) made up of N sub-games and two players, A and B. Then Sprague-Grundy Theorem says that if both A and B play optimally (i.e., they don’t make any mistakes), then the player starting first is guaranteed to win if the XOR of the grundy numbers of position in each sub-games at the beginning of the game is non-zero. Otherwise, if the XOR evaluates to zero, then player A will lose definitely, no matter what.