Topics:
1. Basics:
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.
LCM (Least Common Multiple) of two numbers is the smallest number that can be divided by both numbers.
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.
Prime factorization is a method to find the prime factors of a given number, say a composite number
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.
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!)}
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
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 :
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.
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.
Given three numbers x, y and p, compute (xy) % p.
Given two numbers ‘num’ and ‘divisor’, find the remainder when ‘num’ is divided by ‘divisor’. The use of modulo or % operator is not allowed.
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.
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 )
Given three numbers n, r and p, compute the value of nCr mod p using Dynamic programming.
Given three numbers n, r and p, compute the value of nCr mod p using Lucas Theorem.
Given three numbers n, r and p, compute the value of nCr mod p using Fermat Little 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]
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,
Given a number ‘n’ and a prime ‘p’, find the square root of n under modulo p if it exists.
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.
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
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.
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.
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.
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.
Given a large number n and a prime p, how to efficiently compute n! % p?
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 :
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, …}
Primality test using Ferment Method for checking if a number is prime or not.
Primality test using Miller–Rabin for checking if a number is prime or not.
Primality test using Solovay-Strassen for checking if a number is prime or not.
Given an integer n and a prime number p, find the largest x such that px (p raised to power x) divides n! (factorial)
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
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.
Given a number n, we need to calculate the sum of divisors of factorial of the 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
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
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.
Given a positive integer n, and that it is composite, find a divisor of it.
4. Game Theory:
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.
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).
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.
5. Practice Problems on Number Theory:
6. Miscellaneous Practice problem:
7. Quick Links
- ‘Practice Problems’ on Modular Arithmetic
- ‘Practice Problems’ on Number Theory
- Ask a Question on Number theory
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above