Fermat's Little Theorem Calculator

Use this Fermat's Little Theorem calculator to explore and verify the congruence relations for any integer base `a` and prime modulus `p`. It helps demonstrate the core principles of modular arithmetic and prime number theory, providing step-by-step results for `a^(p-1) ≡ 1 (mod p)` and `a^p ≡ a (mod p)`.

Calculate Fermat's Little Theorem

Enter any integer value for the base (a).
Enter a prime number for the modulus (p).

What is Fermat's Little Theorem?

Fermat's Little Theorem is a fundamental result in number theory that describes a property of prime numbers. It states that if p is a prime number, then for any integer a, the number ap - a is an integer multiple of p. This can be written using modular arithmetic notation as ap ≡ a (mod p).

A widely used corollary of this theorem is that if p is a prime number and a is an integer not divisible by p (i.e., a mod p ≠ 0), then a(p-1) ≡ 1 (mod p). This specific form is often what people refer to when discussing Fermat's Little Theorem in applications like cryptography.

Who Should Use This Fermat's Little Theorem Calculator?

This Fermat's Little Theorem calculator is an invaluable tool for:

  • Students learning number theory, discrete mathematics, or abstract algebra.
  • Educators demonstrating concepts of modular arithmetic and prime properties.
  • Cryptographers working with foundational principles of public-key cryptography (e.g., RSA algorithm).
  • Programmers implementing algorithms that require modular exponentiation or primality testing.
  • Anyone curious about the elegance and power of fundamental mathematical theorems.

Common Misunderstandings about Fermat's Little Theorem

Despite its simplicity, there are a few common pitfalls:

  • 'p' MUST be prime: The theorem strictly applies only when the modulus `p` is a prime number. If `p` is composite, the congruences generally do not hold.
  • Condition for a(p-1) ≡ 1 (mod p): This specific form requires that `a` is not a multiple of `p`. If `a` is a multiple of `p`, then `a mod p = 0`, and thus `a(p-1) mod p` would also be `0`, not `1`.
  • Converse is NOT true: If `a(p-1) ≡ 1 (mod p)` holds for some `a`, it does not necessarily mean `p` is prime. Numbers that satisfy this for some `a` but are composite are called Fermat pseudoprimes. This is why Fermat's Little Theorem is a primality *test* but not a definitive one. For a more robust test, consider the Miller-Rabin primality test.
  • Unitless Values: The values `a` and `p` in Fermat's Little Theorem are integers and are inherently unitless. There are no associated physical units like meters, kilograms, or seconds.

Fermat's Little Theorem Formula and Explanation

Fermat's Little Theorem revolves around two main forms of congruence:

Form 1: General Statement
If p is a prime number, then for any integer a:

ap ≡ a (mod p)

This means that ap - a is perfectly divisible by p, leaving a remainder of 0.

Form 2: Specific Case (when a is not a multiple of p)
If p is a prime number, and a is an integer such that p does not divide a (i.e., a mod p ≠ 0), then:

a(p-1) ≡ 1 (mod p)

This means that a raised to the power of (p-1), when divided by p, leaves a remainder of 1.

Variables in Fermat's Little Theorem

Variables used in Fermat's Little Theorem
Variable Meaning Unit Typical Range
a Base integer Unitless Any integer (positive, negative, zero)
p Prime modulus Unitless Prime numbers (e.g., 2, 3, 5, 7, ...)
Congruence relation N/A Indicates same remainder when divided by `p`
mod p Modulo operation N/A Remainder when divided by `p`

Our Fermat's Little Theorem calculator applies these formulas to the inputs you provide, ensuring p is indeed a prime number for the theorem to hold true.

Practical Examples of Fermat's Little Theorem

Let's illustrate how Fermat's Little Theorem works with a couple of examples. These examples highlight the conditions and results you'd see using the Fermat's Little Theorem calculator.

Example 1: Basic Verification

Inputs:

  • Base (a) = 3
  • Prime Modulus (p) = 7

