Fermats Little Theorem Calculator
Free Fermats little theorem Calculator for arithmetic. Enter values to get step-by-step solutions with formulas and graphs.
Calculator
Adjust values & calculatePower Table: 3^k mod 7
Formula
Where a is any integer, p is a prime number, and gcd(a,p) = 1 means a is not divisible by p. The general form states a^p mod p = a mod p for any integer a. This theorem is the foundation of modular arithmetic and RSA cryptography.
Last reviewed: December 2025
Worked Examples
Example 1: Verifying Fermat's Little Theorem
Example 2: Finding Modular Inverse Using Fermat
Background & Theory
The Fermats Little Theorem Calculator applies the following established principles and formulas. Mathematics rests on a hierarchy of number systems, each extending the previous. The natural numbers (1, 2, 3, ...) support counting and ordering. The integers add negative values and zero, enabling subtraction without restriction. The rational numbers, expressible as p/q where p and q are integers and q is nonzero, close the system under division. The real numbers fill the gaps left by irrationals such as the square root of 2 or pi, forming a complete ordered field. The complex numbers, written as a + bi where i is the square root of negative one, complete the algebraic closure of the reals and allow every polynomial to have a root. Prime factorization states that every integer greater than one is uniquely expressible as a product of primes, a result known as the Fundamental Theorem of Arithmetic. Computing the greatest common divisor (GCD) of two integers relies most efficiently on the Euclidean algorithm: repeatedly replace the larger number with the remainder when it is divided by the smaller, until the remainder is zero. The last nonzero remainder is the GCD. The least common multiple (LCM) follows from the identity LCM(a, b) = |a * b| / GCD(a, b). Modular arithmetic defines equivalence classes of integers that share the same remainder under division by a modulus n. Fermat's Little Theorem and Euler's Theorem arise from this structure and underpin modern cryptography. Logarithms are the inverses of exponential functions. If b raised to the power x equals y, then the logarithm base b of y equals x. The natural logarithm uses base e, approximately 2.71828. Combinatorics counts arrangements and selections. The number of ordered arrangements (permutations) of r objects from n distinct objects is nPr = n! / (n - r)!. The number of unordered selections (combinations) is nCr = n! / (r! * (n - r)!). Pascal's triangle arranges these binomial coefficients so that each entry equals the sum of the two entries directly above it. The Fibonacci sequence, defined by F(1) = 1, F(2) = 1, and F(n) = F(n-1) + F(n-2), appears throughout nature and connects deeply to the golden ratio via Binet's formula.
History
The history behind the Fermats Little Theorem Calculator traces back through the following developments. Mathematics as a systematic discipline traces to ancient Mesopotamia. Babylonian clay tablets dating to around 1800 BCE demonstrate knowledge of quadratic equations, Pythagorean triples, and base-60 arithmetic, suggesting a practical mathematical tradition far preceding Greek formalism. Euclid of Alexandria compiled the Elements around 300 BCE, establishing the axiomatic method that would define rigorous mathematics for over two thousand years. His work organized plane geometry, number theory, and proportion into logically chained propositions derived from a small set of postulates. The algorithm bearing his name for computing GCDs appears in Book VII and remains in use today. In the 9th century, the Persian scholar Muhammad ibn Musa Al-Khwarizmi wrote Al-Kitab al-mukhtasar fi hisab al-jabr wal-muqabala, the treatise whose title gave algebra its name. He systematized the solution of linear and quadratic equations and described procedures that operated on unknowns as objects, a conceptual leap away from purely numerical calculation. Rene Descartes introduced coordinate geometry in 1637 by uniting algebra and Euclidean geometry, allowing curves to be studied through equations. This synthesis set the stage for calculus. Isaac Newton and Gottfried Wilhelm Leibniz independently developed calculus during the 1660s and 1670s, triggering a priority dispute that lasted decades and divided British and Continental mathematicians. Carl Friedrich Gauss proved the Fundamental Theorem of Algebra in 1799, showing that every nonconstant polynomial has at least one complex root. His Disquisitiones Arithmeticae of 1801 established modern number theory. David Hilbert's formalist program at the turn of the 20th century sought to place all of mathematics on an explicit axiomatic foundation, a project that Kurt Godel's incompleteness theorems of 1931 showed to be fundamentally limited. Alan Turing's work in the 1930s on computability introduced the theoretical model of the stored-program computer and linked mathematical logic directly to the limits of algorithmic calculation. His proof that no algorithm can decide in general whether an arbitrary program will halt or run forever placed fundamental boundaries on what mathematics can mechanically determine, and it opened the discipline now known as theoretical computer science.
Frequently Asked Questions
Formula
a^(p-1) mod p = 1 (when p is prime and gcd(a,p) = 1)
Where a is any integer, p is a prime number, and gcd(a,p) = 1 means a is not divisible by p. The general form states a^p mod p = a mod p for any integer a. This theorem is the foundation of modular arithmetic and RSA cryptography.
Worked Examples
Example 1: Verifying Fermat's Little Theorem
Problem: Verify that 3^6 mod 7 = 1 using Fermat's Little Theorem (p=7, a=3).
Solution: Fermat's Little Theorem: a^(p-1) mod p = 1 when p is prime and gcd(a,p) = 1\nHere: a = 3, p = 7, p-1 = 6\ngcd(3, 7) = 1 (coprime, so theorem applies)\n\n3^6 = 3^2 * 3^2 * 3^2 = 9 * 9 * 9\n9 mod 7 = 2, so 3^6 mod 7 = 2 * 2 * 2 mod 7 = 8 mod 7 = 1\n\nThe theorem holds: 3^6 mod 7 = 1
Result: 3^6 mod 7 = 1 (verified)
Example 2: Finding Modular Inverse Using Fermat
Problem: Find the modular inverse of 5 modulo 13 using Fermat's Little Theorem.
Solution: By Fermat: a^(p-2) mod p is the inverse of a mod p\na = 5, p = 13, so inverse = 5^(13-2) mod 13 = 5^11 mod 13\n\nUsing repeated squaring:\n5^2 = 25 mod 13 = 12\n5^4 = 12^2 = 144 mod 13 = 1\n5^8 = 1^2 = 1\n5^11 = 5^8 * 5^2 * 5^1 = 1 * 12 * 5 = 60 mod 13 = 8\n\nVerification: 5 * 8 = 40 mod 13 = 1
Result: Inverse of 5 mod 13 = 8 (since 5 * 8 = 40 mod 13 = 1)
Frequently Asked Questions
What is Fermat's Little Theorem and what does it state?
Fermat's Little Theorem is a fundamental result in number theory discovered by Pierre de Fermat in 1640. It states that if p is a prime number and a is any integer not divisible by p, then a raised to the power (p-1) is congruent to 1 modulo p. In notation: a^(p-1) mod p = 1. There is also a more general form: for any integer a (even those divisible by p), a^p is congruent to a modulo p. For example, with a = 3 and p = 7: 3^6 = 729, and 729 mod 7 = 1. This theorem has profound implications for primality testing, cryptography, and computing modular inverses. It is one of the cornerstones of modern number theory.
How is Fermat's Little Theorem used in cryptography?
Fermat's Little Theorem is a key building block for the RSA encryption algorithm, one of the most widely used public-key cryptosystems. RSA relies on Euler's theorem, which generalizes Fermat's Little Theorem to composite moduli. In RSA, two large primes p and q are multiplied to get n = p * q, and encryption/decryption uses modular exponentiation where the theorem guarantees that the original message can be recovered. Specifically, the decryption exponent d is chosen so that e * d is congruent to 1 modulo the totient of n, and Fermat's theorem ensures that raising a ciphertext to the power d recovers the plaintext. Without this theorem, the mathematical foundation of RSA would not work.
What is the difference between Fermat's Little Theorem and Fermat's Last Theorem?
Despite the similar names, these are completely different theorems. Fermat's Little Theorem (1640) is about modular arithmetic: a^(p-1) mod p = 1 for prime p when gcd(a,p) = 1. It was proven by Euler in 1736 and is widely used in practical applications. Fermat's Last Theorem (1637) states that there are no positive integer solutions to a^n + b^n = c^n for any integer n greater than 2. It remained unproven for 358 years and was finally proved by Andrew Wiles in 1995 using extremely advanced mathematics involving elliptic curves and modular forms. The Little Theorem is elementary and has a simple proof, while the Last Theorem required decades of work by some of the greatest mathematicians.
How can Fermat's Little Theorem be used for primality testing?
Fermat's Little Theorem provides a necessary condition for primality: if p is prime, then a^(p-1) mod p = 1 for any a coprime to p. The contrapositive gives a primality test: if a^(p-1) mod p is not equal to 1 for some a, then p is definitely not prime. This is called the Fermat primality test. To test whether n is prime, choose random bases a and compute a^(n-1) mod n. If the result is not 1, n is composite. If it equals 1 for many bases, n is likely prime. However, certain composite numbers called Carmichael numbers (like 561) pass the Fermat test for all coprime bases. More robust tests like Miller-Rabin extend Fermat's approach to handle these cases.
How do you compute modular inverse using Fermat's Little Theorem?
If p is prime and a is not divisible by p, then the modular inverse of a modulo p is a^(p-2) mod p. This follows directly from the theorem: since a^(p-1) mod p = 1, we can write a * a^(p-2) mod p = 1, which means a^(p-2) is the multiplicative inverse of a modulo p. For example, to find the inverse of 3 mod 7: compute 3^(7-2) = 3^5 = 243, and 243 mod 7 = 5. Verification: 3 * 5 = 15, and 15 mod 7 = 1. This method is computationally efficient using fast modular exponentiation (repeated squaring), which runs in O(log n) multiplications. It is simpler to implement than the extended Euclidean algorithm for prime moduli.
What is Euler's theorem and how does it generalize Fermat's Little Theorem?
Euler's theorem states that if a and n are coprime (gcd(a,n) = 1), then a raised to the power of Euler's totient function of n is congruent to 1 modulo n. In notation: a^(phi(n)) mod n = 1, where phi(n) counts the integers from 1 to n that are coprime to n. When n = p (a prime), phi(p) = p - 1, and Euler's theorem reduces exactly to Fermat's Little Theorem. For composite n, the totient is smaller: phi(12) = 4 (the numbers 1, 5, 7, 11 are coprime to 12). So 5^4 mod 12 = 625 mod 12 = 1. Euler's theorem is the foundation of RSA encryption, where the modulus n = p*q and phi(n) = (p-1)(q-1).
References
Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy