{"id":65067,"date":"2025-08-01T08:00:53","date_gmt":"2025-08-01T05:00:53","guid":{"rendered":"https:\/\/wpcalc.com\/en\/?p=65067"},"modified":"2026-09-30T20:31:00","modified_gmt":"2026-09-30T17:31:00","slug":"fermats-little-theorem","status":"publish","type":"post","link":"https:\/\/wpcalc.com\/en\/mathematics\/fermats-little-theorem\/","title":{"rendered":"Fermat\u2019s Little Theorem Calculator"},"content":{"rendered":"<section class=\"not-prose calculator-box\" aria-labelledby=\"calculator-title\"><div class=\"flex items-baseline sm:items-center justify-between gap-2 sm:gap-3\"><div class=\"flex flex-col sm:flex-row sm:items-center gap-2\"><span class=\"icon icon-calculator text-primary-700! text-base! dark:text-primary-300!\" aria-hidden=\"true\"><\/span><h2 id=\"calculator-title\" class=\"text-lg font-display font-bold\">Check Fermat\u2019s Theorem or Compute Modulo Powers<\/h2><\/div><div class=\"calcdash-favorite group\">\n  <button type=\"button\" class=\"favorite\" id=\"favorite\" data-favorite-id=\"65067\" data-favorite-title=\"Fermat\u2019s Little Theorem\" data-favorite-url=\"https:\/\/wpcalc.com\/en\/mathematics\/fermats-little-theorem\/\" data-favorite-excerpt=\"Verify Fermat&#039;s Little Theorem online: enter a base a and a prime p, and the calculator computes a^(p-1) mod p and checks whether the result equals 1. Useful for number theory, primality testing and cryptography.\" aria-label=\"Add to Favorites\" data-favorite-icon=\"icon icon-arithmetic\">\n    <span class=\"icon icon-shape-star-empty\" aria-hidden=\"true\"><\/span>\n  <\/button>\n  <div class=\"calcdash-favorite__tip\" aria-hidden=\"true\">\n   <span class=\"favorite-tooltip\" id=\"favorite-tooltip\"><\/span>\n  <\/div>\n  <span class=\"sr-only\" role=\"status\" id=\"favorite-status\"><\/span>\n<\/div><\/div><form action=\"https:\/\/wpcalc.com\/en\/mathematics\/fermats-little-theorem\/\" method=\"POST\" class=\"calculator\" id=\"calculator-65067\" data-post=\"65067\"><fieldset class=\"fieldset-input\"><legend class=\"sr-only\">Input Fields<\/legend><div class=\"field has-term\" id=\"input-1\"><label for=\"field-1\">Base (a)<\/label><div class=\"term\">a<\/div>  <div class=\"absolute bottom-0 left-16 top-8 w-2 h-0.5 bg-blue-500\"><\/div><input type=\"text\" name=\"base_a\" id=\"field-1\" aria-describedby=\"calc-info-1\" data-number inputmode=\"decimal\" autocomplete=\"off\" spellcheck=\"false\" min=\"1\" value=\"5\"\/><small id=\"calc-info-1\">Enter an integer a where 1 \u2264 a &lt; p<\/small><\/div><div class=\"field has-term\" id=\"input-2\"><label for=\"field-2\">Prime modulus (p)<\/label><div class=\"term\">p<\/div>  <div class=\"absolute bottom-0 left-16 top-8 w-2 h-0.5 bg-blue-500\"><\/div><input type=\"text\" name=\"prime_modulus_p\" id=\"field-2\" aria-describedby=\"calc-info-2\" data-number inputmode=\"decimal\" autocomplete=\"off\" spellcheck=\"false\" min=\"2\" value=\"7\"\/><small id=\"calc-info-2\">Enter a prime number p<\/small><\/div><\/fieldset><div class=\"buttons\"><button type=\"submit\" data-text=\"Re-Calculate\" id=\"calculate-button\" data-post=\"65067\">Calculate<\/button><button type=\"reset\">Reset<\/button><\/div><div class=\"field is-checkbox hidden!\" id=\"field-auto-calc\"><input type=\"checkbox\" id=\"auto-calc\"><label for=\"auto-calc\">Calculate automatically<\/label><small>If enabled, the result will update automatically when you change any value.<\/small><\/div><div class=\"fieldset-result is-hidden\" aria-labelledby=\"result-title\" aria-live=\"polite\" role=\"region\"> <h3 class=\"result-title bg-gradient-to-r from-primary-50 to-gray-50 dark:from-primary-900 dark:to-gray-800\" id=\"result-title\"><span class=\"icon icon-s-pulse\" aria-hidden=\"true\"><\/span> Your Results<\/h3><div class=\"result-box\"><div class=\"field-result\" id=\"output-1\"><span class=\"field-title\"><span>Result of a^(p\u22121) mod p<\/span><\/span><span class=\"field-value\" id=\"result-1\"><\/span><button type=\"button\" class=\"copy-result\" data-tooltip=\"Copy Result\" aria-label=\"Copy Result\"><span class=\"copy-icon icon icon-document-copy\" aria-hidden=\"true\"><\/span><\/button><\/div><div class=\"field-result\" id=\"output-2\"><span class=\"field-title\"><span>Theorem holds?<\/span><\/span><span class=\"field-value\" id=\"result-2\"><\/span><button type=\"button\" class=\"copy-result\" data-tooltip=\"Copy Result\" aria-label=\"Copy Result\"><span class=\"copy-icon icon icon-document-copy\" aria-hidden=\"true\"><\/span><\/button><\/div><\/div><div class=\"result-actions\"><button type=\"button\" class=\"copy-link\"><svg viewBox=\"0 0 20 20\" fill=\"currentColor\" aria-hidden=\"true\"><path d=\"M12.232 4.232a2.5 2.5 0 0 1 3.536 3.536l-1.225 1.224a.75.75 0 0 0 1.061 1.06l1.224-1.224a4 4 0 0 0-5.656-5.656l-3 3a4 4 0 0 0 .225 5.865.75.75 0 0 0 .977-1.138 2.5 2.5 0 0 1-.142-3.667l3-3Z\"\/><path d=\"M11.603 7.963a.75.75 0 0 0-.977 1.138 2.5 2.5 0 0 1 .142 3.667l-3 3a2.5 2.5 0 0 1-3.536-3.536l1.225-1.224a.75.75 0 0 0-1.061-1.06l-1.224 1.224a4 4 0 1 0 5.656 5.656l3-3a4 4 0 0 0-.225-5.865Z\"\/><\/svg><span class=\"copy-link__text\">Copy link to this calculation<\/span><\/button><span class=\"sr-only\" role=\"status\" id=\"copy-link-status\"><\/span><\/div><\/div><a href=\"#respond\" class=\"hidden transition-opacity duration-300 opacity-0 w-50 text-sm justify-center items-center gap-2 px-4 py-2 rounded bg-gray-200 text-gray-700 hover:bg-gray-300\" id=\"leave-comment\"><span class=\"icon icon-comments\" aria-hidden=\"true\"><\/span>Leave a Comment<\/a><\/form><\/section><section id=\"calc-reactions\" class=\"not-prose hidden my-12 bg-gradient-to-r from-primary-50 to-gray-50 border border-indigo-100 rounded-xl px-6 py-4 shadow-sm\" data-post=\"65067\" aria-live=\"polite\" aria-labelledby=\"calc-reactions-title\"><p id=\"calc-reactions-title\" class=\"text-sm text-gray-500 text-center mb-2\">How did this result make you feel?<\/p><div role=\"group\" aria-labelledby=\"calc-reactions-title\" class=\"grid grid-cols-3 sm:flex sm:flex-row sm:justify-around gap-4 sm:items-center sm:flex-wrap\"><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"like\"><span class=\"reaction-count\" data-reaction-count=\"like\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude00 <\/span><span class=\"reaction-description font-medium\">Like <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"helpful\"><span class=\"reaction-count\" data-reaction-count=\"helpful\">1<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\udca1 <\/span><span class=\"reaction-description font-medium\">Helpful <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"confused\"><span class=\"reaction-count\" data-reaction-count=\"confused\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude15 <\/span><span class=\"reaction-description font-medium\">Confused <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"disappointed\"><span class=\"reaction-count\" data-reaction-count=\"disappointed\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\ud83d\ude1e <\/span><span class=\"reaction-description font-medium\">Disappointed <\/span><\/button><button type=\"button\" aria-pressed=\"false\" class=\"reaction-btn flex flex-col items-center gap-1 text-sm text-gray-700 hover:text-primary-600 cursor-pointer transition-transform hover:scale-105\" data-reaction=\"inaccurate\"><span class=\"reaction-count\" data-reaction-count=\"inaccurate\">0<\/span><span class=\"reaction text-3xl\" aria-hidden=\"true\">\u274c <\/span><span class=\"reaction-description font-medium\">Inaccurate <\/span><\/button><\/div><div id=\"reaction-message\" class=\"hidden mt-8 rounded-md border border-primary-100 bg-white\/50 backdrop-blur-sm px-4 py-2 text-sm text-center shadow-sm\"><\/div><\/section><div class=\"calcdash-ad not-prose calcdash-ad--calc_result calcdash-ad--in-article\"><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-1721569815777345\" data-ad-slot=\"6317458308\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\"><\/ins><script>(adsbygoogle = window.adsbygoogle || []).push({});<\/script><\/div>\n<style id=\"wpcoderpro-inline-1\">.formula-box {\r\n\tborder-bottom: none !important;\r\n}<\/style><section class=\"formula-box\" aria-labelledby=\"formula-title\">\r\n\t<div id=\"formula-title\" class=\"not-prose font-display mb-4\"><span class=\"icon icon-formula\" aria-hidden=\"true\"><\/span><h2>Fermat's Little Theorem Formula<\/h2><\/div>\r\n\t<figure class=\"not-prose formula\">\r\n\t\t<figcaption class=\"formula-title\">Formula<\/figcaption>\r\n\t\t<div class=\"text-base text-gray-800\" id=\"formula\">\n\\[\\text{If } p \\text{ is prime and } a \\not\\equiv 0 \\pmod{p}, \\text{ then } a^{p-1} \\equiv 1 \\pmod{p}\\]\n<\/div>\r\n\t<\/figure>\r\n\t<\/p>\n<p><strong>Where:<\/strong><\/p>\n<ul>\n<li>\\(p\\) \u2014 a prime number (the modulus)<\/li>\n<li>\\(a\\) \u2014 an integer with \\(1 \\le a &lt; p\\), i.e. not divisible by \\(p\\)<\/li>\n<li>\\(a^{p-1} \\bmod p\\) \u2014 the remainder of \\(a^{p-1}\\) upon division by \\(p\\)<\/li>\n<\/ul>\n<p>The theorem states that for any prime \\(p\\) and any integer \\(a\\) not divisible by \\(p\\), raising \\(a\\) to the power \\(p-1\\) and reducing modulo \\(p\\) always leaves a remainder of 1. An equivalent form, \\(a^p \\equiv a \\pmod{p}\\), holds for <em>all<\/em> integers \\(a\\), including multiples of \\(p\\).<\/p>\n<p>\r\n<\/section>\n<style id=\"wpcoderpro-inline-459\">.calculation-box {\r\n\tborder-bottom: none !important;\r\n}<\/style><section id=\"calculation\" class=\"calculation-box\" aria-labelledby=\"calculation-title\">\r\n\t<div id=\"calculation-title\" class=\"not-prose font-display\"><span class=\"icon icon-unordered-list\" aria-hidden=\"true\"><\/span><h2>Fermat's Little Theorem \u2013 Calculation Example<\/h2><\/div>\r\n\r\n<\/p>\n<p><strong>Example:<\/strong> take the default values \\(a = 5\\) and \\(p = 7\\). Since 7 is prime and 5 is not divisible by 7, the theorem should apply.<\/p>\n<p>\\[a^{p-1} \\bmod p = 5^{7-1} \\bmod 7 = 5^6 \\bmod 7\\]<\/p>\n<p>\\[5^6 = 15625, \\qquad 15625 = 7 \\times 2232 + 1 \\;\\Rightarrow\\; 15625 \\bmod 7 = 1\\]<\/p>\n<p><strong>Result:<\/strong> \\(5^6 \\equiv 1 \\pmod{7}\\), so the theorem holds. \u2705<\/p>\n<p>\n\r\n<\/section>\n<h2>How to use the calculator<\/h2>\n<ol>\n<li>Enter the base \\(a\\) in the <strong>Base (a)<\/strong> field. It must satisfy \\(1 \\le a &lt; p\\).<\/li>\n<li>Enter a prime number in the <strong>Prime modulus (p)<\/strong> field (e.g. 2, 3, 5, 7, 11\u2026).<\/li>\n<li>Read the <strong>Result of a^(p\u22121) mod p<\/strong> \u2014 the remainder of \\(a^{p-1}\\) divided by \\(p\\).<\/li>\n<li>Check <strong>Theorem holds?<\/strong> \u2014 it shows \"Yes\" when the remainder equals 1, which is the case for every valid input.<\/li>\n<\/ol>\n<p>If you enter a non-prime modulus, a base outside the allowed range, or \\(a \\ge p\\), the calculator shows an error message instead of a result.<\/p>\n<h2>What Fermat's Little Theorem means<\/h2>\n<p>Fermat's Little Theorem, stated by Pierre de Fermat in 1640, is one of the cornerstones of elementary number theory. It says that prime numbers impose a remarkably regular structure on powers: no matter which base \\(a\\) you choose (as long as \\(p\\) does not divide \\(a\\)), the power \\(a^{p-1}\\) always reduces to exactly 1 modulo \\(p\\). In other words, \\(a^{p-1}\\) is always a multiple of \\(p\\) plus one.<\/p>\n<p>Why does this matter? First, it is a powerful computational shortcut. Instead of computing a huge power like \\(3^{100}\\) directly, you can reduce the exponent modulo \\(p-1\\) and work with much smaller numbers \u2014 a technique used constantly in modular arithmetic. Second, it underlies <strong>primality testing<\/strong>: if \\(a^{p-1} \\bmod p \\ne 1\\) for some \\(a\\), then \\(p\\) is definitely <em>not<\/em> prime. This is the idea behind Fermat's test and, in refined form, the Miller\u2013Rabin test used to find large primes.<\/p>\n<p>Third, the theorem is essential in <strong>cryptography<\/strong>. RSA encryption and decryption are essentially modular exponentiations, and Fermat's Little Theorem (together with its generalization, <a href=\"https:\/\/wpcalc.com\/en\/mathematics\/eulers-theorem\/\">Euler's Theorem<\/a>) is what guarantees that decrypting a message recovers the original. It also leads directly to the formula for finding <a href=\"https:\/\/wpcalc.com\/en\/mathematics\/multiplicative-inverse-modulo\/\">multiplicative inverses modulo a prime<\/a>: since \\(a^{p-1} \\equiv 1 \\pmod{p}\\), the inverse of \\(a\\) is simply \\(a^{p-2} \\bmod p\\).<\/p>\n<h2>How to interpret the result<\/h2>\n<p>For any valid input \u2014 a prime \\(p\\) and a base \\(a\\) with \\(1 \\le a &lt; p\\) \u2014 the calculator will always return 1 and &quot;Yes&quot;. That is precisely the content of the theorem: it is a statement about <em>all<\/em> such pairs, not just some. If you want to see why it fails for non-primes, try \\(a = 2\\), \\(p = 9\\): \\(2^8 = 256\\) and \\(256 \\bmod 9 = 4 \\ne 1\\). The theorem is a one-way test \u2014 a non-prime always fails for at least one base, while a pass does not by itself prove primality (see the note on Carmichael numbers below).<\/p>\n<h2>Assumptions, limitations and tips<\/h2>\n<ul>\n<li><strong>The modulus must be prime.<\/strong> The theorem does not hold for composite moduli in general. For composite \\(n\\), use Euler's Theorem with \\(a^{\\varphi(n)} \\equiv 1 \\pmod{n}\\) instead, where \\(\\varphi\\) is Euler's totient function and \\(\\gcd(a, n) = 1\\).<\/li>\n<li><strong>The base must not be divisible by \\(p\\).<\/strong> If \\(a \\equiv 0 \\pmod{p}\\), then \\(a^{p-1} \\equiv 0\\), not 1. The equivalent form \\(a^p \\equiv a \\pmod{p}\\) still holds in that case.<\/li>\n<li><strong>Fermat tests can be fooled.<\/strong> There exist composite numbers called <em>Carmichael numbers<\/em> (the smallest is 561) that satisfy \\(a^{n-1} \\equiv 1 \\pmod{n}\\) for every \\(a\\) coprime to \\(n\\). A single passing test is therefore not proof of primality.<\/li>\n<li><strong>Work with reduced exponents.<\/strong> In hand calculations, combine the theorem with successive squaring: reduce exponents modulo \\(p-1\\) and intermediate products modulo \\(p\\) to keep numbers small.<\/li>\n<li><strong>Related tools.<\/strong> For systems of congruences, see the <a href=\"https:\/\/wpcalc.com\/en\/mathematics\/chinese-remainder-theorem\/\">Chinese Remainder Theorem calculator<\/a>; for division in modular arithmetic, the <a href=\"https:\/\/wpcalc.com\/en\/mathematics\/multiplicative-inverse-modulo\/\">multiplicative inverse modulo calculator<\/a> is a natural companion.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Verify Fermat's Little Theorem online: enter a base a and a prime p, and the calculator computes a^(p-1) mod p and checks whether the result equals 1. Useful for number theory, primality testing and cryptography.<\/p>\n","protected":false},"author":3168,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7],"tags":[72],"class_list":["post-65067","post","type-post","status-publish","format-standard","hentry","category-mathematics","tag-arithmetic-calculators"],"acf":[],"_links":{"self":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/65067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/users\/3168"}],"replies":[{"embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/comments?post=65067"}],"version-history":[{"count":5,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/65067\/revisions"}],"predecessor-version":[{"id":66236,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/posts\/65067\/revisions\/66236"}],"wp:attachment":[{"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/media?parent=65067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/categories?post=65067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpcalc.com\/en\/wp-json\/wp\/v2\/tags?post=65067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}