Number Theory (Interesting Facts and Algorithms)

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 :

  1. All 4 digit palindromic numbers are divisible by 11.
  2. If we repeat a three-digit number twice, to form a six-digit number. The result will will be divisible by 7, 11 and 13, and dividing by all three will give your original three-digit number.
  3. A number of form 2N has exactly N+1 divisors. For example 4 has 3 divisors, 1, 2 and 4.
  4. To calculate sum of factors of a number, we can find the number of prime factors and their exponents. N = ae1 * be2 * ce3
    Then sum = (1 + a + a^2….)(1 + b + b^2 .. )
  5. 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
  6. Goldbech`s conjecture: Every even integer greater than 2 can be expressed as the sum of 2 primes.
  7. 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
  8. 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

Number Theory Algorithms

GCD and LCM

  1. GCD and LCM
  2. LCM of array
  3. GCD of array
  4. Basic and Extended Euclidean algorithms

Recent Articles on GCD and LCM!

Prime Factorization and Divisors :

  1. Prime factors
  2. Pollard’s Rho Algorithm for Prime Factorization
  3. Find all divisors of a natural number
  4. Sum of all proper divisors of a natural number
  5. Prime Factorization using Sieve O(log n) for multiple queries
  6. Find politeness of a number
  7. Print prime numbers in a given range using C++ STL
  8. k-th prime factor of a given number
  9. Smith Numbers

Recent Articles on Prime Factors!

Fibonacci Numbers:

  1. Fibonacci Numbers
  2. Interesting facts about Fibonacci numbers
  3. How to check if a given number is Fibonacci number?
  4. Zeckendorf’s Theorem (Non-Neighbouring Fibonacci Representation)

Recent Articles on Fibonacci Numbers!

Catalan Numbers :

  1. Catalan numbers
  2. Applications of Catalan Numbers

Recent Articles on Catalan Numbers!

Modular Arithmetic :

  1. Modular Exponentiation (Power in Modular Arithmetic)
  2. Modular multiplicative inverse
  3. Modular Division
  4. Multiplicative order
  5. Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
  6. Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
  7. Euler’s criterion (Check if square root under modulo p exists)
  8. Multiply large integers under large modulo
  9. Find sum of modulo K of first N natural number
  10. How to compute mod of a big number?
  11. BigInteger Class in Java
  12. Modulo 10^9+7 (1000000007)
  13. How to avoid overflow in modular multiplication?
  14. RSA Algorithm in Cryptography
  15. Find (a^b)%m where ‘a’ is very large
  16. Find power of power under mod of a prime

Recent Articles on Modular Arithmetic!

Euler Totient Function:

  1. Euler’s Totient Function
  2. Optimized Euler Totient Function for Multiple Evaluations
  3. Euler’s Totient function for all numbers smaller than or equal to n
  4. Primitive root of a prime number n modulo n

nCr Computations :

  1. Binomial Coefficient
  2. Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
  3. Compute nCr % p | Set 2 (Lucas Theorem)
  4. Compute nCr % p | Set 3 (Using Fermat Little Theorem)

Chinese Remainder Theorem :

  1. Set 1 (Introduction)
  2. Set 2 (Inverse Modulo based Implementation)
  3. Cyclic Redundancy Check and Modulo-2 Division
  4. Using Chinese Remainder Theorem to Combine Modular equations

Factorial :

  1. Factorial
  2. Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)
  3. Sum of divisors of factorial of a number
  4. Count Divisors of Factorial
  5. Compute n! under modulo p
    1. Recent Articles on Factorial!

      Prime numbers and Primality Tests :

      1. Wilson’s Theorem
      2. Primality Test | Set 1 (Introduction and School Method)
      3. Primality Test | Set 2 (Fermat Method)
      4. Primality Test | Set 3 (Miller–Rabin)
      5. Primality Test | Set 4 (Solovay-Strassen)
      6. GFact 22 | (2^x + 1 and Prime)
      7. Euclid’s Lemma

      Recent Articles on Prime Number!

      Sieve Algorithms :

      1. Sieve of Eratosthenes
      2. Segmented Sieve
      3. Sieve of Atkin
      4. Sieve of Sundaram to print all primes smaller than n
      5. Sieve of Eratosthenes in 0(n) time complexity

      Recent Articles on Sieve!

      Divisibility and Large Numbers :

      1. Check if a large number is divisible by 3 or not
      2. Check if a large number is divisible by 11 or not
      3. To check divisibility of any large number by 999

      Recent Articles on Divisibility!

      Misc :

      1. Carmichael Numbers
      2. Generators of finite cyclic group under addition
      3. Measure one litre using two vessels and infinite water supply
      4. Program to find last digit of n’th Fibonnaci Number
      5. GCD of two numbers when one of them can be very large
      6. Find Last Digit Of a^b for Large Numbers
      7. Remainder with 7 for large numbers
      8. Count all sub-arrays having sum divisible by k
      9. Partition a number into two divisible parts
      10. Number of substrings divisible by 6 in a string of integers

      Recent Articles on Mathematical Algorithms!

      Quick Links :

      1. ‘Practice Problems’ on Modular Arithmetic
      2. ‘Practice Problems’ on Number Theory
      3. Ask a Question on Number theory
      4. Padovan, OESIS

      Above facts are contributed by Yash Kodesia. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

      Please write comments if you find anything incorrect, or you want to share more facts about Number Theory.

      GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.