Number Theory (Interesting Facts and Algorithms)

Difficulty Level :
Hard
Questions based on various concepts of number theory and different types of number are quite frequently asked in programming contests. In this article, we discuss some famous facts and algorithms:
Interesting Facts :
All 4 digit palindromic numbers are divisible by 11.If we repeat a three-digit number twice, to form a six-digit number. The result will be divisible by 7, 11 and 13, and dividing by all three will give your original three-digit number.A number of form 2N has exactly N+1 divisors. For example 4 has 3 divisors, 1, 2 and 4.To calculate sum of factors of a number, we can find the number of prime factors and their exponents. Let p1, p2, ... pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* ... (pkak).
All 4 digit palindromic numbers are divisible by 11.
If we repeat a three-digit number twice, to form a six-digit number. The result will be divisible by 7, 11 and 13, and dividing by all three will give your original three-digit number.
A number of form 2N has exactly N+1 divisors. For example 4 has 3 divisors, 1, 2 and 4.
To calculate sum of factors of a number, we can find the number of prime factors and their exponents. Let p1, p2, ... pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* ... (pkak).
Sum of divisors = (1 + p1 + p12 ... p1a1) * 
                  (1 + p2 + p22 ... p2a2) *
                  .............................................
                  (1 + pk + pk2 ... pkak) 

We can notice that individual terms of above 
formula are Geometric Progressions (GP). We
can rewrite the formula as.

Sum of divisors = (p1a1+1 - 1)/(p1 -1) * 
                  (p2a2+1 - 1)/(p2 -1) *
                  ..................................
                  (pkak+1 - 1)/(pk -1)
