<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://number.subwiki.org/w/index.php?action=history&amp;feed=atom&amp;title=Fermat_primality_test</id>
	<title>Fermat primality test - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://number.subwiki.org/w/index.php?action=history&amp;feed=atom&amp;title=Fermat_primality_test"/>
	<link rel="alternate" type="text/html" href="https://number.subwiki.org/w/index.php?title=Fermat_primality_test&amp;action=history"/>
	<updated>2026-07-30T23:16:44Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.2</generator>
	<entry>
		<id>https://number.subwiki.org/w/index.php?title=Fermat_primality_test&amp;diff=772&amp;oldid=prev</id>
		<title>Vipul at 23:26, 4 January 2012</title>
		<link rel="alternate" type="text/html" href="https://number.subwiki.org/w/index.php?title=Fermat_primality_test&amp;diff=772&amp;oldid=prev"/>
		<updated>2012-01-04T23:26:55Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:26, 4 January 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot;&gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Definition==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Definition==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The &#039;&#039;&#039;Fermat primality test&#039;&#039;&#039; is a quick and often inconclusive [[primality test]]. In the version below, we consider only &amp;lt;math&amp;gt;n &amp;gt; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;2 &lt;/del&gt;&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The &#039;&#039;&#039;Fermat primality test&#039;&#039;&#039; is a quick and often inconclusive [[primality test]]. In the version below, we consider only &amp;lt;math&amp;gt;n &amp;gt; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;1&lt;/ins&gt;&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given a [[natural number]] &amp;lt;math&amp;gt;n &amp;gt; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;2&lt;/del&gt;&amp;lt;/math&amp;gt;, the test works as follows:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given a [[natural number]] &amp;lt;math&amp;gt;n &amp;gt; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;1&lt;/ins&gt;&amp;lt;/math&amp;gt;, the test works as follows:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Pick a random element &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; from the congruence classes of integers mod &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (exclude the congruence &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;classes &lt;/del&gt;of 0 &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;and 1&lt;/del&gt;). Explicitly, pick &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; as an integer among &amp;lt;math&amp;gt;2,3,\dots,n-1&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Pick a random element &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; from the congruence classes of &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;nonzero &lt;/ins&gt;integers mod &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (exclude the congruence &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;class &lt;/ins&gt;of 0). Explicitly, pick &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; as an integer among &amp;lt;math&amp;gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;1,&lt;/ins&gt;2,3,\dots,n-1&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Check whether &amp;lt;math&amp;gt;a^{n-1} \equiv 1 \pmod n&amp;lt;/math&amp;gt;. To compute the power use [[repeated squaring]]. If the condition does &amp;#039;&amp;#039;not&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is composite. If the condition &amp;#039;&amp;#039;does&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; may be prime or composite.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Check whether &amp;lt;math&amp;gt;a^{n-1} \equiv 1 \pmod n&amp;lt;/math&amp;gt;. To compute the power use [[repeated squaring]]. If the condition does &amp;#039;&amp;#039;not&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is composite. If the condition &amp;#039;&amp;#039;does&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; may be prime or composite.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Vipul</name></author>
	</entry>
	<entry>
		<id>https://number.subwiki.org/w/index.php?title=Fermat_primality_test&amp;diff=771&amp;oldid=prev</id>
		<title>Vipul: Created page with &quot;{{primality test}}  ==Definition==  The &#039;&#039;&#039;Fermat primality test&#039;&#039;&#039; is a quick and often inconclusive primality test. In the version below, we consider only &lt;math&gt;n &gt; 2 &lt;/...&quot;</title>
		<link rel="alternate" type="text/html" href="https://number.subwiki.org/w/index.php?title=Fermat_primality_test&amp;diff=771&amp;oldid=prev"/>
		<updated>2012-01-04T23:23:37Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{primality test}}  ==Definition==  The &amp;#039;&amp;#039;&amp;#039;Fermat primality test&amp;#039;&amp;#039;&amp;#039; is a quick and often inconclusive &lt;a href=&quot;/w/index.php?title=Primality_test&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Primality test (page does not exist)&quot;&gt;primality test&lt;/a&gt;. In the version below, we consider only &amp;lt;math&amp;gt;n &amp;gt; 2 &amp;lt;/...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{primality test}}&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Fermat primality test&amp;#039;&amp;#039;&amp;#039; is a quick and often inconclusive [[primality test]]. In the version below, we consider only &amp;lt;math&amp;gt;n &amp;gt; 2 &amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Given a [[natural number]] &amp;lt;math&amp;gt;n &amp;gt; 2&amp;lt;/math&amp;gt;, the test works as follows:&lt;br /&gt;