Calculations:

  • p is prime? Yes (7 is prime).
  • p-1 = 7 - 1 = 6.
  • a mod p = 3 mod 7 = 3. (Since 3 ≠ 0, the a(p-1) ≡ 1 (mod p) form should hold.)
  • a(p-1) mod p = 36 mod 7.
    • 31 = 3
    • 32 = 9 ≡ 2 (mod 7)
    • 33 = 3 * 2 = 6 ≡ 6 (mod 7)
    • 36 = (33)2 = 62 = 36 ≡ 1 (mod 7)
    So, 36 ≡ 1 (mod 7). This confirms the theorem.
  • ap mod p = 37 mod 7.
    • 37 = 36 * 31 ≡ 1 * 3 (mod 7) ≡ 3 (mod 7)
    So, 37 ≡ 3 (mod 7). This also confirms the theorem, as a mod p = 3.

Results: Both forms of Fermat's Little Theorem are satisfied.

Example 2: When 'a' is a multiple of 'p'

Inputs:

  • Base (a) = 10
  • Prime Modulus (p) = 5

Calculations:

  • p is prime? Yes (5 is prime).
  • p-1 = 5 - 1 = 4.
  • a mod p = 10 mod 5 = 0. (Since a is a multiple of p, the a(p-1) ≡ 1 (mod p) form does NOT apply directly.)
  • a(p-1) mod p = 104 mod 5.
    • Since 10 mod 5 = 0, then 104 mod 5 = 04 mod 5 = 0 mod 5 = 0.
    So, 104 ≡ 0 (mod 5). This is not 1, as expected, because a is a multiple of p.
  • ap mod p = 105 mod 5.
    • Since 10 mod 5 = 0, then 105 mod 5 = 05 mod 5 = 0 mod 5 = 0.
    So, 105 ≡ 0 (mod 5). This confirms the general form of the theorem, ap ≡ a (mod p), because a mod p = 0.

Results: The general form ap ≡ a (mod p) holds true. The specific form a(p-1) ≡ 1 (mod p) does not apply because a is a multiple of p.

How to Use This Fermat's Little Theorem Calculator

Using our Fermat's Little Theorem calculator is straightforward, designed for clarity and ease of understanding:

  1. Enter the Base (a): In the "Base (a)" input field, type any integer value. This can be positive, negative, or zero.
  2. Enter the Prime Modulus (p): In the "Prime Modulus (p)" input field, type a prime number. Remember, `p` must be a prime number (e.g., 2, 3, 5, 7, 11, etc.) for the theorem to apply. The calculator will automatically check if your input for `p` is prime.
  3. Click "Calculate": Once both values are entered, click the "Calculate" button.
  4. Interpret the Results:
    • The calculator will first verify if `p` is prime.
    • It will then display p-1, a mod p, a(p-1) mod p, and ap mod p.
    • A primary highlighted result will indicate whether the theorem's congruences are satisfied.
    • A note will clarify if the a(p-1) ≡ 1 (mod p) form is applicable based on whether `a` is a multiple of `p`.
  5. View the Modular Table: Below the main results, a table will show the value of ak mod p for k from 1 up to p, illustrating the cyclical nature of modular exponentiation.
  6. Analyze the Chart: A dynamic chart will visually represent the sequence of ak mod p values, helping you see the patterns.
  7. Reset: To perform a new calculation, click the "Reset" button to clear the inputs and results.
  8. Copy Results: Use the "Copy Results" button to quickly get a text summary of your calculation.

Remember that all values are unitless integers. The calculator handles large numbers using efficient modular exponentiation algorithms.

Key Factors That Affect Fermat's Little Theorem