For a product of N numbers, if we have to subtract a constant K such that the product gets its maximum value, then subtract it from a largest value such that largest value-k is greater than 0. If we have to subtract a constant K such that the product gets its minimum value, then subtract it from the smallest value where smallest value-k should be greater than 0Goldbach’s conjecture: Every even integer greater than 2 can be expressed as the sum of 2 primes.Perfect numbers or Amicable numbers: Perfect numbers are those numbers which are equal to the sum of their proper divisors. Example: 6 = 1 + 2 + 3Lychrel numbers: Are those numbers that cannot form a palindrome when repeatedly reversed and added to itself. For example 47 is not a Lychrel Number as 47 + 74 = 121Lemoine’s Conjecture : Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine’s conjecture.Fermat’s Last Theorem : According to the theorem, no three positive integers a, b, c satisfy the equation, for any integer value of n greater than 2. For n = 1 and n = 2, the equation have infinitely many solutions.
For a product of N numbers, if we have to subtract a constant K such that the product gets its maximum value, then subtract it from a largest value such that largest value-k is greater than 0. If we have to subtract a constant K such that the product gets its minimum value, then subtract it from the smallest value where smallest value-k should be greater than 0
Goldbach’s conjecture: Every even integer greater than 2 can be expressed as the sum of 2 primes.
Perfect numbers or Amicable numbers: Perfect numbers are those numbers which are equal to the sum of their proper divisors. Example: 6 = 1 + 2 + 3
Lychrel numbers: Are those numbers that cannot form a palindrome when repeatedly reversed and added to itself. For example 47 is not a Lychrel Number as 47 + 74 = 121
Lemoine’s Conjecture : Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine’s conjecture.
Fermat’s Last Theorem : According to the theorem, no three positive integers a, b, c satisfy the equation, for any integer value of n greater than 2. For n = 1 and n = 2, the equation have infinitely many solutions.
Number Theory Algorithms
GCD and LCM 
GCD and LCMLCM of arrayGCD of arrayBasic and Extended Euclidean algorithms
GCD and LCM
LCM of array
GCD of array
Basic and Extended Euclidean algorithms
Recent Articles on GCD and LCM! Prime Factorization and Divisors :
Prime factorsPollard’s Rho Algorithm for Prime FactorizationFind all divisors of a natural numberSum of all proper divisors of a natural numberPrime Factorization using Sieve O(log n) for multiple queriesFind politeness of a numberPrint prime numbers in a given range using C++ STLk-th prime factor of a given numberSmith Numbers
Prime factors
Pollard’s Rho Algorithm for Prime Factorization
Find all divisors of a natural number
Sum of all proper divisors of a natural number
Prime Factorization using Sieve O(log n) for multiple queries
Find politeness of a number
Print prime numbers in a given range using C++ STL
k-th prime factor of a given number
Smith Numbers
Recent Articles on Prime Factors! Fibonacci Numbers:
Fibonacci NumbersInteresting facts about Fibonacci numbersHow to check if a given number is Fibonacci number?Zeckendorf’s Theorem (Non-Neighbouring Fibonacci Representation)
Fibonacci Numbers
Interesting facts about Fibonacci numbers
How to check if a given number is Fibonacci number?
Zeckendorf’s Theorem (Non-Neighbouring Fibonacci Representation)
Recent Articles on Fibonacci Numbers! Catalan Numbers :
Catalan numbersApplications of Catalan Numbers
Catalan numbers
Applications of Catalan Numbers
Recent Articles on Catalan Numbers! Modular Arithmetic :
Modular Exponentiation (Power in Modular Arithmetic)Modular multiplicative inverseModular DivisionMultiplicative orderFind Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)Euler’s criterion (Check if square root under modulo p exists)Multiply large integers under large moduloFind sum of modulo K of first N natural numberHow to compute mod of a big number?BigInteger Class in JavaModulo 10^9+7 (1000000007)How to avoid overflow in modular multiplication?RSA Algorithm in CryptographyFind (a^b)%m where ‘a’ is very largeFind power of power under mod of a prime
Modular Exponentiation (Power in Modular Arithmetic)
Modular multiplicative inverse
Modular Division
Multiplicative order
Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
Euler’s criterion (Check if square root under modulo p exists)
Multiply large integers under large modulo
Find sum of modulo K of first N natural number
How to compute mod of a big number?
BigInteger Class in Java
Modulo 10^9+7 (1000000007)
How to avoid overflow in modular multiplication?
RSA Algorithm in Cryptography
Find (a^b)%m where ‘a’ is very large
Find power of power under mod of a prime
Recent Articles on Modular Arithmetic! Euler Totient Function:
Euler’s Totient FunctionOptimized Euler Totient Function for Multiple EvaluationsEuler’s Totient function for all numbers smaller than or equal to nPrimitive root of a prime number n modulo n
Euler’s Totient Function
Optimized Euler Totient Function for Multiple Evaluations
Euler’s Totient function for all numbers smaller than or equal to n
Primitive root of a prime number n modulo n
nCr Computations :
Binomial CoefficientCompute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)Compute nCr % p | Set 2 (Lucas Theorem)Compute nCr % p | Set 3 (Using Fermat Little Theorem)
Binomial Coefficient
Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
Compute nCr % p | Set 2 (Lucas Theorem)
Compute nCr % p | Set 3 (Using Fermat Little Theorem)
Chinese Remainder Theorem : 
Set 1 (Introduction)Set 2 (Inverse Modulo based Implementation)Cyclic Redundancy Check and Modulo-2 DivisionUsing Chinese Remainder Theorem to Combine Modular equations
Set 1 (Introduction)
Set 2 (Inverse Modulo based Implementation)
Cyclic Redundancy Check and Modulo-2 Division
Using Chinese Remainder Theorem to Combine Modular equations
Factorial :
FactorialLegendre’s formula (Given p and n, find the largest x such that p^x divides n!)Sum of divisors of factorial of a numberCount Divisors of FactorialCompute n! under modulo pWilson’s TheoremPrimality Test | Set 1 (Introduction and School Method)Primality Test | Set 2 (Fermat Method)Primality Test | Set 3 (Miller–Rabin)Primality Test | Set 4 (Solovay-Strassen)GFact 22 | (2^x + 1 and Prime)Euclid’s LemmaSieve of EratosthenesSegmented SieveSieve of AtkinSieve of Sundaram to print all primes smaller than nSieve of Eratosthenes in 0(n) time complexityCheck if a large number is divisible by 3 or notCheck if a large number is divisible by 11 or notTo check divisibility of any large number by 999Carmichael NumbersGenerators of finite cyclic group under additionMeasure one litre using two vessels and infinite water supplyProgram to find last digit of n’th Fibonacci NumberGCD of two numbers when one of them can be very largeFind Last Digit Of a^b for Large NumbersRemainder with 7 for large numbersCount all sub-arrays having sum divisible by kPartition a number into two divisible partsNumber of substrings divisible by 6 in a string of integers‘Practice Problems’ on Modular Arithmetic‘Practice Problems’ on Number TheoryAsk a Question on Number theoryPadovan, OESIS
Factorial
Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)
Sum of divisors of factorial of a number
Count Divisors of Factorial
Compute n! under modulo pWilson’s TheoremPrimality Test | Set 1 (Introduction and School Method)Primality Test | Set 2 (Fermat Method)Primality Test | Set 3 (Miller–Rabin)Primality Test | Set 4 (Solovay-Strassen)GFact 22 | (2^x + 1 and Prime)Euclid’s LemmaSieve of EratosthenesSegmented SieveSieve of AtkinSieve of Sundaram to print all primes smaller than nSieve of Eratosthenes in 0(n) time complexityCheck if a large number is divisible by 3 or notCheck if a large number is divisible by 11 or notTo check divisibility of any large number by 999Carmichael NumbersGenerators of finite cyclic group under additionMeasure one litre using two vessels and infinite water supplyProgram to find last digit of n’th Fibonacci NumberGCD of two numbers when one of them can be very largeFind Last Digit Of a^b for Large NumbersRemainder with 7 for large numbersCount all sub-arrays having sum divisible by kPartition a number into two divisible partsNumber of substrings divisible by 6 in a string of integers‘Practice Problems’ on Modular Arithmetic‘Practice Problems’ on Number TheoryAsk a Question on Number theoryPadovan, OESIS
Wilson’s TheoremPrimality Test | Set 1 (Introduction and School Method)Primality Test | Set 2 (Fermat Method)Primality Test | Set 3 (Miller–Rabin)Primality Test | Set 4 (Solovay-Strassen)GFact 22 | (2^x + 1 and Prime)Euclid’s LemmaSieve of EratosthenesSegmented SieveSieve of AtkinSieve of Sundaram to print all primes smaller than nSieve of Eratosthenes in 0(n) time complexityCheck if a large number is divisible by 3 or notCheck if a large number is divisible by 11 or notTo check divisibility of any large number by 999Carmichael NumbersGenerators of finite cyclic group under additionMeasure one litre using two vessels and infinite water supplyProgram to find last digit of n’th Fibonacci NumberGCD of two numbers when one of them can be very largeFind Last Digit Of a^b for Large NumbersRemainder with 7 for large numbersCount all sub-arrays having sum divisible by kPartition a number into two divisible partsNumber of substrings divisible by 6 in a string of integers‘Practice Problems’ on Modular Arithmetic‘Practice Problems’ on Number TheoryAsk a Question on Number theoryPadovan, OESIS
Wilson’s Theorem
Primality Test | Set 1 (Introduction and School Method)
Primality Test | Set 2 (Fermat Method)
Primality Test | Set 3 (Miller–Rabin)
Primality Test | Set 4 (Solovay-Strassen)
GFact 22 | (2^x + 1 and Prime)
Euclid’s Lemma
Sieve of Eratosthenes
Segmented Sieve
Sieve of Atkin
Sieve of Sundaram to print all primes smaller than n
Sieve of Eratosthenes in 0(n) time complexity
Check if a large number is divisible by 3 or not
Check if a large number is divisible by 11 or not
To check divisibility of any large number by 999
Carmichael Numbers
Generators of finite cyclic group under addition
Measure one litre using two vessels and infinite water supply
Program to find last digit of n’th Fibonacci Number
GCD of two numbers when one of them can be very large
Find Last Digit Of a^b for Large Numbers
Remainder with 7 for large numbers
Count all sub-arrays having sum divisible by k
Partition a number into two divisible parts
Number of substrings divisible by 6 in a string of integers
‘Practice Problems’ on Modular Arithmetic
‘Practice Problems’ on Number Theory
Ask a Question on Number theory
Padovan, OESIS
chhabradhanvi
binomial coefficient
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Count ways to reach the n'th stair
Fizz Buzz Implementation
Median of two sorted arrays of same size
Product of Array except itself
Find Union and Intersection of two unsorted arrays