&lt;br /&gt;
# Pick a random element &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; from the congruence classes of integers mod &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (exclude the congruence classes of 0 and 1). Explicitly, pick &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; as an integer among &amp;lt;math&amp;gt;2,3,\dots,n-1&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Check whether &amp;lt;math&amp;gt;a^{n-1} \equiv 1 \pmod n&amp;lt;/math&amp;gt;. To compute the power use [[repeated squaring]]. If the condition does &amp;#039;&amp;#039;not&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is composite. If the condition &amp;#039;&amp;#039;does&amp;#039;&amp;#039; hold, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; may be prime or composite.&lt;br /&gt;
&lt;br /&gt;
The above is a &amp;#039;&amp;#039;single iteration&amp;#039;&amp;#039; of the test. Multiple iterations of the test may be carried out with multiple random elements. If at any stage the condition &amp;lt;math&amp;gt;a^{n-1} \equiv 1 \pmod n&amp;lt;/math&amp;gt; is violated, we can conclude that the number is composite. If, however, the condition holds every time, the situation is inconclusive and &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; may be prime or composite.&lt;br /&gt;
&lt;br /&gt;
==Theory==&lt;br /&gt;
&lt;br /&gt;
===Why it works===&lt;br /&gt;
&lt;br /&gt;
The primality test above is correct because of [[Fermat&amp;#039;s little theorem]], which says that for a prime &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; relatively prime to &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;, we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;a^{p-1} \equiv 1 \pmod p&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Behavior on composite numbers===&lt;br /&gt;
&lt;br /&gt;
For any composite number &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, we use the following terms:&lt;br /&gt;
&lt;br /&gt;
* A [[Fermat witness]] for &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a value &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;a^{n-1} \not \equiv 1 \pmod n&amp;lt;/math&amp;gt;.&lt;br /&gt;
* A [[Fermat liar]] for &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a value &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;a^{n-1} \equiv 1 \pmod n&amp;lt;/math&amp;gt;.&lt;br /&gt;
* We say that &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a [[Fermat pseudoprime]] to base &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; if &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; is a Fermat liar for &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Nonzero numbers that have a common factor with &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; &amp;#039;&amp;#039;always&amp;#039;&amp;#039; serve as Fermat witnesses. There are &amp;lt;math&amp;gt;n - \varphi(n) - 1&amp;lt;/math&amp;gt; of these where &amp;lt;math&amp;gt;\varphi&amp;lt;/math&amp;gt; is the [[Euler totient function]]. For &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; composite, this number is &amp;#039;&amp;#039;at least&amp;#039;&amp;#039; equal to &amp;lt;math&amp;gt;\sqrt{n} - 1&amp;lt;/math&amp;gt;, the worst case arising when &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a square of a prime. In general, if &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; has a small number of large prime factors, there are very few such numbers and a uniform random choice is highly unlikely to hit them.&lt;br /&gt;
&lt;br /&gt;
For the numbers that are relatively prime to &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, we have the [[formula for probability of relatively prime integer being a Fermat liar]]. If the probability is 1, then &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a [[Carmichael number]]. If the probability is less than 1, then it is at most 1/2, which means that the Fermat primality test can find a witness with arbitrarily high probability of success in a finite number of trials.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Related tests==&lt;br /&gt;
&lt;br /&gt;
* [[Miller-Rabin primality test]]&lt;br /&gt;
* [[Solovay-Strassen primality test]]&lt;/div&gt;</summary>
		<author><name>Vipul</name></author>
	</entry>
</feed>