Understanding the factors that influence Fermat's Little Theorem is crucial for its correct application and interpretation:

  1. Primality of 'p': This is the most critical factor. The theorem fundamentally relies on `p` being a prime number. If `p` is composite, the congruences generally do not hold. For instance, if `p=4` (composite), `a=3`, then `3^(4-1) = 3^3 = 27`. `27 mod 4 = 3`, which is not `1`.
  2. Relationship between 'a' and 'p': Specifically, whether `a` is a multiple of `p` (i.e., `a mod p = 0`). This determines which form of the theorem applies. If `a` is a multiple of `p`, then `a^(p-1) ≡ 0 (mod p)`, not `1`. However, `a^p ≡ a (mod p)` still holds as `0 ≡ 0 (mod p)`.
  3. Magnitude of 'a' and 'p': While the theorem holds for any integers `a` and prime `p`, practical computation limits exist. Very large numbers require specialized arbitrary-precision arithmetic libraries, which this calculator handles internally for reasonable ranges. The modular arithmetic keeps the intermediate results manageable.
  4. Modular Exponentiation Algorithm: The efficiency of calculating `a^k mod p` for large `k` is vital. Algorithms like exponentiation by squaring (binary exponentiation) are used to compute these values rapidly, especially for the high powers involved (like `p-1` or `p`).
  5. Integer Nature: Both `a` and `p` must be integers. The theorem does not extend to real or complex numbers.
  6. Applications in Cryptography: The theorem's properties are foundational for algorithms like RSA encryption. The ability to quickly compute modular exponentiation and the behavior of `a^(p-1) ≡ 1 (mod p)` are leveraged for generating keys and encrypting/decrypting messages. This is a key concept in RSA calculator implementations.

Fermat's Little Theorem FAQ

Q: What is Fermat's Little Theorem in simple terms?

A: It's a rule in number theory stating that if you pick a prime number `p` and any whole number `a`, then `a` raised to the power of `p` will have the same remainder as `a` when both are divided by `p`. If `a` is not a multiple of `p`, then `a` raised to the power of `(p-1)` will leave a remainder of 1 when divided by `p`.

Q: Why is 'p' required to be a prime number?

A: The theorem's proof relies on properties unique to prime numbers, specifically that in modular arithmetic modulo a prime `p`, every non-zero element has a multiplicative inverse. Without `p` being prime, these properties don't universally hold, and the congruences usually break down.

Q: Can 'a' be a negative number?

A: Yes, `a` can be any integer, including negative numbers. The modular arithmetic rules still apply. For example, `-3 mod 5` is `2` (since `-3 + 5 = 2`). The calculator handles negative `a` values correctly.

Q: What if 'a' is zero?

A: If `a = 0`, then `0^p ≡ 0 (mod p)` is always true for `p > 0`. Also, `0^(p-1) ≡ 0 (mod p)` for `p-1 > 0`. The theorem holds: `0 ≡ 0 (mod p)`. The specific form `a^(p-1) ≡ 1 (mod p)` would not apply as `a` is a multiple of `p` (in fact, `0` is a multiple of every integer).

Q: Does this theorem have units?

A: No, the values `a` and `p` in Fermat's Little Theorem are pure integers and are therefore unitless. The results of modular exponentiation are also unitless integers representing remainders.

Q: What is the difference between Fermat's Little Theorem and Fermat's Last Theorem?

A: They are vastly different! Fermat's Little Theorem is a simple, proven statement about prime numbers and modular arithmetic. Fermat's Last Theorem is a much more complex statement about integer solutions to `a^n + b^n = c^n` for `n > 2`, which remained an unsolved conjecture for centuries until proven by Andrew Wiles in 1994.

Q: How is Fermat's Little Theorem used in cryptography?

A: It's a cornerstone of public-key cryptography, particularly in the RSA algorithm. The property `a^(p-1) ≡ 1 (mod p)` allows for efficient modular exponentiation, which is fundamental to RSA's encryption and decryption processes. It's also used in some primality tests (though not infallible, as seen with Fermat primality test).

Q: What are Fermat pseudoprimes?

A: A composite number `n` is called a Fermat pseudoprime to base `a` if `a^(n-1) ≡ 1 (mod n)` holds, even though `n` is not prime. This shows that the converse of Fermat's Little Theorem is false and highlights why it's not a foolproof primality test.

🔗 Related Calculators