<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.exploringbinary.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Exploring Binary</title>
	
	<link>http://www.exploringbinary.com</link>
	<description>Binary Numbers, Binary Code, and Binary Logic</description>
	<lastBuildDate>Thu, 11 Mar 2010 15:03:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.exploringbinary.com/ExploringBinary" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="exploringbinary" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">ExploringBinary</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Counting Binary/Hexadecimal Palindromes</title>
		<link>http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/</link>
		<comments>http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 17:06:25 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Geometric series]]></category>
		<category><![CDATA[Proof]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=272</guid>
		<description><![CDATA[In my article &#8220;Counting Binary and Hexadecimal Palindromes&#8221; I derived formulas for counting binary palindromes and hexadecimal palindromes. For each type of palindrome, I derived two pairs of formulas: one pair to count n-digit palindromes, and one pair to count palindromes of n digits or less.
In this article, I will derive similar formulas to count [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/">Counting Binary/Hexadecimal Palindromes</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In my article <a href="http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/" title="Read Rick Regan's Article &ldquo;Counting Binary and Hexadecimal Palindromes&rdquo;">&ldquo;Counting Binary and Hexadecimal Palindromes&rdquo;</a> I derived formulas for counting binary palindromes and hexadecimal palindromes. For each type of palindrome, I derived two pairs of formulas: one pair to count n-digit palindromes, and one pair to count palindromes of n digits or less.</p>
<p>In this article, I will derive similar formulas to count binary/hexadecimal palindromes &#8212; <a href="http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/" title="Read Rick Regan's Article &ldquo;The Structure of Binary/Hexadecimal Palindromes&rdquo;">multi-base palindromes I&#8217;ve shown to have an algorithmically defined structure</a>.</p>
<p><span id="more-272"></span></p>
<h2>Binary/Hexadecimal Palindromes of n Hexadecimal Digits</h2>
<p>To derive the formula to count binary/hexadecimal palindromes, look at the tables in my article <a href="http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/" title="Read Rick Regan's Article &ldquo;The Structure of Binary/Hexadecimal Palindromes&rdquo;">&ldquo;The Structure of Binary/Hexadecimal Palindromes&rdquo;</a>. There&#8217;s a table of palindromes for each starting hexadecimal digit: 1, 3, 5, 7, 9, and F. Each table lists the binary/hexadecimal palindromes of one to five hex digits, for each starting digit. If you count the palindromes in the tables &#8212; and continue the pattern beyond five digits &#8212; this is what you&#8217;ll get:</p>
<table class="center" border="1" summary="Counts of nonzero, n hex digit binary/hexadecimal palindromes">
<caption><strong>Counts of Nonzero, n Hex Digit Binary/Hexadecimal Palindromes</strong></caption>
<tbody>
<tr>
<th class="center">Starting Hex Digit</th>
<th class="center">Count From n=1 &#8230; </th>
</tr>
<tr>
<td class="left">1</td>
<td class="left">1, 1, 2, 2, 4, 4, 8, 8, &#8230;</td>
</tr>
<tr>
<td class="left">3</td>
<td class="left">1, 1, 2, 2, 4, 4, 8, 8, &#8230;</td>
</tr>
<tr>
<td class="left">5</td>
<td class="left">1, 1, 4, 4, 16, 16, 64, 64, &#8230;</td>
</tr>
<tr>
<td class="left">7</td>
<td class="left">1, 1, 4, 4, 16, 16, 64, 64, &#8230;</td>
</tr>
<tr>
<td class="left">9</td>
<td class="left">1, 1, 4, 4, 16, 16, 64, 64, &#8230;</td>
</tr>
<tr>
<td class="left">F</td>
<td class="left">1, 1, 4, 4, 16, 16, 64, 64, &#8230;</td>
</tr>
</tbody>
</table>
<p>This pattern continues forever, based on the structure of the palindromes.</p>
<p>(Notice we&#8217;re not counting 0 as a palindrome &#8212; if you want to, add 1 to the formulas we derive below.)</p>
<p>There are two different count sequences: one of interleaved nonnegative powers of two, and one of interleaved nonnegative powers of four. As we learned when <a href="http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/" title="Read Rick Regan's Article &ldquo;Counting Binary and Hexadecimal Palindromes&rdquo;">counting n-digit binary palindromes</a>, the sequence of interleaved nonnegative powers of two is captured by these two formulas:</p>
<ul>
<li>When n is even: 2<sup>n/2-1</sup></li>
<li>When n is odd: 2<sup>(n+1)/2-1</sup></li>
</ul>
<p>Those two formulas apply to the starting hex digits 1 and 3, counting the number of n hex digit binary/hexadecimal palindromes for each.</p>
<p>You can use similar formulas to generate the sequence of interleaved nonnegative powers of four:</p>
<ul>
<li>When n is even: 4<sup>n/2-1</sup> = 2<sup>2&middot;(n/2-1)</sup> = 2<sup>n-2</sup></li>
<li>When n is odd: 4<sup>(n+1)/2-1</sup> = 2<sup>2&middot;((n+1)/2-1)</sup> = 2<sup>n-1</sup></li>
</ul>
<p>(It should not be surprising that these expressions simplify to powers of two &#8212; powers of four are powers of two.)</p>
<p>Those two formulas apply to the starting hex digits 5, 7, 9, and F, counting the number of n hex digit binary/hexadecimal palindromes for each.</p>
<h3>Combining the Formulas</h3>
<p>The formulas above count the number of n hex digit binary/hexadecimal palindromes by starting hex digit. We can combine them into one pair of formulas that counts <em>all</em> n hex digit binary/hexadecimal palindromes, by adding two copies of the first pair of formulas to four copies of the second pair:</p>
<ul>
<li>When n is even: 2&middot;2<sup>n/2-1</sup> + 4&middot;2<sup>n-2</sup> = <span class="highlight_theme_blue_box">2<sup>n/2</sup> + 2<sup>n</sup></span></li>
<li>When n is odd: 2&middot;2<sup>(n+1)/2-1</sup> + 4&middot;2<sup>n-1</sup> = <span class="highlight_theme_blue_box">2<sup>(n+1)/2</sup> + 2<sup>n+1</sup></span></li>
</ul>
<h2>Binary/Hexadecimal Palindromes of n Hexadecimal Digits or Less</h2>
<p>To compute the total number of binary/hexadecimal palindromes of n hex digits or less, we need to add together the count of palindromes for each number of digits, 1 through n. We could create a series using the pair of formulas above, but it&#8217;s easier to start from scratch: we&#8217;ll create a series for each starting digit, and then add them together. </p>
<p>The count of palindromes for each starting digit, from 1 to n digits, is an interleaved sequence &#8212; either of nonnegative powers of two or nonnegative powers of four. If you separate those sequences and add their terms &#8212; creating series &#8212; you&#8217;ll have these building blocks, which I&#8217;ll use to derive a formula:</p>
<ul>
<li>The sum of the first m nonnegative powers of two: <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/a7caf098e64f4c7cefae4ba5d99311a9.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{m-1}2^i}}}'/> = 2<sup>m</sup> &#8211; 1 .</li>
<li>The sum of the first m nonnegative powers of four: <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/c2a59ba17783d30b33957beaa8158c36.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{m-1}4^i}}}'/> = (4<sup>m</sup> &#8211; 1)/3 .</li>
</ul>
<p>We can use these expressions for each of the interleaved series, adding them together to get expressions that count palindromes of n digits or less by starting digit. We can then combine <em>those</em> expressions to get one pair of formulas &#8212; one that counts <em>all</em> palindromes of n digits or less.</p>
<h3>Total Palindromes for Each of the Starting Digits 1 and 3</h3>
<ul>
<li><strong>The number of digits n is even</strong>
<p>The sum consists of two identical series of n/2 terms each:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/759a5a69c86e49a3f5a72accf5d77b49.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n}{2}-1}2^i}}}'/> = 2<sup>n/2</sup> &#8211; 1.</p>
<p>Added together, they are <strong>2(2<sup>n/2</sup> &#8211; 1)</strong>.</p>
</li>
<li><strong>The number of digits n is odd</strong>
<p>The sum consists of two identical series of (n-1)/2 terms each, plus an extra term. Each series looks like this:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/e1b7237ca73f62e78f9e3fb5b0c376f0.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n-1}{2}-1}2^i}}}'/> = 2<sup>(n-1)/2</sup> &#8211; 1.</p>
<p>Added together, they are 2&middot;(2<sup>(n-1)/2</sup> &#8211; 1).</p>
<p>The extra term is 2<sup>(n-1)/2</sup>, what would have been the next term in one of the series. Adding this to the two series we get 2(2<sup>(n-1)/2</sup> &#8211; 1) + 2<sup>(n-1)/2</sup>, which simplifies to <strong>3&middot;2<sup>(n-1)/2</sup> &#8211; 2</strong>.</p>
</li>
</ul>
<h3>Total Palindromes for Each of the Starting Digits 5, 7, 9, and F</h3>
<ul>
<li><strong>The number of digits n is even</strong>
<p>The sum consists of two identical series of n/2 terms each:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/7c0351dcd3dd141b39ee9ac4f1f5d941.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n}{2}-1}4^i}}}'/> = (4<sup>n/2</sup> &#8211; 1)/3.</p>
<p>Added together, they are <strong>2&middot;(4<sup>n/2</sup> &#8211; 1)/3</strong>.</p>
</li>
<li><strong>The number of digits n is odd</strong>
<p>The sum consists of two identical series of (n-1)/2 terms each, plus an extra term. Each series looks like this:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-hexadecimal-palindromes/70de5e7f1b0c74eec45c0ce2ed8e469e.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n-1}{2}-1}4^i}}}'/> =  (4<sup>(n-1)/2</sup> &#8211; 1)/3.</p>
<p>Added together, they are 2&middot;(4<sup>(n-1)/2</sup> &#8211; 1)/3.</p>
<p>The extra term is 4<sup>(n-1)/2</sup>, what would have been the next term in one of the series. Adding this to the two series we get 2(4<sup>(n-1)/2</sup> &#8211; 1)/3 + 4<sup>(n-1)/2</sup>, which simplifies to <strong>(5&middot;4<sup>(n-1)/2</sup> &#8211; 2)/3</strong>.</p>
</li>
</ul>
<h3>Total Palindromes for All Digits</h3>
<p>To get the total palindromes of n digits or less, we add two copies of the &ldquo;1/3&rdquo; pair of formulas to four copies of the &ldquo;5/7/9/F&rdquo; pair of formulas:</p>
<ul>
<li><strong>The number of digits n is even</strong>
<p>2(2(2<sup>n/2</sup> &#8211; 1)) + 4(2(4<sup>n/2</sup> &#8211; 1)/3) = <span class="highlight_theme_blue_box">4(2<sup>n/2</sup> &#8211; 1) + 8(2<sup>n</sup> &#8211; 1)/3</span></p>
</li>
<li><strong>The number of digits n is odd</strong>
<p>2(3&middot;2<sup>(n-1)/2</sup> &#8211; 2) + 4((5&middot;4<sup>(n-1)/2</sup> &#8211; 2)/3) = <span class="highlight_theme_blue_box">2(3&middot;2<sup>(n-1)/2</sup> &#8211; 2) + 4(5&middot;2<sup>n-1</sup> &#8211; 2)/3</span></p>
</li>
</ul>
<h2>Summary</h2>
<p>Here is a summary of the binary/hexadecimal palindrome counting formulas:</p>
<table class="center" border="1" summary="Summary of binary/hexadecimal nonzero palindrome counting formulas">
<caption><strong>Nonzero Binary/Hexadecimal Palindrome Counting Formulas</strong></caption>
<tbody>
<tr>
<th class="center">Num Hex Digits</th>
<th class="center">Even n</th>
<th class="center">Odd n</th>
</tr>
<tr>
<td class="left">n</td>
<td class="center">2<sup>n/2</sup> + 2<sup>n</sup></td>
<td class="center">2<sup>(n+1)/2</sup> + 2<sup>n+1</sup></td>
</tr>
<tr>
<td class="left">n or less</td>
<td class="center">4(2<sup>n/2</sup> &#8211; 1) + 8(2<sup>n</sup> &#8211; 1)/3</td>
<td class="center">2(3&middot;2<sup>(n-1)/2</sup> &#8211; 2) + 4(5&middot;2<sup>n-1</sup> &#8211; 2)/3</td>
</tr>
</tbody>
</table>
<p class="break">Here&#8217;s a table listing the values of these formulas for n = 1 to 16:</p>
<table class="center" border="1" summary="All nonzero 1 through 16 hex digit binary/hexadecimal palindromes">
<caption><strong>1-16 Hex Digit Binary/Hexadecimal Palindromes (not counting 0)</strong></caption>
<tbody>
<tr>
<th class="center">Num Hex Digits (n)</th>
<th class="center">Num n-digit Palindromes</th>
<th class="center">Total Palindromes</th>
</tr>
<tr>
<td class="left">1</td>
<td class="center">6</td>
<td class="center">6</td>
</tr>
<tr>
<td class="left">2</td>
<td class="center">6</td>
<td class="center">12</td>
</tr>
<tr>
<td class="left">3</td>
<td class="center">20</td>
<td class="center">32</td>
</tr>
<tr>
<td class="left">4</td>
<td class="center">20</td>
<td class="center">52</td>
</tr>
<tr>
<td class="left">5</td>
<td class="center">72</td>
<td class="center">124</td>
</tr>
<tr>
<td class="left">6</td>
<td class="center">72</td>
<td class="center">196</td>
</tr>
<tr>
<td class="left">7</td>
<td class="center">272</td>
<td class="center">468</td>
</tr>
<tr>
<td class="left">8</td>
<td class="center">272</td>
<td class="center">740</td>
</tr>
<tr>
<td class="left">9</td>
<td class="center">1056</td>
<td class="center">1796</td>
</tr>
<tr>
<td class="left">10</td>
<td class="center">1056</td>
<td class="center">2852</td>
</tr>
<tr>
<td class="left">11</td>
<td class="center">4160</td>
<td class="center">7012</td>
</tr>
<tr>
<td class="left">12</td>
<td class="center">4160</td>
<td class="center">11172</td>
</tr>
<tr>
<td class="left">13</td>
<td class="center">16512</td>
<td class="center">27684</td>
</tr>
<tr>
<td class="left">14</td>
<td class="center">16512</td>
<td class="center">44196</td>
</tr>
<tr>
<td class="left">15</td>
<td class="center">65792</td>
<td class="center">109988</td>
</tr>
<tr>
<td class="left">16</td>
<td class="center">65792</td>
<td class="center">175780</td>
</tr>
</tbody>
</table>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/">Counting Binary/Hexadecimal Palindromes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Install and Run GMP on Windows Using MPIR</title>
		<link>http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/</link>
		<comments>http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 19:35:02 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Exponents]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=270</guid>
		<description><![CDATA[To perform arbitrary-precision arithmetic in C and C++ programs on Windows, I use GMP. In particular, I use MPIR, a Windows port of GMP. MPIR is a simple alternative to using GMP under Cygwin or MinGW.
I will show you how to install MPIR in Microsoft Visual C++ as a static, 32-bit library. I will also [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/">How to Install and Run GMP on Windows Using MPIR</a></p>
]]></description>
			<content:encoded><![CDATA[<p>To perform arbitrary-precision arithmetic in C and C++ programs on Windows, I use <a title="GMP" href="http://gmplib.org/">GMP</a>. In particular, I use <a title="MPIR" href="http://www.mpir.org/">MPIR</a>, a <a title="MPIR Description" href="http://gladman.plushost.co.uk/oldsite/computing/gmp4win.php">Windows port of GMP</a>. MPIR is a simple alternative to using GMP under <a title="Cygwin" href="http://www.cygwin.com/">Cygwin</a> or <a title="MinGW" href="http://www.mingw.org/">MinGW</a>.</p>
<p>I will show you how to install MPIR in Microsoft Visual C++ as a static, 32-bit library. I will also show you how to install the <em>optional</em> C++ interface &#8212; also as a static, 32-bit library. I will provide two example C programs that call the GMP integer and floating-point functions, and two equivalent C++ programs &#8212; programs that use the same GMP functions, only indirectly through the C++ interface.</p>
<p><span id="more-270"></span></p>
<h2>1. Download What You Need</h2>
<p>To install MPIR and run it under Visual C++ as I have, you&#8217;ll need four things:</p>
<ul>
<li>Microsoft Visual C++ (I use <a title="Download page for Visual Studio Express" href="http://www.microsoft.com/eXPress/download/">Visual C++ 2008 Express Edition</a>).</li>
<li>A file archiver (I use <a title="7-Zip Download" href="http://www.7-zip.org/download.html">7-Zip</a>).</li>
<li>The MPIR source code and Visual C++ solution (I use <a title="MPIR 1.3.1 Source Code Download" href="http://www.mpir.org/mpir-1.3.1.tar.gz">mpir-1.3.1.tar.gz</a>, which is based on GMP version 4.2.1).</li>
<li>The Yasm assembler (I use <a title="Yasm Assembler Download" href="http://www.tortall.net/projects/yasm/releases/yasm-0.8.0-win32.exe">yasm-0.8.0-win32.exe</a>).</li>
</ul>
<p>(I won&#8217;t describe how to install Visual C++ or 7-zip.)</p>
<h2>2. Prepare to Build MPIR</h2>
<p>MPIR must be built with Visual C++; to prepare for that, do the following:</p>
<ol>
<li>From mpir-1.3.1.tar.gz, use your file archiver to extract mpir-1.3.1.tar; from mpir-1.3.1.tar, extract folder <strong>mpir-1.3.1</strong>.</li>
<li>Copy \mpir-1.3.1\build.vc9\yasm.rules to the Visual C++ project defaults directory (mine is <strong>C:\Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults\</strong>).</li>
<li>Rename file yasm-0.8.0-win32.exe to <strong>yasm.exe</strong>.</li>
<li>Move yasm.exe to the Visual C++ binary directory (mine is <strong>C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\</strong>).</li>
</ol>
<h2>3. Build MPIR</h2>
<p>Here&#8217;s how to build MPIR as a static Win32 library:</p>
<ol>
<li>Open the MPIR Visual C++ solution <strong>\mpir-1.3.1\build.vc9\mpir.sln</strong>.
<p>You get the following error message if you are using Visual Studio Express:</p>
<p><em>The project consists entirely of configurations that require support for platforms which are not installed on this machine. The project cannot be loaded.</em></p>
<p><img src="http://www.exploringbinary.com/wp-content/uploads/mpir-project-not-loaded.jpg" alt="64-bit projects error message" width="540" height="85"/></p>
<p>This message is referring to the 64-bit projects, which we won&#8217;t be using anyway. Hit &lsquo;Enter&rsquo; to ignore (do this eight times &#8212; one for each 64-bit project in the solution.)</p>
</li>
<li>Right click on &ldquo;Solution &lsquo;mpir&rsquo; (14 projects)&rdquo;, select &ldquo;Properties&rdquo; and then click on &ldquo;Configuration Properties&rdquo; and then click on &ldquo;Configuration Manager&rdquo;. In the box labeled &ldquo;Active solution configuration&rdquo; select &ldquo;Release&rdquo; and then click &ldquo;Close&rdquo; and then click &ldquo;OK&rdquo;.
<p><img src="http://www.exploringbinary.com/wp-content/uploads/mpir-release.jpg" alt="Change to Release" width="540" height="366"/></p>
</li>
<li>Build the version of the library you want by right clicking on the project name and selecting &ldquo;Build&rdquo; (I built <strong>lib_mpir_p4</strong>, labeled in the readme file as the &ldquo;MPIR library using Pentium IV assembler&rdquo;).
<p><img src="http://www.exploringbinary.com/wp-content/uploads/mpir-build.lib_mpir_p4.jpg" alt="Build lib_mpir_p4" width="235" height="462"/></p>
<p>A successful build of lib_mpir_p4, which should take less than two minutes, gives this message:</p>
<p><em>Build: 6 succeeded, 0 failed, 0 up-to-date, 0 skipped</em></p>
<p> (You can ignore the compiler warning messages.)</p>
</li>
<li><em>Optional: for C++ interface only</em>: Build the C++ interface by right clicking on project <strong>lib_mpir_cxx</strong> and selecting &ldquo;Build&rdquo; (again, ignore the compiler warning messages).</li>
</ol>
<h2>4. Install the MPIR Library in Visual C++</h2>
<p>To install MPIR in Visual C++, you need to copy three files from the MPIR build directory into corresponding Visual C++ directories:</p>
<ul>
<li>Copy files <strong>mpir.lib</strong> and <strong>mpir.pdb</strong> from
<p>\mpir-1.3.1\build.vc9\<strong>lib_mpir_p4</strong>\Win32\Release\</p>
<p>to the Visual C++ library directory</p>
<p>C:\Program Files\Microsoft Visual Studio 9.0\VC\<strong>lib\</strong>.</p>
</li>
<li>Copy \mpir-1.3.1\build.vc9\<strong>lib_mpir_p4</strong>\Win32\Release\<strong>mpir.h</strong>
<p>to the Visual C++ include directory</p>
<p>C:\Program Files\Microsoft Visual Studio 9.0\VC\<strong>include\</strong>.</p>
</li>
</ul>
<p><em>Optional: for C++ interface only</em>: To install the library for the C++ interface, you need to copy three files from the MPIR build directory into corresponding Visual C++ directories:</p>
<ul>
<li>Copy files <strong>mpirxx.lib</strong> and <strong>mpirxx.pdb</strong> from
<p>\mpir-1.3.1\build.vc9\<strong>lib_mpir_cxx</strong>\Win32\Release\</p>
<p>to the Visual C++ library directory</p>
<p>C:\Program Files\Microsoft Visual Studio 9.0\VC\<strong>lib\</strong>.</p>
</li>
<li>Copy \mpir-1.3.1\build.vc9\<strong>lib_mpir_cxx</strong>\Win32\Release\<strong>mpirxx.h</strong>
<p>to the Visual C++ include directory</p>
<p>C:\Program Files\Microsoft Visual Studio 9.0\VC\<strong>include\</strong>.</p>
</li>
</ul>
<p>(Unless you want to build another library, you can delete the folder mpir-1.3.1 at this point, and perhaps file mpir-1.3.1.tar.gz too. You might also want to wait until you run the test programs below.)</p>
<h2>5. Configure Your Visual C++ Project to Use MPIR</h2>
<ul>
<li>Open an existing Visual C++ solution or create a new one.</li>
<li>Right click on the project name and select &ldquo;Properties&rdquo;. Then click &ldquo;Configuration Properties&rdquo;, then click &ldquo;Linker&rdquo;, and then click &ldquo;Command Line&rdquo;. In the &ldquo;Additional options&rdquo; box, enter &ldquo;<strong>mpir.lib</strong>&rdquo; and click OK.
<p><img src="http://www.exploringbinary.com/wp-content/uploads/mpir-add-library.jpg" alt="Add library to command line" width="540" height="376"/></p>
<p><em>Optional: for C++ interface only</em>: Also add &ldquo;<strong>mpirxx.lib</strong>&rdquo; to the command line:</p>
<p><img src="http://www.exploringbinary.com/wp-content/uploads/mpir-add-library-cpp.jpg" alt="Add C++ library to command line" width="434" height="140"/></p>
</li>
<li>Right click on the project name and select &ldquo;Properties&rdquo;. Then click &ldquo;Configuration Properties&rdquo;, then click &ldquo;C/C++&rdquo;, and then click &ldquo;Code Generation&rdquo;. In the &ldquo;Runtime Library&rdquo; pulldown menu, select &ldquo;<strong>Multi-threaded (/MT)</strong>&rdquo; and click OK. This takes care of the linker warning message you would otherwise get:
<p>
<em>LINK : warning LNK4098: defaultlib &#8216;LIBCMT&#8217; conflicts with use of other libs; use /NODEFAULTLIB:library</em>
</p>
</li>
</ul>
<h2>Writing a C/C++ Program that Uses the Standard GMP Interface</h2>
<p>To access the GMP functions through their standard interface from your C/C++ source code, you must include the file &ldquo;<strong>mpir.h</strong>&rdquo;, as follows:</p>
<p><strong>#include &lt;mpir.h&gt;</strong></p>
<p>(A vanilla GMP program would have the line &ldquo;#include &lt;gmp.h&gt;&rdquo; instead; other than this, there are no source code differences.)</p>
<p>The MPIR GMP functions are documented in the <a title="MPIR manual" href="http://www.mpir.org/mpir-1.3.1.pdf">MPIR manual</a>, which is essentially the same document as the <a title="GMP manual" href="http://gmplib.org/gmp-man-5.0.1.pdf">GMP manual</a>.</p>
<p>Here are two C programs I wrote that you can use to test out the standard GMP interface: one uses some integer functions, and one uses some floating-point functions:</p>
<h3>C Program Using GMP Integers Through the Standard Interface</h3>
<pre>
#include &lt;stdio.h&gt;
#include &lt;mpir.h&gt;

int main (int argc, char *argv[])
{
 mpz_t aBigPO2;

 mpz_init(aBigPO2);

 mpz_set_ui(aBigPO2, 1073741824); //2^30
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^60
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^120
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^240
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^480
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^960
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^1920

 mpz_out_str(stdout,10,aBigPO2);
 printf (&quot;\n&quot;);

 mpz_clear(aBigPO2);
}
</pre>
<p>This prints out 2<sup>1920</sup>:</p>
<p>949711451807891414054698636958849699906924706346851167428009563305851<br />
662866960338751057874083211050161729488483879798993810787765480587192<br />
741530384819193300769874625884321977783469748956377553448566093328992<br />
717820774610081821193616932757859144579109671494034728110890670954570<br />
186561270637912025593911079819522904974136715161890547150302121514577<br />
299257466073410681074505560366912534455201581754427662731068044605805<br />
987604257959314070588213630129796572870132647963130222671409082294912<br />
848599974253399700073940596408585364978789157781640247045138282505908<br />
97948604589281308443672576</p>
<p>(I verified this with <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a>).</p>
<h3>C Program Using GMP Floating-Point Numbers Through the Standard Interface</h3>
<pre>
#include &lt;stdio.h&gt;
#include &lt;mpir.h&gt;

int main (int argc, char *argv[])
{
 mpf_t aSmallPO2;

 mpf_init2(aSmallPO2,4449); //Set precision to 4449 bits

 mpf_set_d (aSmallPO2,0.000000000931322574615478515625); //2^-30
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-60
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-120
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-240
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-480
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-960
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-1920

 mpf_out_str (stdout,10,0,aSmallPO2);
 printf (&quot;\n&quot;);

 mpf_clear(aSmallPO2);
}
</pre>
<p>In case you are wondering, 4449 bits of precision is the minimum needed to print all the digits of 2<sup>-1920</sup>, which is:</p>
<p>0.10529513970757941370610747782095090628278032322332406685248932058897<br />
4872190707039601594120403123249585501787935873212314961661043417067942<br />
8278180446530326422046737097860094653249850843254396524641486587776526<br />
8438334467392843186701182578047022952222095279213542847547625013806063<br />
5939813263124389547821599195698069457805581599831394945162148920227813<br />
5870110642683804987806409062559492005002245124038200955046244238057665<br />
8723954500997837640072880232154236046343680085877727078528072722123195<br />
3662151119904110195371481974425580486352182740921723969072266817056082<br />
2079226689557687055189455076290288097855736909213224995450090355633626<br />
2479360506670537902070512869428970881638135729845035256195565406246915<br />
5908284846389916152190520040559448133294815988479693870277001617833335<br />
8035796655478612315835426745796222381146595433091161056422873002023624<br />
5605524128583533967685249775398427450933606825973311143113749366383842<br />
6445031772775456966272872821230559421629513369329254203751128364614621<br />
2033824114891508165551708120643223406118819602960923135752402206346995<br />
9597949051896076865883172740955991670305216873490456538967283758977304<br />
0869715545540580641662604687640672610266478149937775606620121327969356<br />
0952412990814675362368055269333370619407233848969981813715084977716048<br />
9153948872988676360548263714707910434915092735830288717124858521856367<br />
588043212890625e-577</p>
<p>(I verified this with <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a>).</p>
<h2>Writing a C++ Program that Uses the GMP C++ Interface</h2>
<p>(To use the C++ interface you will have had to have followed the build steps labeled &lsquo;<em>optional</em>&rsquo; above.)</p>
<p>To access the GMP functions through their C++ interface, you must include the file &ldquo;<strong>mpirxx.h</strong>&rdquo;, as follows:</p>
<p><strong>#include &lt;mpirxx.h&gt;</strong></p>
<p>(A vanilla GMP program would have the line &ldquo;#include &lt;gmpxx.h&gt;&rdquo; instead.)</p>
<p>Also, your source code will require this additional line, if you want to remove the compiler warnings due to mpirxx.h:</p>
<p>#pragma warning(disable: 4800)</p>
<p>Without this, you will get about a dozen warning messages like this:</p>
<p><em>c:\program files\microsoft visual studio 9.0\vc\include\mpirxx.h(1610) : warning C4800: &#8216;int&#8217; : forcing value to bool &#8216;true&#8217; or &#8216;false&#8217; (performance warning)</em></p>
<p>The MPIR GMP C++ interface is documented in the <a title="MPIR manual" href="http://www.mpir.org/mpir-1.3.1.pdf">MPIR manual</a> (and the <a title="GMP manual" href="http://gmplib.org/gmp-man-5.0.1.pdf">GMP manual</a>).</p>
<p>Here are two C++ programs I wrote that you can use to test out the GMP C++ interface: they are the equivalent of the two C programs above, except they&#8217;re expressed much more naturally (as to be expected in a C++ program):</p>
<h3>C++ Program Using GMP Integers Through the C++ Interface</h3>
<pre>
#include &lt;iostream&gt;
using namespace std;

#pragma warning(disable: 4800)
#include &lt;mpirxx.h&gt;
#pragma warning(default: 4800)

int main (int argc, char *argv[])
{
  mpz_class aBigPO2;

  aBigPO2 = 1073741824; //2^30
  aBigPO2*=aBigPO2; //2^60
  aBigPO2*=aBigPO2; //2^120
  aBigPO2*=aBigPO2; //2^240
  aBigPO2*=aBigPO2; //2^480
  aBigPO2*=aBigPO2; //2^960
  aBigPO2*=aBigPO2; //2^1920

  cout &lt;&lt; aBigPO2 &lt;&lt; endl;
}
</pre>
<p>(This gives the same output as the C program above.)</p>
<h3>C++ Program Using GMP Floating-Point Numbers Through the C++ Interface</h3>
<pre>
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
using namespace std;

#pragma warning(disable: 4800)
#include &lt;mpirxx.h&gt;
#pragma warning(default: 4800)

int main (int argc, char *argv[])
{
  mpf_class aSmallPO2(0,4449); //Init to 0, precision 4449 bits

  aSmallPO2 = 0.000000000931322574615478515625; //2^-30
  aSmallPO2*=aSmallPO2; //2^-60
  aSmallPO2*=aSmallPO2; //2^-120
  aSmallPO2*=aSmallPO2; //2^-240
  aSmallPO2*=aSmallPO2; //2^-480
  aSmallPO2*=aSmallPO2; //2^-960
  aSmallPO2*=aSmallPO2; //2^-1920

  cout &lt;&lt; setprecision (1343) &lt;&lt; aSmallPO2 &lt;&lt; endl;
}
</pre>
<p>The value of 1343 to setprecision is the precision in <em>decimal</em> digits.</p>
<p>The output differs slightly from it&#8217;s corresponding C program (the number is shifted one place and has the corresponding exponent to match):</p>
<p>1.05295139707579413706107477820950906282780323223324066852489320588974<br />
8721907070396015941204031232495855017879358732123149616610434170679428<br />
2781804465303264220467370978600946532498508432543965246414865877765268<br />
4383344673928431867011825780470229522220952792135428475476250138060635<br />
9398132631243895478215991956980694578055815998313949451621489202278135<br />
8701106426838049878064090625594920050022451240382009550462442380576658<br />
7239545009978376400728802321542360463436800858777270785280727221231953<br />
6621511199041101953714819744255804863521827409217239690722668170560822<br />
0792266895576870551894550762902880978557369092132249954500903556336262<br />
4793605066705379020705128694289708816381357298450352561955654062469155<br />
9082848463899161521905200405594481332948159884796938702770016178333358<br />
0357966554786123158354267457962223811465954330911610564228730020236245<br />
6055241285835339676852497753984274509336068259733111431137493663838426<br />
4450317727754569662728728212305594216295133693292542037511283646146212<br />
0338241148915081655517081206432234061188196029609231357524022063469959<br />
5979490518960768658831727409559916703052168734904565389672837589773040<br />
8697155455405806416626046876406726102664781499377756066201213279693560<br />
9524129908146753623680552693333706194072338489699818137150849777160489<br />
1539488729886763605482637147079104349150927358302887171248585218563675<br />
88043212890625e-578</p>
<p class="break">Besides the much cleaner syntax of the C++ programs, the underlying GMP functions &#8212; &ldquo;*_init&rdquo;, &ldquo;*_clear&rdquo;, &ldquo;*_mul&rdquo;, etc. &#8212; are hidden in the C++ classes.</p>
<h2>For Further Information</h2>
<p>This article is my distillation of the MPIR readme file for a specific build scenario &#8212; please refer to \mpir-1.3.1\build.vc9\readme.txt for details on other builds.</p>
<h2>Acknowledgement</h2>
<p>Thanks to Brian Gladman for answering my questions and for suggesting I explore the C++ interface.</p>
<h2>Feedback</h2>
<p>If you try these instructions, let me know how it works out (and if you have suggestions to improve them, please let me know that too).</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/">How to Install and Run GMP on Windows Using MPIR</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to Install and Run PARI/GP Calculator on Windows</title>
		<link>http://www.exploringbinary.com/how-to-install-and-run-pari-gp-calculator-on-windows/</link>
		<comments>http://www.exploringbinary.com/how-to-install-and-run-pari-gp-calculator-on-windows/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 21:01:11 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=268</guid>
		<description><![CDATA[PARI/GP calculator, or gp for short, is an arbitrary-precision calculator (among other things) that  I use frequently in my study of binary numbers. Here are instructions for installing and running it on Windows.

Installing

Download the  PARI/GP self-installing binary distribution for Windows.
As of this writing, the current stable version is Pari-2-3-4.exe. (There&#8217;s no need to [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/how-to-install-and-run-pari-gp-calculator-on-windows/">How to Install and Run PARI/GP Calculator on Windows</a></p>
]]></description>
			<content:encoded><![CDATA[<p>PARI/GP calculator, or <strong>gp</strong> for short, is an arbitrary-precision calculator (among other things) that <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;"> I use frequently in my study of binary numbers</a>. Here are instructions for installing and running it on Windows.</p>
<p><span id="more-268"></span></p>
<h2>Installing</h2>
<ul>
<li>Download the <a title="PARI/GP download for Windows" href="http://pari.math.u-bordeaux.fr/download.html"> PARI/GP self-installing binary distribution for Windows</a>.
<p>As of this writing, the current stable version is Pari-2-3-4.exe. (There&#8217;s no need to download or install the optional packages &#8212; at least I don&#8217;t need them).</p>
</li>
<li>Double click on the installer file to install (install all components).</li>
</ul>
<h3>Changing Settings</h3>
<p>You can set gp default settings by editing the gp customization file, &ldquo;\PARI\.gprc&rdquo;. For example, I have edited mine as follows:</p>
<ul>
<li>Changed the command prompt from the default timestamp format (prompt = &quot;(%H:%M) gp > &quot;) to &#8216;?&#8217; (<strong>prompt = &quot;? &quot;</strong>).</li>
<li>Added a line (<strong>format = &quot;f&quot;</strong>) that makes all decimals print without scientific notation.</li>
</ul>
<h2>Running</h2>
<p>Here are some basic things you should know about running gp:</p>
<ul>
<li><strong>Starting gp</strong>. Launch gp from the PARI folder under the Windows start menu.</li>
<li><strong>Setting variables</strong>. You can assign constants or results of calculations to variables; for example, <strong>n=8</strong>.</li>
<li><strong>Reusing prior calculations</strong>. The results of calculations are stored in a history array: <strong>%1</strong> is the result of the first calculation, <strong>%2</strong> is the result of the second calculation, etc. You can use these as variables in subsequent expressions.</li>
<li><strong>Changing the default precision</strong>. The default precision of printed decimal values is 28 significant digits; this can be changed with the &lsquo;<strong>\p</strong>&rsquo; command. For example, &lsquo;<strong>\p 50</strong>&rsquo; changes the print precision to 50 digits (PARI may set its internal precision higher than what you ask for; it must respect boundaries dictated by its implementation).</li>
<li><strong>Printing decimals instead of fractions</strong>. To force gp to print decimals instead of fractions, make sure a decimal point appears in the expression; for example, <strong>2.^-2</strong></li>
<li><strong>Executing previous commands</strong>. Previous commands can be accessed with the up arrow and down arrow keys.</li>
<li><strong>Copying and pasting</strong>. This is cumbersome, but can be done by right-clicking on the top of the command window and selecting &lsquo;<strong>edit</strong>&rsquo;. For copy, select &lsquo;<strong>mark</strong>&rsquo;, highlight the desired text in the command window and then hit enter. For paste, select &lsquo;<strong>paste</strong>&rsquo;.</li>
<li><strong>Logging your session</strong>. Type &lsquo;<strong>\l</strong>&rsquo; to record your session &#8212; all commands and output &#8212; to the default PARI log file (\PARI\examples\pari.log). Type &lsquo;<strong>\l</strong>&rsquo; again to stop recording.</li>
<li><strong>Exiting gp</strong>. Type &lsquo;<strong>quit</strong>&rsquo; to exit gp.</li>
</ul>
<div class="wp-caption aligncenter" style="width: 534px"><img src="http://www.exploringbinary.com/wp-content/uploads/PARI-gp-running.jpg" alt="Running PARI/GP Calculator" width="524" height="290"/><p class="wp-caption-text">Running PARI/GP Calculator</p></div>
<h2>Examples</h2>
<p>See my article <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">&ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;</a> for example gp calculations.</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/how-to-install-and-run-pari-gp-calculator-on-windows/">How to Install and Run PARI/GP Calculator on Windows</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/how-to-install-and-run-pari-gp-calculator-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Structure of Binary/Hexadecimal Palindromes</title>
		<link>http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/</link>
		<comments>http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 16:57:14 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Binary integers]]></category>
		<category><![CDATA[Convert to hexadecimal]]></category>
		<category><![CDATA[Definition]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=265</guid>
		<description><![CDATA[Binary/hexadecimal palindromes are integers that are palindromic in both binary and hexadecimal. Unlike binary/decimal palindromes, for example, they have a predictable structure. This means they can be generated directly, rather than searched for. So what is their structure?
Certainly they&#8217;re made up of the hexadecimal digits that are themselves palindromic in binary: 0, 6, 9, F; [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/">The Structure of Binary/Hexadecimal Palindromes</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Binary/hexadecimal palindromes are integers that are palindromic in both binary and hexadecimal. Unlike binary/decimal palindromes, for example, they have a predictable structure. This means they can be generated directly, rather than <a href="http://www.exploringbinary.com/finding-numbers-that-are-palindromic-in-multiple-bases/" title="Read Rick Regan's Article &ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;">searched for</a>. So what is their structure?</p>
<p>Certainly they&#8217;re made up of the hexadecimal digits that are themselves palindromic in binary: 0, 6, 9, F; for example, F060F<sub>16</sub> = 11110000011000001111<sub>2</sub> and 9F9<sub>16</sub> = 100111111001<sub>2</sub>. Each of these four hexadecimal digits maps neatly to a 4-digit binary palindrome, so any hexadecimal palindrome made from them is automatically palindromic in binary.</p>
<p>But there are other binary/hexadecimal palindromes, like 525<sub>16</sub> = 10100100101<sub>2</sub> and 70207<sub>16</sub> = 1110000001000000111<sub>2</sub>, that contain hexadecimal digits that are <em>not</em> palindromic in binary. In this case, binary palindromes are produced with <em>combinations</em> of hexadecimal digits. It turns out there are a limited number of valid combinations, and that they&#8217;re localized &#8212; they span only <em>two</em> hexadecimal digits.</p>
<p>In this article, I&#8217;ll analyze binary/hexadecimal palindromes and describe their structure &#8212; a structure due to the relationship of the two bases, binary and hexadecimal.</p>
<div class="wp-caption aligncenter" style="width: 544px"><img src="http://www.exploringbinary.com/wp-content/uploads/hex-bin-palindromes.png" alt="Example Binary/Hexadecimal Palindromes" width="534" height="213"/><p class="wp-caption-text">Example Binary/Hexadecimal Palindromes</p></div>
<p><span id="more-265"></span></p>
<h2>Inferring the Structure of Binary/Hexadecimal Palindromes</h2>
<p>Before determining the structure of binary/hexadecimal palindromes analytically, let&#8217;s look at more examples. I listed the first 124 (nonzero) binary/hexadecimal palindromes &#8212; that is, those palindromes consisting of five hexadecimal  digits or less &#8212; using my <a href="http://www.exploringbinary.com/finding-numbers-that-are-palindromic-in-multiple-bases/" title="Read Rick Regan's Article &ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;">C program that finds multi-base palindromes</a>. I noticed that each palindrome starts (and thus ends) with one of six hexadecimal digits: 1, 3, 5, 7, 9, F. I also noticed that, depending on the starting digit, only certain middle digits appear:</p>
<ul>
<li>Palindromes starting with 1: only 0 and 1 appear as middle digits.</li>
<li>Palindromes starting with 3: only 0 and 3 appear as middle digits.</li>
<li>Palindromes starting with 5 or 7: only 0, 2, 5, and 7 appear as middle digits.</li>
<li>Palindromes starting with 9 or F: only 0, 6, 9, and F appear as middle digits.</li>
</ul>
<p>Within each of the four groups of palindromes, the middle digits alternate in each digit position, in the order listed above. </p>
<p>Here is the output of my program, segmented by starting digit (the highlighted digits in the hexadecimal palindromes are the digits &ldquo;inserted&rdquo; into them to take them from n digits to n+1 digits):</p>
<p>Palindromes starting with hex &lsquo;1&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex 1)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with 1<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">1<sub>16</sub></td>
<td class="right internal_border_bottom">1<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">1</span>1<sub>16</sub></td>
<td class="right internal_border_top_bottom">10001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">1<span class="highlight_theme_blue">0</span>1<sub>16</sub></td>
<td class="right internal_border_top">100000001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">1<span class="highlight_theme_blue">1</span>1<sub>16</sub></td>
<td class="right internal_border_bottom">100010001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">1<span class="highlight_theme_blue">0</span>01<sub>16</sub></td>
<td class="right internal_border_top">1000000000001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">1<span class="highlight_theme_blue">1</span>11<sub>16</sub></td>
<td class="right internal_border_bottom">1000100010001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">10<span class="highlight_theme_blue">0</span>01<sub>16</sub></td>
<td class="right internal_border_top">10000000000000001<sub>2</sub></td>
</tr>
<tr>
<td class="right">10<span class="highlight_theme_blue">1</span>01<sub>16</sub></td>
<td class="right">10000000100000001<sub>2</sub></td>
</tr>
<tr>
<td class="right">11<span class="highlight_theme_blue">0</span>11<sub>16</sub></td>
<td class="right">10001000000010001<sub>2</sub></td>
</tr>
<tr>
<td class="right">11<span class="highlight_theme_blue">1</span>11<sub>16</sub></td>
<td class="right">10001000100010001<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">Palindromes starting with &lsquo;3&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex 3)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with 3<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">3<sub>16</sub></td>
<td class="right internal_border_bottom">11<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">3</span>3<sub>16</sub></td>
<td class="right internal_border_top_bottom">110011<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">3<span class="highlight_theme_blue">0</span>3<sub>16</sub></td>
<td class="right internal_border_top">1100000011<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">3<span class="highlight_theme_blue">3</span>3<sub>16</sub></td>
<td class="right internal_border_bottom">1100110011<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">3<span class="highlight_theme_blue">0</span>03<sub>16</sub></td>
<td class="right internal_border_top">11000000000011<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">3<span class="highlight_theme_blue">3</span>33<sub>16</sub></td>
<td class="right internal_border_bottom">11001100110011<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">30<span class="highlight_theme_blue">0</span>03<sub>16</sub></td>
<td class="right internal_border_top">110000000000000011<sub>2</sub></td>
</tr>
<tr>
<td class="right">30<span class="highlight_theme_blue">3</span>03<sub>16</sub></td>
<td class="right">110000001100000011<sub>2</sub></td>
</tr>
<tr>
<td class="right">33<span class="highlight_theme_blue">0</span>33<sub>16</sub></td>
<td class="right">110011000000110011<sub>2</sub></td>
</tr>
<tr>
<td class="right">33<span class="highlight_theme_blue">3</span>33<sub>16</sub></td>
<td class="right">110011001100110011<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">Palindromes starting with hex &lsquo;5&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex 5)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with 5<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">5<sub>16</sub></td>
<td class="right internal_border_bottom">101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">5</span>5<sub>16</sub></td>
<td class="right internal_border_top_bottom">1010101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">5<span class="highlight_theme_blue">0</span>5<sub>16</sub></td>
<td class="right internal_border_top">10100000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">5<span class="highlight_theme_blue">2</span>5<sub>16</sub></td>
<td class="right">10100100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">5<span class="highlight_theme_blue">5</span>5<sub>16</sub></td>
<td class="right">10101010101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">5<span class="highlight_theme_blue">7</span>5<sub>16</sub></td>
<td class="right internal_border_bottom">10101110101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">5<span class="highlight_theme_blue">0</span>05<sub>16</sub></td>
<td class="right internal_border_top">101000000000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">5<span class="highlight_theme_blue">2</span>25<sub>16</sub></td>
<td class="right">101001000100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">5<span class="highlight_theme_blue">5</span>55<sub>16</sub></td>
<td class="right">101010101010101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">5<span class="highlight_theme_blue">7</span>75<sub>16</sub></td>
<td class="right internal_border_bottom">101011101110101<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">50<span class="highlight_theme_blue">0</span>05<sub>16</sub></td>
<td class="right internal_border_top">1010000000000000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">50<span class="highlight_theme_blue">2</span>05<sub>16</sub></td>
<td class="right">1010000001000000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">50<span class="highlight_theme_blue">5</span>05<sub>16</sub></td>
<td class="right">1010000010100000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">50<span class="highlight_theme_blue">7</span>05<sub>16</sub></td>
<td class="right">1010000011100000101<sub>2</sub></td>
</tr>
<tr>
<td class="right">52<span class="highlight_theme_blue">0</span>25<sub>16</sub></td>
<td class="right">1010010000000100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">52<span class="highlight_theme_blue">2</span>25<sub>16</sub></td>
<td class="right">1010010001000100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">52<span class="highlight_theme_blue">5</span>25<sub>16</sub></td>
<td class="right">1010010010100100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">52<span class="highlight_theme_blue">7</span>25<sub>16</sub></td>
<td class="right">1010010011100100101<sub>2</sub></td>
</tr>
<tr>
<td class="right">55<span class="highlight_theme_blue">0</span>55<sub>16</sub></td>
<td class="right">1010101000001010101<sub>2</sub></td>
</tr>
<tr>
<td class="right">55<span class="highlight_theme_blue">2</span>55<sub>16</sub></td>
<td class="right">1010101001001010101<sub>2</sub></td>
</tr>
<tr>
<td class="right">55<span class="highlight_theme_blue">5</span>55<sub>16</sub></td>
<td class="right">1010101010101010101<sub>2</sub></td>
</tr>
<tr>
<td class="right">55<span class="highlight_theme_blue">7</span>55<sub>16</sub></td>
<td class="right">1010101011101010101<sub>2</sub></td>
</tr>
<tr>
<td class="right">57<span class="highlight_theme_blue">0</span>75<sub>16</sub></td>
<td class="right">1010111000001110101<sub>2</sub></td>
</tr>
<tr>
<td class="right">57<span class="highlight_theme_blue">2</span>75<sub>16</sub></td>
<td class="right">1010111001001110101<sub>2</sub></td>
</tr>
<tr>
<td class="right">57<span class="highlight_theme_blue">5</span>75<sub>16</sub></td>
<td class="right">1010111010101110101<sub>2</sub></td>
</tr>
<tr>
<td class="right">57<span class="highlight_theme_blue">7</span>75<sub>16</sub></td>
<td class="right">1010111011101110101<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">Palindromes starting with hex &lsquo;7&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex 7)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with 7<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">7<sub>16</sub></td>
<td class="right internal_border_bottom">111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">7</span>7<sub>16</sub></td>
<td class="right internal_border_top_bottom">1110111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">7<span class="highlight_theme_blue">0</span>7<sub>16</sub></td>
<td class="right internal_border_top">11100000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">7<span class="highlight_theme_blue">2</span>7<sub>16</sub></td>
<td class="right">11100100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">7<span class="highlight_theme_blue">5</span>7<sub>16</sub></td>
<td class="right">11101010111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">7<span class="highlight_theme_blue">7</span>7<sub>16</sub></td>
<td class="right internal_border_bottom">11101110111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">7<span class="highlight_theme_blue">0</span>07<sub>16</sub></td>
<td class="right internal_border_top">111000000000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">7<span class="highlight_theme_blue">2</span>27<sub>16</sub></td>
<td class="right">111001000100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">7<span class="highlight_theme_blue">5</span>57<sub>16</sub></td>
<td class="right">111010101010111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">7<span class="highlight_theme_blue">7</span>77<sub>16</sub></td>
<td class="right internal_border_bottom">111011101110111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">70<span class="highlight_theme_blue">0</span>07<sub>16</sub></td>
<td class="right internal_border_top">1110000000000000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">70<span class="highlight_theme_blue">2</span>07<sub>16</sub></td>
<td class="right">1110000001000000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">70<span class="highlight_theme_blue">5</span>07<sub>16</sub></td>
<td class="right">1110000010100000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">70<span class="highlight_theme_blue">7</span>07<sub>16</sub></td>
<td class="right">1110000011100000111<sub>2</sub></td>
</tr>
<tr>
<td class="right">72<span class="highlight_theme_blue">0</span>27<sub>16</sub></td>
<td class="right">1110010000000100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">72<span class="highlight_theme_blue">2</span>27<sub>16</sub></td>
<td class="right">1110010001000100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">72<span class="highlight_theme_blue">5</span>27<sub>16</sub></td>
<td class="right">1110010010100100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">72<span class="highlight_theme_blue">7</span>27<sub>16</sub></td>
<td class="right">1110010011100100111<sub>2</sub></td>
</tr>
<tr>
<td class="right">75<span class="highlight_theme_blue">0</span>57<sub>16</sub></td>
<td class="right">1110101000001010111<sub>2</sub></td>
</tr>
<tr>
<td class="right">75<span class="highlight_theme_blue">2</span>57<sub>16</sub></td>
<td class="right">1110101001001010111<sub>2</sub></td>
</tr>
<tr>
<td class="right">75<span class="highlight_theme_blue">5</span>57<sub>16</sub></td>
<td class="right">1110101010101010111<sub>2</sub></td>
</tr>
<tr>
<td class="right">75<span class="highlight_theme_blue">7</span>57<sub>16</sub></td>
<td class="right">1110101011101010111<sub>2</sub></td>
</tr>
<tr>
<td class="right">77<span class="highlight_theme_blue">0</span>77<sub>16</sub></td>
<td class="right">1110111000001110111<sub>2</sub></td>
</tr>
<tr>
<td class="right">77<span class="highlight_theme_blue">2</span>77<sub>16</sub></td>
<td class="right">1110111001001110111<sub>2</sub></td>
</tr>
<tr>
<td class="right">77<span class="highlight_theme_blue">5</span>77<sub>16</sub></td>
<td class="right">1110111010101110111<sub>2</sub></td>
</tr>
<tr>
<td class="right">77<span class="highlight_theme_blue">7</span>77<sub>16</sub></td>
<td class="right">1110111011101110111<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">Palindromes starting with hex &lsquo;9&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex 9)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with 9<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">9<sub>16</sub></td>
<td class="right internal_border_bottom">1001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">9</span>9<sub>16</sub></td>
<td class="right internal_border_top_bottom">10011001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">9<span class="highlight_theme_blue">0</span>9<sub>16</sub></td>
<td class="right internal_border_top">100100001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9<span class="highlight_theme_blue">6</span>9<sub>16</sub></td>
<td class="right">100101101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9<span class="highlight_theme_blue">9</span>9<sub>16</sub></td>
<td class="right">100110011001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">9<span class="highlight_theme_blue">F</span>9<sub>16</sub></td>
<td class="right internal_border_bottom">100111111001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">9<span class="highlight_theme_blue">0</span>09<sub>16</sub></td>
<td class="right internal_border_top">1001000000001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9<span class="highlight_theme_blue">6</span>69<sub>16</sub></td>
<td class="right">1001011001101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9<span class="highlight_theme_blue">9</span>99<sub>16</sub></td>
<td class="right">1001100110011001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">9<span class="highlight_theme_blue">F</span>F9<sub>16</sub></td>
<td class="right internal_border_bottom">1001111111111001<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">90<span class="highlight_theme_blue">0</span>09<sub>16</sub></td>
<td class="right internal_border_top">10010000000000001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">90<span class="highlight_theme_blue">6</span>09<sub>16</sub></td>
<td class="right">10010000011000001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">90<span class="highlight_theme_blue">9</span>09<sub>16</sub></td>
<td class="right">10010000100100001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">90<span class="highlight_theme_blue">F</span>09<sub>16</sub></td>
<td class="right">10010000111100001001<sub>2</sub></td>
</tr>
<tr>
<td class="right">96<span class="highlight_theme_blue">0</span>69<sub>16</sub></td>
<td class="right">10010110000001101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">96<span class="highlight_theme_blue">6</span>69<sub>16</sub></td>
<td class="right">10010110011001101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">96<span class="highlight_theme_blue">9</span>69<sub>16</sub></td>
<td class="right">10010110100101101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">96<span class="highlight_theme_blue">F</span>69<sub>16</sub></td>
<td class="right">10010110111101101001<sub>2</sub></td>
</tr>
<tr>
<td class="right">99<span class="highlight_theme_blue">0</span>99<sub>16</sub></td>
<td class="right">10011001000010011001<sub>2</sub></td>
</tr>
<tr>
<td class="right">99<span class="highlight_theme_blue">6</span>99<sub>16</sub></td>
<td class="right">10011001011010011001<sub>2</sub></td>
</tr>
<tr>
<td class="right">99<span class="highlight_theme_blue">9</span>99<sub>16</sub></td>
<td class="right">10011001100110011001<sub>2</sub></td>
</tr>
<tr>
<td class="right">99<span class="highlight_theme_blue">F</span>99<sub>16</sub></td>
<td class="right">10011001111110011001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9F<span class="highlight_theme_blue">0</span>F9<sub>16</sub></td>
<td class="right">10011111000011111001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9F<span class="highlight_theme_blue">6</span>F9<sub>16</sub></td>
<td class="right">10011111011011111001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9F<span class="highlight_theme_blue">9</span>F9<sub>16</sub></td>
<td class="right">10011111100111111001<sub>2</sub></td>
</tr>
<tr>
<td class="right">9F<span class="highlight_theme_blue">F</span>F9<sub>16</sub></td>
<td class="right">10011111111111111001<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">Palindromes starting with hex &lsquo;F&rsquo;:</p>
<table class="center" border="1" summary="1-5 digit binary/hex palindromes, starting with hex F)">
<caption><strong>Binary/Hexadecimal Palindromes (1-5 Hex Digits, Starting with F<sub>16</sub>)</strong></caption>
<tbody>
<tr>
<th class="center">Hexadecimal</th>
<th class="center">Binary</th>
</tr>
<tr>
<td class="right internal_border_bottom">F<sub>16</sub></td>
<td class="right internal_border_bottom">1111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top_bottom"><span class="highlight_theme_blue">F</span>F<sub>16</sub></td>
<td class="right internal_border_top_bottom">11111111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">F<span class="highlight_theme_blue">0</span>F<sub>16</sub></td>
<td class="right internal_border_top">111100001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F<span class="highlight_theme_blue">6</span>F<sub>16</sub></td>
<td class="right">111101101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F<span class="highlight_theme_blue">9</span>F<sub>16</sub></td>
<td class="right">111110011111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">F<span class="highlight_theme_blue">F</span>F<sub>16</sub></td>
<td class="right internal_border_bottom">111111111111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">F<span class="highlight_theme_blue">0</span>0F<sub>16</sub></td>
<td class="right internal_border_top">1111000000001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F<span class="highlight_theme_blue">6</span>6F<sub>16</sub></td>
<td class="right">1111011001101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F<span class="highlight_theme_blue">9</span>9F<sub>16</sub></td>
<td class="right">1111100110011111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_bottom">F<span class="highlight_theme_blue">F</span>FF<sub>16</sub></td>
<td class="right internal_border_bottom">1111111111111111<sub>2</sub></td>
</tr>
<tr>
<td class="right internal_border_top">F0<span class="highlight_theme_blue">0</span>0F<sub>16</sub></td>
<td class="right internal_border_top">11110000000000001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F0<span class="highlight_theme_blue">6</span>0F<sub>16</sub></td>
<td class="right">11110000011000001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F0<span class="highlight_theme_blue">9</span>0F<sub>16</sub></td>
<td class="right">11110000100100001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F0<span class="highlight_theme_blue">F</span>0F<sub>16</sub></td>
<td class="right">11110000111100001111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F6<span class="highlight_theme_blue">0</span>6F<sub>16</sub></td>
<td class="right">11110110000001101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F6<span class="highlight_theme_blue">6</span>6F<sub>16</sub></td>
<td class="right">11110110011001101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F6<span class="highlight_theme_blue">9</span>6F<sub>16</sub></td>
<td class="right">11110110100101101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F6<span class="highlight_theme_blue">F</span>6F<sub>16</sub></td>
<td class="right">11110110111101101111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F9<span class="highlight_theme_blue">0</span>9F<sub>16</sub></td>
<td class="right">11111001000010011111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F9<span class="highlight_theme_blue">6</span>9F<sub>16</sub></td>
<td class="right">11111001011010011111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F9<span class="highlight_theme_blue">9</span>9F<sub>16</sub></td>
<td class="right">11111001100110011111<sub>2</sub></td>
</tr>
<tr>
<td class="right">F9<span class="highlight_theme_blue">F</span>9F<sub>16</sub></td>
<td class="right">11111001111110011111<sub>2</sub></td>
</tr>
<tr>
<td class="right">FF<span class="highlight_theme_blue">0</span>FF<sub>16</sub></td>
<td class="right">11111111000011111111<sub>2</sub></td>
</tr>
<tr>
<td class="right">FF<span class="highlight_theme_blue">6</span>FF<sub>16</sub></td>
<td class="right">11111111011011111111<sub>2</sub></td>
</tr>
<tr>
<td class="right">FF<span class="highlight_theme_blue">9</span>FF<sub>16</sub></td>
<td class="right">11111111100111111111<sub>2</sub></td>
</tr>
<tr>
<td class="right">FF<span class="highlight_theme_blue">F</span>FF<sub>16</sub></td>
<td class="right">11111111111111111111<sub>2</sub></td>
</tr>
</tbody>
</table>
<p class="break">You can see that binary palindromes starting with hex digits 1, 3, 5, and 7 are not multiples of four bits. This is because leading zeros do not count. Each of those four hex digits &#8212; when used as a starting digit &#8212; requires less than four bits. In this case, it&#8217;s important to remember that hex digits are formed in groups of four bits, <em>from right to left</em>.</p>
<h2>Starting Digits</h2>
<p>We&#8217;ve seen empirically that a <strong>nonzero binary/hexadecimal palindrome starts with the hexadecimal digit 1, 3, 5, 7, 9, or F</strong> &#8212; now it&#8217;s time to prove it.</p>
<p>First, observe that the starting hex digit must be odd: an even hex digit ends with a 0 when written in binary, and palindromes can&#8217;t start with 0. This leaves eight starting digits as candidates: 1, 3, 5, 7, 9, B, D, F.</p>
<p>Let&#8217;s classify our palindromes into two categories: single hex digit and multiple hex digit. A single hex digit palindrome is a single hex digit that is palindromic in binary: 1, 3, 5, 7, 9, and F are the only choices (1<sub>16</sub> = 1<sub>2</sub>, 3<sub>16</sub> = 11<sub>2</sub>, 5<sub>16</sub> = 101<sub>2</sub>, 7<sub>16</sub> = 111<sub>2</sub>, 9<sub>16</sub> = 1001<sub>2</sub>, F<sub>16</sub> = 1111<sub>2</sub>). A multiple hex digit palindrome <em>as a whole</em> is palindromic in binary, but individually, each hex digit may not be &#8212; and in fact, <em>will</em> not be &#8212; palindromic in binary.</p>
<p>Let&#8217;s analyze multiple hex digit palindromes by classifying them into two categories, depending on whether their ending hex digit has a binary equivalent that starts with a 1 or 0:</p>
<ul>
<li><strong>Binary equivalent starts with 1</strong>.
<p>The hexadecimal palindrome will have an associated binary palindrome that is a multiple of four bits. This means that each hex digit must stand alone as a binary palindrome: 9 and F are the only choices for the starting digit. There are two 2-digit palindromes that start with 9 and F: 99<sub>16</sub> = 10011001<sub>2</sub> and FF<sub>16</sub> = 11111111<sub>2</sub>; these will serve as &ldquo;bookends&rdquo; for all palindromes of 3 digits or more.</p>
</li>
<li><strong>Binary equivalent starts with 0</strong>.
<p>The hexadecimal palindrome will have an associated binary palindrome that is <em>not</em> a multiple of four bits. This means that each hex digit does <em>not</em> stand alone as a binary palindrome.</p>
<p>The best way to show which starting digits are allowed in this case is to try all four candidates as 2-digit palindromes: 11<sub>16</sub> = 10001<sub>2</sub>, 33<sub>16</sub> = 110011<sub>2</sub>, 55<sub>16</sub> = 1010101<sub>2</sub>, and 77<sub>16</sub> = 1110111<sub>2</sub>. All are binary palindromes, so 1, 3, 5, 7 are the valid starting digits.</p>
</li>
</ul>
<h2>Middle Digits</h2>
<p>We’ve seen empirically what the middle digits of multiple digit binary/hexadecimal palindromes are, and that they follow a pattern &#8212; now I&#8217;ll show why.</p>
<p><strong>Definition</strong>. I&#8217;ll say a number is a <em><strong>leading-zero palindrome</strong></em> or is <em><strong>leading-zero palindromic</strong></em> if it is palindromic with zero or more leading zeros. </p>
<h3>Middle Digits for Starting Digits 9, F</h3>
<p>Palindromes starting with 9 or F are straightforward to analyze, so I&#8217;ll start with them. Each middle hex digit must stand alone as a leading-zero binary palindrome, so 0, 6, 9, and F are the choices.</p>
<p>The structure of these palindromes is easy to describe. The n+1 hex digit palindromes can be created from the n hex digit palindromes by inserting the digits 0, 6, 9, or F. When n is odd, one n+1 digit palindrome is spawned from each n digit palindrome, by replicating the middle hex digit (which will be 0, 6, 9 or F). When n is even, four n+1 digit palindromes are spawned from each n digit palindrome, by inserting the digits 0, 6, 9, and F in turn.</p>
<h3>Middle Digits for Starting Digits 1, 3, 5, 7</h3>
<p>The analysis of palindromes starting with 1, 3, 5, or 7 is based on the number of leading zeros in the binary representation of the ending hex digit; let&#8217;s define the three cases:</p>
<ul>
<li>1 leading zero: 5 (0101) and 7 (0111).</li>
<li>2 leading zeros: 3 (0011).</li>
<li>3 leading zeros: 1 (0001).</li>
</ul>
<p>Recall how palindrome generation works: each inserted hex digit takes an n digit palindrome and turns it into an n+1 digit palindrome. To ensure the result remains a binary palindrome, the inserted hex digit must maintain a binary palindrome around the &ldquo;center&rdquo;. Here&#8217;s how it&#8217;s done:</p>
<ul>
<li>The binary representation of the inserted hex digit must have the same number of leading zeros as the binary representation of the hex digit to its right.</li>
<li>The binary representation of the inserted hex digit must be palindromic without the leading zeros.</li>
</ul>
<p>In other words, the inserted leading zeros mirror the existing leading zeros, and the rest of the hex digit &#8212; which is between the sets of zeros &#8212; stands alone as a binary palindrome.</p>
<p>(There&#8217;s an alternate way to think of this: the inserted hex digit, with the one, two, or three leading binary zeros in the hex digit to its right, is a leading zero palindrome. The left side of this palindrome replaces the leading zeros used from the right.)</p>
<p>So what are the allowable middle digits? Hex digits 0-7 are the candidates, since they&#8217;re the only 4-bit hex digits that have leading zeros in their binary representations. But after you strip away the required number of leading zeros, only 0, 1, 2, 3, 5, and 7 stand alone as binary palindromes:</p>
<ul>
<li>1 leading zero: 0 (000), 2 (010), 5 (101), 7 (111).</li>
<li>2 leading zeros: 0 (00) or 3 (11).</li>
<li>3 leading zeros: 0 (0) or 1 (1).</li>
</ul>
<p>The insertion of middle digits can continue indefinitely, since the number of binary leading zeros in each group is the same for each digit. Any middle digit in each group can be adjacent to any other. The number of leading zeros in the ending digit is the number of leading zeros maintained throughout.</p>
<h4>Example</h4>
<p>This diagram shows an example of binary/hexadecimal palindrome generation:</p>
<div class="wp-caption aligncenter" style="width: 338px"><img src="http://www.exploringbinary.com/wp-content/uploads/palin-gen-example.png" alt="Example Binary/Hexadecimal Palindrome Generation" width="328" height="465"/><p class="wp-caption-text">Example Binary/Hexadecimal Palindrome Generation</p></div>
<h2>Summary</h2>
<p>In summary, the (nonzero) binary/hexadecimal palindromes are</p>
<ul>
<li>The single digit hexadecimal palindromes 1, 3, 5, 7, 9, and F.</li>
<li>All hexadecimal palindromes that start and end with 1 and have only 0s and 1s between.</li>
<li>All hexadecimal palindromes that start and end with 3 and have only 0s and 3s between.</li>
<li>All hexadecimal palindromes that start and end with 5 or 7 and have only 0s, 2s, 5s, and 7s between.</li>
<li>All hexadecimal palindromes that start and end with 9 or F and have only 0s, 6s, 9s, and Fs between.</li>
</ul>
<p class="break">(And now that we know their structure we can count them &#8212; see my article <a href="http://www.exploringbinary.com/counting-binary-hexadecimal-palindromes/" title="Read Rick Regan's Article &ldquo;Counting Binary/Hexadecimal Palindromes&rdquo;">&ldquo;Counting Binary/Hexadecimal Palindromes&rdquo;</a>.)</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/">The Structure of Binary/Hexadecimal Palindromes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/the-structure-of-binary-hexadecimal-palindromes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Counting Binary and Hexadecimal Palindromes</title>
		<link>http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/</link>
		<comments>http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 16:00:59 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Geometric series]]></category>
		<category><![CDATA[Proof]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=264</guid>
		<description><![CDATA[How many nonzero, n-digit, decimal number palindromes are there? These two formulas give the answer: 

When n is even: 9&#183;10n/2-1
When n is odd: 9&#183;10(n+1)/2-1

How many nonzero, decimal number palindromes are there, consisting of n-digits or less? These two formulas give the answer: 

When n is even: 2(10n/2 &#8211; 1)
When n is odd: 11&#183;10(n-1)/2 &#8211; 2

So [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/">Counting Binary and Hexadecimal Palindromes</a></p>
]]></description>
			<content:encoded><![CDATA[<p>How many nonzero, n-digit, decimal <a href="http://www.exploringbinary.com/finding-numbers-that-are-palindromic-in-multiple-bases/" title="Read Rick Regan's Article &ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;">number palindromes</a> are there? These two <a title="Sloane's &ldquo;Number of nonzero palindromes of length n&rdquo;" href="http://www.research.att.com/~njas/sequences/A050683">formulas</a> give the answer: </p>
<ul>
<li>When n is even: 9&middot;10<sup>n/2-1</sup></li>
<li>When n is odd: 9&middot;10<sup>(n+1)/2-1</sup></li>
</ul>
<p>How many nonzero, decimal number palindromes are there, consisting of n-digits <em>or less</em>? These two <a title="Wolfram MathWorld Article on Palindromic Numbers" href="http://mathworld.wolfram.com/PalindromicNumber.html">formulas</a> give the answer: </p>
<ul>
<li>When n is even: 2(10<sup>n/2</sup> &#8211; 1)</li>
<li>When n is odd: 11&middot;10<sup>(n-1)/2</sup> &#8211; 2</li>
</ul>
<p>So for example, there are 900 5-digit decimal palindromes, 9,000 8-digit decimal palindromes, 1,098 decimal palindromes of 5 digits or less, and 19,998 decimal palindromes of 8 digits or less.</p>
<p>In this article, I will derive similar formulas to count binary and hexadecimal number palindromes.</p>
<p><span id="more-264"></span></p>
<h2>Binary Palindromes</h2>
<p>Number palindromes have a simple structure: n-digit palindromes are related to n-1 digit palindromes. You can think of generating palindromes as an iterative process, where you start with single-digit palindromes and work your way through palindromes of an increasing number of digits. Here&#8217;s an example of the process, using binary numbers:</p>
<ol>
<li>From the only (nonzero) one-digit palindrome, 1, make the two-digit palindrome 11.</li>
<li>From 11, make the two three-digit palindromes 101 and 111.</li>
<li>From 101, make 1001; from 111, make 1111.</li>
<li>From 1001, make 10001 and 10101; from 1111, make 11011 and 11111.</li>
<li>From 10001, make 100001; from 10101, make 101101; from 11011, make 110011; from 11111 make 111111.</li>
<li>&#8230;</li>
</ol>
<p>From each odd-length palindrome, you can generate <em>one</em> even-length palindrome; you have to replicate the middle digit in order to keep it palindromic. From each even-length palindrome, you can generate <em>two</em> odd-length palindromes, by inserting a 0 or 1 in the middle.</p>
<p>This table illustrates the structure better &#8212; it shows all 1 through 8 digit binary palindromes:</p>
<table class="center" border="1" summary="All 1 through 8 digit binary palindromes">
<caption><strong>1 to 8-Digit Binary Palindromes (not counting 0)</strong></caption>
<tbody>
<tr>
<th class="center">Num Digits (n)</th>
<th class="center">Binary Palindromes</th>
<th class="center">Num n-digit Palindromes</th>
<th class="center">Total Palindromes</th>
</tr>
<tr>
<td class="right">1</td>
<td class="right">
1
</td>
<td class="center">1</td>
<td class="center">1</td>
</tr>
<tr>
<td class="right">2</td>
<td class="right">
<span class="highlight_theme_blue ">1</span>1
</td>
<td class="center">1</td>
<td class="center">2</td>
</tr>
<tr>
<td class="right">3</td>
<td class="right">
1<span class="highlight_theme_blue ">0</span>1<br />
1<span class="highlight_theme_blue ">1</span>1
</td>
<td class="center">2</td>
<td class="center">4</td>
</tr>
<tr>
<td class="right">4</td>
<td class="right">
1<span class="highlight_theme_blue ">0</span>01<br />
1<span class="highlight_theme_blue ">1</span>11
</td>
<td class="center">2</td>
<td class="center">6</td>
</tr>
<tr>
<td class="right">5</td>
<td class="right">
10<span class="highlight_theme_blue ">0</span>01<br />
10<span class="highlight_theme_blue ">1</span>01<br />
11<span class="highlight_theme_blue ">0</span>11<br />
11<span class="highlight_theme_blue ">1</span>11
</td>
<td class="center">4</td>
<td class="center">10</td>
</tr>
<tr>
<td class="right">6</td>
<td class="right">
10<span class="highlight_theme_blue ">0</span>001<br />
10<span class="highlight_theme_blue ">1</span>101<br />
11<span class="highlight_theme_blue ">0</span>011<br />
11<span class="highlight_theme_blue ">1</span>111
</td>
<td class="center">4</td>
<td class="center">14</td>
</tr>
<tr>
<td class="right">7</td>
<td class="right">
100<span class="highlight_theme_blue ">0</span>001<br />
100<span class="highlight_theme_blue ">1</span>001<br />
101<span class="highlight_theme_blue ">0</span>101<br />
101<span class="highlight_theme_blue ">1</span>101<br />
110<span class="highlight_theme_blue ">0</span>011<br />
110<span class="highlight_theme_blue ">1</span>011<br />
111<span class="highlight_theme_blue ">0</span>111<br />
111<span class="highlight_theme_blue ">1</span>111
</td>
<td class="center">8</td>
<td class="center">22</td>
</tr>
<tr>
<td class="right">8</td>
<td class="right">
100<span class="highlight_theme_blue ">0</span>0001<br />
100<span class="highlight_theme_blue ">1</span>1001<br />
101<span class="highlight_theme_blue ">0</span>0101<br />
101<span class="highlight_theme_blue ">1</span>1101<br />
110<span class="highlight_theme_blue ">0</span>0011<br />
110<span class="highlight_theme_blue ">1</span>1011<br />
111<span class="highlight_theme_blue ">0</span>0111<br />
111<span class="highlight_theme_blue ">1</span>1111
</td>
<td class="center">8</td>
<td class="center">30</td>
</tr>
</tbody>
</table>
<h3>Counting Binary Palindromes</h3>
<p>As typical with things binary, there is a pattern in the palindromes involving powers of two. In this case, it&#8217;s easy to see why: each set of odd-length palindromes is double the size of the preceding set of even-length palindromes, and each set of even-length palindromes is the same size as the preceding set of odd-length palindromes. These two formulas capture this relationship, giving the number of n-digit binary palindromes:</p>
<ul>
<li>When n is even: 2<sup>n/2-1</sup></li>
<li>When n is odd: 2<sup>(n+1)/2-1</sup></li>
</ul>
<p>For n = 1 to 8, you can verify these formulas match the counts in the table: 1, 1, 2, 2, 4, 4, 8, 8.</p>
<p>(The formula can be stated in one expression as <img class='align_bottom' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/f370eed2d453b72bdcf3df9cae6de931.png' alt='\mbox{\footnotesize{\displaystyle{2^{\lfloor (n-1)/2 \rfloor}}}}'/>, which is the base 2 version of <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/1a863f9f0683a328814a9eb1667012e8.png' alt='\mbox{\footnotesize{\displaystyle{(b-1) \cdot b^{\lfloor (n-1)/2 \rfloor}}}}'/>, <a title="Sloane's &ldquo;Number of palindromes of length n&rdquo;" href="http://www.research.att.com/~njas/sequences/A050683">the formula stated here</a>. I rewrote it into two parts to remove the slightly cumbersome &#8220;floor&#8221; function, and to be consistent with the formulas below.)</p>
<h4>Number of Binary Palindromes of n Digits or Less</h4>
<p>You can further derive a formula for the total number of palindromes of n digits or less, by adding the terms 1, 1, 2, 2, 4, 4, 8, 8, &#8230; . Recognizing this sequence as two interleaved sequences of 1, 2, 4, 8, &#8230; &#8212; that is, sequences of nonnegative powers of two &#8212; the derivation of the formula is straightforward. </p>
<p>Here&#8217;s the formula to express the sum of the first m nonnegative powers of two:</p>
<p class="center"><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/a7caf098e64f4c7cefae4ba5d99311a9.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{m-1}2^i}}}'/> = 2<sup>m</sup> &#8211; 1</p>
<p>We can use this expression for each of the interleaved series, and then add them together. There are two cases:</p>
<ul>
<li><strong>The number of digits n is even</strong>
<p>The sum consists of two identical series of n/2 terms each:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/759a5a69c86e49a3f5a72accf5d77b49.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n}{2}-1}2^i}}}'/> = 2<sup>n/2</sup> &#8211; 1.</p>
<p>Added together, they are <strong>2(2<sup>n/2</sup> &#8211; 1)</strong>.</p>
</li>
<li><strong>The number of digits n is odd</strong>
<p>The sum consists of two identical series of (n-1)/2 terms each, plus an extra term. Each series looks like this:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/e1b7237ca73f62e78f9e3fb5b0c376f0.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{\frac{n-1}{2}-1}2^i}}}'/> = 2<sup>(n-1)/2</sup> &#8211; 1.</p>
<p>Added together, they are 2(2<sup>(n-1)/2</sup> &#8211; 1).</p>
<p>The extra term is 2<sup>(n-1)/2</sup>, what would have been the next term in one of the series. Adding this to the two series we get 2(2<sup>(n-1)/2</sup> &#8211; 1) + 2<sup>(n-1)/2</sup>, which simplifies to <strong>3&middot;2<sup>(n-1)/2</sup> &#8211; 2</strong>.</p>
</li>
</ul>
<p>For n = 1 to 8, you can verify these formulas match the total counts in the table: 1, 2, 4, 6, 10, 14, 22, 30.</p>
<h2>Hexadecimal Palindromes</h2>
<p>Using a similar analysis, here are the two formulas to count n-digit hexadecimal palindromes:</p>
<ul>
<li>When n is even: 15&middot;16<sup>n/2-1</sup></li>
<li>When n is odd: 15&middot;16<sup>(n+1)/2-1</sup></li>
</ul>
<p>For n = 1 to 8, the value of these formulas is 15, 15, 240, 240, 3840, 3840, 61440, 61440.</p>
<p>(The formula can also be stated in one expression as <img class='align_bottom' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/193348f2359821dbd724ab2115fa7f61.png' alt='\mbox{\footnotesize{\displaystyle{15 \cdot 16^{\lfloor (n-1)/2 \rfloor}}}}'/> .)</p>
<p>To count the number of hexadecimal palindromes of n digits or less, we can add series just as we did for binary palindromes. If you factor out 15 from each term in the sequence above, you&#8217;re left with this sequence: 1, 1, 16, 16, 256, 256, 4096, 4096. Those are interleaved sequences of nonnegative powers of sixteen.</p>
<p>Here’s the formula to express the sum of the first m nonnegative powers of sixteen:</p>
<p class="center"><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/772c116c9d6e1a9e1691e83f9a6b298a.png' alt='\mbox{\footnotesize{\displaystyle{\sum_{i=0}^{m-1}16^i}}}'/> = (16<sup>m</sup> &#8211; 1)/15</p>
<p>We can use this expression for each of the interleaved sequences, and then add them together. There are two cases:</p>
<ul>
<li><strong>The number of digits n is even</strong>
<p>The sum consists of two identical series of n/2 terms each:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/d518e16c3809c2eb4d4b146e1458ef8b.png' alt='\mbox{\footnotesize{\displaystyle{15\sum_{i=0}^{\frac{n}{2}-1}16^i}}}'/> = 16<sup>n/2</sup> &#8211; 1.</p>
<p>Added together, they are <strong>2(16<sup>n/2</sup> &#8211; 1)</strong>.</p>
</li>
<li><strong>The number of digits n is odd</strong>
<p>The sum consists of two identical series of (n-1)/2 terms each, plus an extra term. Each series looks like this:</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/counting-binary-and-hexadecimal-palindromes/e27fce5dc1e312e63e7db8afe486e621.png' alt='\mbox{\footnotesize{\displaystyle{15\sum_{i=0}^{\frac{n-1}{2}-1}16^i}}}'/> = 16<sup>(n-1)/2</sup> &#8211; 1.</p>
<p>Added together, they are 2(16<sup>(n-1)/2</sup> &#8211; 1).</p>
<p>The extra term is 15&middot;16<sup>(n-1)/2</sup>, what would have been the next term in one of the series. Adding this to the two series we get 2(16<sup>(n-1)/2</sup> &#8211; 1) + 15&middot;16<sup>(n-1)/2</sup>, which simplifies to <strong>17&middot;16<sup>(n-1)/2</sup> &#8211; 2</strong>.</p>
</li>
</ul>
<p>For n = 1 to 8, these formulas return these values: 15, 30, 270, 510, 4350, 8190, 69630, 131070.</p>
<h2>Trying These Formulas in PARI/GP</h2>
<p>In this section, I&#8217;ll demonstrate the formulas for counting binary palindromes, using <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a>.</p>
<p>Print the number of 1-digit to 20-digit binary palindromes, using the &#8220;floor&#8221; form of the expression:</p>
<pre>
? <strong>for (i=1,20,print(2^(floor((i-1)/2))))</strong>
1
1
2
2
4
4
8
8
16
16
32
32
64
64
128
128
256
256
512
512
</pre>
<p class="break">Print the number of 1-digit to 20-digit binary palindromes, using the even and odd case palindrome counting expressions:</p>
<pre>
? <strong>for(i=1,20,if(i%2==1,print(2^((i+1)/2-1)),print(2^(i/2-1))))</strong>
1
1
2
2
4
4
8
8
16
16
32
32
64
64
128
128
256
256
512
512
</pre>
<p class="break">Print the total number of binary palindromes of n digits or less, for n = 1 to 20:</p>
<pre>
? <strong>for(i=1,20,if(i%2==1,print(3*2^((i-1)/2)-2),print(2*(2^(i/2)-1))))</strong>
1
2
4
6
10
14
22
30
46
62
94
126
190
254
382
510
766
1022
1534
2046
</pre>
<h2>Summary</h2>
<p>Here is a summary of the binary palindrome counting formulas:</p>
<table class="center" border="1" summary="Summary of binary palindrome counting formulas">
<caption><strong>Nonzero Binary Palindrome Counting Formulas</strong></caption>
<tbody>
<tr>
<th class="center">Num Digits</th>
<th class="center">Even n</th>
<th class="center">Odd n</th>
</tr>
<tr>
<td class="left">n</td>
<td class="center">2<sup>n/2-1</sup></td>
<td class="center">2<sup>(n+1)/2-1</sup></td>
</tr>
<tr>
<td class="left">n or less</td>
<td class="center">2(2<sup>n/2</sup> &#8211; 1)</td>
<td class="center">3&middot;2<sup>(n-1)/2</sup> &#8211; 2</td>
</tr>
</tbody>
</table>
<p class="break">Here is a summary of the hexadecimal palindrome counting formulas:</p>
<table class="center" border="1" summary="Summary of hexadecimal palindrome counting formulas">
<caption><strong>Nonzero Hexadecimal Palindrome Counting Formulas</strong></caption>
<tbody>
<tr>
<th class="center">Num Digits</th>
<th class="center">Even n</th>
<th class="center">Odd n</th>
</tr>
<tr>
<td class="left">n</td>
<td class="center">15&middot;16<sup>n/2-1</sup></td>
<td class="center">15&middot;16<sup>(n+1)/2-1</sup></td>
</tr>
<tr>
<td class="left">n or less</td>
<td class="center">2(16<sup>n/2</sup> &#8211; 1)</td>
<td class="center">17&middot;16<sup>(n-1)/2</sup> &#8211; 2</td>
</tr>
</tbody>
</table>
<h2>Discussion</h2>
<p>These formulas can be extended to any base, like octal. (You can see the pattern they follow, although that in itself is not a proof they hold for all bases.)</p>
<p>In my article <a href="http://www.exploringbinary.com/finding-numbers-that-are-palindromic-in-multiple-bases/" title="Read Rick Regan's Article &ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;">&ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;</a> I showed how to generate palindromes by iterating through a counter. The analysis of palindromes in this article suggests another algorithm: generating n-digit palindromes from n-1 digit palindromes. (It&#8217;s probably not a better way though, at least not for large n, because the required storage space grows exponentially.)</p>
<h3>What About Zero?</h3>
<p><a title="Wikipedia article defining palindromic number" href="http://en.wikipedia.org/wiki/Palindromic_number">The number 0 is defined to be a palindrome</a>, although you could make an argument against it. No palindrome can start with zero, yet 0 is a palindrome. In any case, it is not counted in the formulas above, simply because it makes the derivations easier. To count zero, add one to each formula.</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/">Counting Binary and Hexadecimal Palindromes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/counting-binary-and-hexadecimal-palindromes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cycle Length of Powers of Five Mod Powers of Ten</title>
		<link>http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/</link>
		<comments>http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 17:37:22 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Powers of two]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Modular arithmetic]]></category>
		<category><![CDATA[Proof]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=263</guid>
		<description><![CDATA[In my article &#8220;Patterns in the Last Digits of the Positive Powers of Five&#8221; I noted that the positive powers of five modulo 10m cycle with period 2m-2, m &#8805; 2, starting at 5m. In this article, I&#8217;ll present my proof, which has two parts:

Part 1 shows that the powers of five mod 2m cycle [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/">Cycle Length of Powers of Five Mod Powers of Ten</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In my article <a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Five&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">&ldquo;Patterns in the Last Digits of the Positive Powers of Five&rdquo;</a> I noted that the positive powers of five modulo 10<sup>m</sup> cycle with period 2<sup>m-2</sup>, m &ge; 2, starting at 5<sup>m</sup>. In this article, I&#8217;ll present my proof, which has two parts:</p>
<ul>
<li>Part 1 shows that the powers of five <strong>mod 2<sup>m</sup></strong> cycle with period 2<sup>m-2</sup>,  m &ge; 2, <strong>starting at 5<sup>0</sup></strong>.</li>
<li>Part 2 shows that the powers of five <strong>mod 10<sup>m</sup></strong> cycle with the same period as the powers of five mod 2<sup>m</sup>, <strong>starting at 5<sup>m</sup></strong>.</li>
</ul>
<p>The highlight of my proof is in part 1, where I derive a formula to show that the period, or <a title="Wikipedia article on multiplicative order" href="http://en.wikipedia.org/wiki/Multiplicative_order">order</a>, of 5 mod 2<sup>m</sup> is 2<sup>m-2</sup>. While it is in general not possible to derive a formula for the order of a number, I&#8217;ll show it <em>is</em> possible for the powers of five mod 2<sup>m</sup> &#8212; <strong>due to a hidden, binary structure I&#8217;ve uncovered</strong>.</p>
<p><span id="more-263"></span>To understand the proof, you&#8217;ll need to know some algebra and the basics of modular arithmetic.</p>
<h2>Part 1: Period mod 2<sup>m</sup> is 2<sup>m-2</sup></h2>
<p>Part 1 shows that the order of 5 mod 2<sup>m</sup> is 2<sup>m-2</sup>. I break it into three steps, showing that</p>
<ul>
<li><strong>The order of 5 mod 2<sup>m</sup> is a power of two</strong>. This constrains the values for the order so that only expressions of the form 5<sup>2<sup>n</sup></sup> &#8211; 1 need be considered.</li>
<li><strong>5<sup>2<sup>n</sup></sup> &#8211; 1 can be factored to isolate its factors of two</strong>. This is my key insight, made possible by recursive factoring of differences of squares.</li>
<li><strong>5<sup>2<sup>n</sup></sup> &#8211; 1 is divisible by 2<sup>n+2</sup></strong>. This shows that the order of 5 mod 2<sup>n+2</sup> is 2<sup>n</sup>, or equivalently, that the order of 5 mod 2<sup>m</sup> is 2<sup>m-2</sup>.</li>
</ul>
<p>In addition, I show that the powers of five mod 2<sup>m</sup> cycle starting at 5<sup>0</sup>; this is important because it will show that the cycle mod 10<sup>m</sup> always starts after the cycle mod 2<sup>m</sup>.</p>
<h3>The Order of 5 mod 2<sup>m</sup> is a Power of Two</h3>
<p>The order of a mod b is related to <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/35e45b0c464ebbf22f8db5e5404c1af1.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (b)}}}'/>, so let&#8217;s start there. <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/35e45b0c464ebbf22f8db5e5404c1af1.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (b)}}}'/> is Euler&#8217;s phi function, the number of positive integers less than or equal to b that are relatively prime to b (for b a power of two, this is just the count of the odd numbers less than b). We want to compute <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/3904e07c4fb197326b638bb5badf1cbc.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (2^m)}}}'/>, which we can do with this <a title="Wikipedia article that shows the formula for Euler's Phi Function" href="http://en.wikipedia.org/wiki/Euler%27s_totient_function#Computing_Euler.27s_function">simple formula</a>: <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/b00cb65570b30fb2762cf72221133b28.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (p^m) = (p \,$-$ 1) \cdot p^{m\,\textnormal{-}1}}}}'/>. For p=2, this gives <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/3904e07c4fb197326b638bb5badf1cbc.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (2^m)}}}'/> = 2<sup>m-1</sup>. </p>
<p>The <a title="Wikipedia article on multiplicative order" href="http://en.wikipedia.org/wiki/Multiplicative_order">order of <em>a</em> mod <em>b</em> must divide <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/35e45b0c464ebbf22f8db5e5404c1af1.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (b)}}}'/></a>, so the order of the powers of five mod 2<sup>m</sup> must divide <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/3904e07c4fb197326b638bb5badf1cbc.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (2^m)}}}'/>, or 2<sup>m-1</sup>. In other words, the order must be a power of two &#8212; a power of two less than or equal to 2<sup>m-1</sup> to be specific &#8212; because the divisors of a positive power of two are all the positive powers of two less than or equal to it. </p>
<p>Let 2<sup>e</sup> represent any of the candidates for the order of 5 mod 2<sup>m</sup>: 1, 2, 4, 8, &#8230; , 2<sup>m-2</sup>, 2<sup>m-1</sup>. By definition, if the order is 2<sup>e</sup>, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/57178b9f53f3cfc6e4b6eefd30d0ee32.png' alt='\mbox{\footnotesize{\displaystyle{5^{2^e} \equiv 1 \pmod{2^m}}}}'/>, or equivalently, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/2b7d384155f2d5c268e73885167820f0.png' alt='\mbox{\footnotesize{\displaystyle{5^{2^e} \,$-$ 1 \equiv 0 \pmod{2^m}}}}'/>. That is, 5<sup>2<sup>e</sup></sup> &#8211; 1 is divisible by 2<sup>m</sup>. Now all we have to do is figure out which e makes this true.</p>
<h3>5<sup>2<sup>n</sup></sup> &#8211; 1 Can Be Factored to Isolate its Factors of Two</h3>
<p>You can factor 5<sup>2<sup>n</sup></sup> &#8211; 1 into (5<sup>2<sup>n/2</sup></sup> + 1)(5<sup>2<sup>n/2</sup></sup> &#8211; 1), since it is a difference of two squares. You can factor 5<sup>2<sup>n/2</sup></sup> &#8211; 1 similarly, into (5<sup>2<sup>n/4</sup></sup> + 1)(5<sup>2<sup>n/4</sup></sup> &#8211; 1). You can do this n times, since log<sub>2</sub>(2<sup>n</sup>) = n. Here&#8217;s an example:</p>
<ol>
<li>5<sup>16</sup> &#8211; 1 = (5<sup>8</sup> + 1)(5<sup>8</sup> &#8211; 1)</li>
<li>5<sup>8</sup> &#8211; 1 = (5<sup>4</sup> + 1)(5<sup>4</sup> &#8211; 1)</li>
<li>5<sup>4</sup> &#8211; 1  = (5<sup>2</sup> + 1)(5<sup>2</sup> &#8211; 1)</li>
<li>5<sup>2</sup> &#8211; 1  = (5<sup>1</sup> + 1)(5<sup>1</sup> &#8211; 1)</li>
<li>5<sup>1</sup> &#8211; 1  = 4</li>
</ol>
<div class="wp-caption aligncenter" style="width: 330px"><img src="http://www.exploringbinary.com/wp-content/uploads/PO5Factors.jpg" alt="Recursive Factoring of Differences of Squares" width="320" height="252"/><p class="wp-caption-text">Recursive Factoring of Differences of Squares</p></div>
<p>Combining these recursively you get 5<sup>16</sup> &#8211; 1 = 4(5<sup>1</sup> + 1)(5<sup>2</sup> + 1)(5<sup>4</sup> + 1)(5<sup>8</sup> + 1).</p>
<p>This generalizes to 5<sup>2<sup>n</sup></sup> &#8211; 1 = 4(5<sup>1</sup> + 1)(5<sup>2</sup> + 1)(5<sup>4</sup> + 1)&middot;&middot;&middot;(5<sup>2<sup>n/2</sup></sup> + 1).</p>
<p>There is a factor of two in each 5<sup>2<sup>f</sup></sup> + 1, as I&#8217;ll show in the next section.</p>
<h3>5<sup>2<sup>n</sup></sup> &#8211; 1 is divisible by 2<sup>n+2</sup></h3>
<p>The factor of four in the factored expression shows there are at least two factors of two in 5<sup>2<sup>n</sup></sup> &#8211; 1. There is also exactly one factor of two in each of the factors 5<sup>2<sup>f</sup></sup> + 1. I&#8217;ll show it in two steps:</p>
<ul>
<li><strong>5<sup>2<sup>f</sup></sup> + 1 is divisible by 2</strong>.
<p><a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Five&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">Every positive power of five ends in 5</a>, so it is odd. This means 5<sup>2<sup>f</sup></sup> + 1 is even, meaning it&#8217;s divisible by 2.</p>
</li>
<li><strong>5<sup>2<sup>f</sup></sup> + 1 is not divisible by any power of two greater than 2</strong>.
<p>To show this, it suffices to show that 5<sup>2<sup>f</sup></sup> + 1 is not divisible by 4. If it&#8217;s not divisible by 4, it&#8217;s not divisible by any higher power of two (because the divisors of a positive power of two are all positive powers of two less than or equal to it). So if 2<sup>2</sup> is not a factor, 2<sup>s</sup>, s &gt; 1, is not a factor.</p>
<p>Let&#8217;s show 5<sup>2<sup>f</sup></sup> + 1 is not divisible by 4 by breaking it in two cases:</p>
<ul>
<li><strong>5<sup>2<sup>0</sup></sup> + 1</strong>. 5<sup>1</sup> + 1 = 6, which obviously is not divisible by 4.</li>
<li><strong>5<sup>2<sup>f</sup></sup> + 1, f &ge; 1</strong>. <a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Five&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">5<sup>2<sup>f</sup></sup> ends in 25</a>, so 5<sup>2<sup>f</sup></sup> + 1 ends in 26. <a title="Wikipedia article on base ten integer divisibility rules" href="http://en.wikipedia.org/wiki/Divisibility_rule">An integer is divisible by 4 if and only if its last two digits are divisible by 4</a>. Since 26 is not divisible by 4, neither is 5<sup>2<sup>f</sup></sup> + 1.</li>
</ul>
</li>
</ul>
<p>So the example expression 4(5<sup>1</sup> + 1)(5<sup>2</sup> + 1)(5<sup>4</sup> + 1)(5<sup>8</sup> + 1) has a factor of 2<sup>6</sup>: 2<sup>2</sup> times four factors of 2. </p>
<p>This generalizes to show that 5<sup>2<sup>n</sup></sup> &#8211; 1 is divisible by 2<sup>n+2</sup>. Recasting this from the perspective of the power of five to the modulus, let&#8217;s substitute m-2 for n. This gives the equivalent statement 5<sup>2<sup>m-2</sup></sup> &#8211; 1 is divisible by 2<sup>m</sup>. Just to be explicit, this implies that no 5<sup>2<sup>e</sup></sup>, e &lt; 2<sup>m-2</sup>, is divisible by 2<sup>m</sup>. So, the candidate power of two we were seeking is <strong>2<sup>m-2</sup>, the order of 5 mod 2<sup>m</sup></strong>.</p>
<h3>The Powers of Five Mod 2<sup>m</sup> Cycle Starting at 5<sup>0</sup></h3>
<p>Let the period p = 2<sup>m-2</sup>.  As we proved above, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/06ab2fb22597579dee369e9c27c50e83.png' alt='\mbox{\footnotesize{\displaystyle{5^p \equiv 1 \pmod{2^m}}}}'/>. We also know, trivially, that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/98c9e5a1783d8a98ca9f7e1214ed813a.png' alt='\mbox{\footnotesize{\displaystyle{5^0 \equiv 1 \pmod{2^m}}}}'/>. The difference between the exponents p and 0 is p, showing a full cycle occurs starting at 5<sup>0</sup>.</p>
<h2>Part 2: Period mod 10<sup>m</sup> is 2<sup>m-2</sup></h2>
<p>Part 2 shows, using the definition of <a title="Wikipedia article on modular arithmetic" href="http://en.wikipedia.org/wiki/Modular_arithmetic">modular arithmetic</a>, the <a title="Read Rick Regan's Article &ldquo;The Laws of Exponents&rdquo;" href="http://www.exploringbinary.com/the-laws-of-exponents/">laws of exponents</a>, and simple algebra, that the powers of five mod 10<sup>m</sup> have the same period as the powers of five mod 2<sup>m</sup>. It&#8217;s broken into two halves, showing that</p>
<ul>
<li>The period mod 2<sup>m</sup> is a cycle mod 10<sup>m</sup>.</li>
<li>The cycle mod 10<sup>m</sup> is the period mod 2<sup>m</sup>.</li>
</ul>
<p>The first half only shows that the period mod 2<sup>m</sup> is <em>a multiple</em> of the period mod 10<sup>m</sup>. The second half shows that the period mod 10<sup>m</sup> is in fact the same as the period mod 2<sup>m</sup>.</p>
<p>I&#8217;ll use the variable <em>p</em> for the period mod 2<sup>m</sup>, as above, to simplify the notation in the proof that follows.</p>
<h4>The period mod 2<sup>m</sup> is a cycle mod 10<sup>m</sup></h4>
<p>For i &ge; 0, what we&#8217;ve shown is that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/6fd4af1d057b92ccd8f19b4aa441e8fb.png' alt='\mbox{\footnotesize{\displaystyle{5^{i+p} \equiv 5^i \pmod{2^m}}}}'/>. That is, powers of five with exponents a period apart are congruent mod 2<sup>m</sup>.</p>
<p>Congruence mod 2<sup>m</sup> means that 5<sup>i+p</sup> &#8211; 5<sup>i</sup> is a multiple of 2<sup>m</sup>; that is, 5<sup>i+p</sup> &#8211; 5<sup>i</sup> = n&middot;2<sup>m</sup>, for some nonnegative integer n.</p>
<p>Factoring out 5<sup>i</sup> we get 5<sup>i</sup>(5<sup>p</sup> &#8211; 1) = n&middot;2<sup>m</sup>. <strong>When i&ge;m, n is a multiple of 5<sup>m</sup></strong>, so n = n&#8217;&middot;5<sup>m</sup>.</p>
<p>Substituting n&#8217;&middot;5<sup>m</sup> for n we get 5<sup>i</sup>(5<sup>p</sup> &#8211; 1) = n&#8217;&middot;5<sup>m</sup>2<sup>m</sup> = n&#8217;&middot;10<sup>m</sup>.</p>
<p>Reversing the process above, we can show congruence mod 10<sup>m</sup>: </p>
<p>5<sup>i</sup>(5<sup>p</sup> &#8211; 1) = 5<sup>i+p</sup> &#8211; 5<sup>i</sup> = n&#8217;&middot;10<sup>m</sup>.</p>
<p>This shows that for i&ge;m, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/4e39319c968be2bc9462ab6feae2ca25.png' alt='\mbox{\footnotesize{\displaystyle{5^{i+p} \equiv 5^i \pmod{10^m}}}}'/>, so p is a cycle mod 10<sup>m</sup>.</p>
<h4>The cycle mod 10<sup>m</sup> is the period mod 2<sup>m</sup></h4>
<p>We&#8217;ve shown that p is a cycle mod 10<sup>m</sup>, but not that p is the period mod 10<sup>m</sup>. In other words, it&#8217;s possible the period mod 10<sup>m</sup> is less than p &#8212; we have to show it isn&#8217;t.</p>
<p>For i &ge; m, we&#8217;ve shown that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/4e39319c968be2bc9462ab6feae2ca25.png' alt='\mbox{\footnotesize{\displaystyle{5^{i+p} \equiv 5^i \pmod{10^m}}}}'/>. Using the argument above, congruence mod 10<sup>m</sup> means that:</p>
<p>5<sup>i+p</sup> &#8211; 5<sup>i</sup> = n&middot;10<sup>m</sup> = n&middot;5<sup>m</sup>2<sup>m</sup> = (n&middot;5<sup>m</sup>)2<sup>m</sup>.</p>
<p>This shows that for i&ge;m, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-five-mod-powers-of-ten/6fd4af1d057b92ccd8f19b4aa441e8fb.png' alt='\mbox{\footnotesize{\displaystyle{5^{i+p} \equiv 5^i \pmod{2^m}}}}'/>, so p is the period mod 2<sup>m</sup> <em>and</em> mod 10<sup>m</sup>.</p>
<h2>Empirical Evidence</h2>
<p>This section is not part of the proof, but it shows some <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a> calculations that support it.</p>
<p>Compute the order of 5 mod 2 through 5 mod 2<sup>16</sup> (you can see they&#8217;re powers of two):</p>
<pre>
? <strong>for(m=1,16,print(znorder(Mod(5,2^m))))</strong>
1
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
</pre>
<p class="break">Show the factors in 5<sup>16</sup> &#8211; 1:</p>
<pre>
? <strong>factor(5^16-1)</strong>
%1 =
<strong>[2 6]</strong>
[3 1]
[13 1]
[17 1]
[313 1]
[11489 1]

? <strong>factor(4*(5^1+1)*(5^2+1)*(5^4+1)*(5^8+1))</strong>
%2 =
<strong>[2 6]</strong>
[3 1]
[13 1]
[17 1]
[313 1]
[11489 1]

? <strong>factor((5^1+1))</strong>
%3 =
<strong>[2 1]</strong>
[3 1]

? <strong>factor((5^2+1))</strong>
%4 =
<strong>[2 1]</strong>
[13 1]

? <strong>factor((5^4+1))</strong>
%5 =
<strong>[2 1]</strong>
[313 1]

? <strong>factor((5^8+1))</strong>
%6 =
<strong>[2 1]</strong>
[17 1]
[11489 1]
</pre>
<p class="break">Show the factors of two in 5<sup>n</sup> &#8211; 1, n = 1 to 32 (you can see how the <a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Five&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">nested cycles</a> correspond to the powers of two):</p>
<pre>
? <strong>for(i=1,32,print(&quot;5^&quot;,i,&quot;-1 has factor 2^&quot;,factor(5^i-1)[1,2]))</strong>

<strong>5^1-1 has factor 2^2</strong>
<strong>5^2-1 has factor 2^3</strong>
5^3-1 has factor 2^2
<strong>5^4-1 has factor 2^4</strong>
5^5-1 has factor 2^2
5^6-1 has factor 2^3
5^7-1 has factor 2^2
<strong>5^8-1 has factor 2^5</strong>
5^9-1 has factor 2^2
5^10-1 has factor 2^3
5^11-1 has factor 2^2
5^12-1 has factor 2^4
5^13-1 has factor 2^2
5^14-1 has factor 2^3
5^15-1 has factor 2^2
<strong>5^16-1 has factor 2^6</strong>
5^17-1 has factor 2^2
5^18-1 has factor 2^3
5^19-1 has factor 2^2
5^20-1 has factor 2^4
5^21-1 has factor 2^2
5^22-1 has factor 2^3
5^23-1 has factor 2^2
5^24-1 has factor 2^5
5^25-1 has factor 2^2
5^26-1 has factor 2^3
5^27-1 has factor 2^2
5^28-1 has factor 2^4
5^29-1 has factor 2^2
5^30-1 has factor 2^3
5^31-1 has factor 2^2
<strong>5^32-1 has factor 2^7</strong>
</pre>
<p>(The pattern of the powers of two is reminiscent of the <a title="Read Rick Regan's Article &ldquo;What a Binary Counter Looks and Sounds Like&rdquo;" href="http://www.exploringbinary.com/what-a-binary-counter-looks-and-sounds-like/">representation of powers of two in a binary counter</a>.)</p>
<h2>Discussion</h2>
<p>Part 2 of the proof is identical to part 2 of the proof in my article <a title="Read Rick Regan's Article &ldquo;Cycle Length of Powers of Two Mod Powers of Ten&rdquo;" href="http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/">&ldquo;Cycle Length of Powers of Two Mod Powers of Ten&rdquo;</a>, <strong>except that 5 and 2 are interchanged</strong>. Part 1 is quite different though. I could not use the same approach, since powers of two beyond four don&#8217;t have primitive roots.</p>
<p>Coming up with an alternate approach took some time. I had the elements of it early on: knowing that the order must be a power of two, knowing how to factor differences of squares, and knowing that each factor was even. But I was missing the piece that let me show each factor had <em>exactly</em> one factor of two &#8212; until I realized I could apply the divisibility by four test.</p>
<p>The binary structure of powers of a number computed <em>mod powers of two</em> make this approach possible (it should work for powers of any odd number, although it may be more difficult to show the factors of two).</p>
<h2>Summary</h2>
<p>To prove the period formula for the positive powers of five mod powers of ten, I first proved it for mod powers of two. I showed that the period must be a power of two, and derived an expression that showed <em>which</em> power of two. I then showed that the period mod powers of two and mod powers of ten must be the same, after a certain starting point.</p>
<p>I&#8217;ve previously shown a <a title="Read Rick Regan's Article &ldquo;Seeing Powers of Five in Powers of Two and Vice Versa&rdquo;" href="http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/">connection between the positive powers of five and negative powers of two</a>; now I&#8217;ve shown a connection between the positive powers of five and the <em>positive</em> powers of two.</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/">Cycle Length of Powers of Five Mod Powers of Ten</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Dates in 2010 and 2011</title>
		<link>http://www.exploringbinary.com/binary-dates-in-2010-and-2011/</link>
		<comments>http://www.exploringbinary.com/binary-dates-in-2010-and-2011/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 20:51:54 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary numbers]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=262</guid>
		<description><![CDATA[People have been tweeting about the upcoming dates that look like binary numbers. 10/10/10 seems to be a favorite, both because of its symmetry and because 101010 = 42 in decimal (you know, the answer to the ultimate question of life, the universe, and everything). Here are the nine dates in each year, interpreted as [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-dates-in-2010-and-2011/">Binary Dates in 2010 and 2011</a></p>
]]></description>
			<content:encoded><![CDATA[<p>People have been tweeting about the upcoming dates that look like binary numbers. 10/10/10 seems to be a favorite, both because of its symmetry and because 101010 = 42 in decimal (you know, the <a title="Wikipedia article on &ldquo;The Hitchhiker&rsquo;s Guide to the Galaxy&rdquo;" href="http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29">answer to the ultimate question of life, the universe, and everything</a>). Here are the nine dates in each year, interpreted as binary numbers, and with their decimal equivalents:</p>
<p><span id="more-262"></span></p>
<table class="center" border="1" summary="Binary Dates in the Year 2010 (month/day/year)">
<caption><strong>Binary Dates in 2010 (mm/dd/yy)</strong></caption>
<tbody>
<tr>
<th class="center">Date</th>
<th class="center">mm/dd/yy</th>
<th class="center">Binary</th>
<th class="center">Decimal</th>
</tr>
<tr>
<td rowspan="2" class="left">January 1st, 2010</td>
<td class="right">1/1/10</td>
<td class="right">1110</td>
<td class="center">14</td>
</tr>
<tr>
<td class="right">1/01/10</td>
<td class="right">10110</td>
<td class="center">22</td>
</tr>
<tr>
<td class="left">January 10th, 2010</td>
<td class="right">1/10/10</td>
<td class="right">11010</td>
<td class="center">26</td>
</tr>
<tr>
<td class="left">January 11th, 2010</td>
<td class="right">1/11/10</td>
<td class="right">11110</td>
<td class="center">30</td>
</tr>
<tr>
<td rowspan="2" class="left">October 1st, 2010</td>
<td class="right">10/1/10</td>
<td class="right">10110</td>
<td class="center">22</td>
</tr>
<tr>
<td class="right">10/01/10</td>
<td class="right">100110</td>
<td class="center">38</td>
</tr>
<tr>
<td class="left">October 10th, 2010</td>
<td class="right">10/10/10</td>
<td class="right">101010</td>
<td class="center">42</td>
</tr>
<tr>
<td class="left">October 11th, 2010</td>
<td class="right">10/11/10</td>
<td class="right">101110</td>
<td class="center">46</td>
</tr>
<tr>
<td rowspan="2" class="left">November 1st, 2010</td>
<td class="right">11/1/10</td>
<td class="right">11110</td>
<td class="center">30</td>
</tr>
<tr>
<td class="right">11/01/10</td>
<td class="right">110110</td>
<td class="center">54</td>
</tr>
<tr>
<td class="left">November 10th, 2010</td>
<td class="right">11/10/10</td>
<td class="right">111010</td>
<td class="center">58</td>
</tr>
<tr>
<td class="left">November 11th, 2010</td>
<td class="right">11/11/10</td>
<td class="right">111110</td>
<td class="center">62</td>
</tr>
</tbody>
</table>
<p class="break">Here they are in dd/mm/yy format:</p>
<table class="center" border="1" summary="Binary Dates in the Year 2010 (day/month/year)">
<caption><strong>Binary Dates in 2010 (dd/mm/yy)</strong></caption>
<tbody>
<tr>
<th class="center">Date</th>
<th class="center">dd/mm/yy</th>
<th class="center">Binary</th>
<th class="center">Decimal</th>
</tr>
<tr>
<td rowspan="2" class="left">January 1st, 2010</td>
<td class="right">1/1/10</td>
<td class="right">1110</td>
<td class="center">14</td>
</tr>
<tr>
<td class="right">1/01/10</td>
<td class="right">10110</td>
<td class="center">22</td>
</tr>
<tr>
<td rowspan="2" class="left">January 10th, 2010</td>
<td class="right">10/1/10</td>
<td class="right">10110</td>
<td class="center">22</td>
</tr>
<tr>
<td class="right">10/01/10</td>
<td class="right">100110</td>
<td class="center">38</td>
</tr>
<tr>
<td rowspan="2" class="left">January 11th, 2010</td>
<td class="right">11/1/10</td>
<td class="right">11110</td>
<td class="center">30</td>
</tr>
<tr>
<td class="right">11/01/10</td>
<td class="right">110110</td>
<td class="center">54</td>
</tr>
<tr>
<td class="left">October 1st, 2010</td>
<td class="right">1/10/10</td>
<td class="right">11010</td>
<td class="center">26</td>
</tr>
<tr>
<td class="left">October 10th, 2010</td>
<td class="right">10/10/10</td>
<td class="right">101010</td>
<td class="center">42</td>
</tr>
<tr>
<td class="left">October 11th, 2010</td>
<td class="right">11/10/10</td>
<td class="right">111010</td>
<td class="center">58</td>
</tr>
<tr>
<td class="left">November 1st, 2010</td>
<td class="right">1/11/10</td>
<td class="right">11110</td>
<td class="center">30</td>
</tr>
<tr>
<td class="left">November 10th, 2010</td>
<td class="right">10/11/10</td>
<td class="right">101110</td>
<td class="center">46</td>
</tr>
<tr>
<td class="left">November 11th, 2010</td>
<td class="right">11/11/10</td>
<td class="right">111110</td>
<td class="center">62</td>
</tr>
</tbody>
</table>
<h2>2011</h2>
<p>The same days also look like binary in 2011:</p>
<table class="center" border="1" summary="Binary Dates in the Year 2011 (month/day/year)">
<caption><strong>Binary Dates in 2011 (mm/dd/yy)</strong></caption>
<tbody>
<tr>
<th class="center">Date</th>
<th class="center">mm/dd/yy</th>
<th class="center">Binary</th>
<th class="center">Decimal</th>
</tr>
<tr>
<td rowspan="2" class="left">January 1st, 2011</td>
<td class="right">1/1/11</td>
<td class="right">1111</td>
<td class="center">15</td>
</tr>
<tr>
<td class="right">1/01/11</td>
<td class="right">10111</td>
<td class="center">23</td>
</tr>
<tr>
<td class="left">January 10th, 2011</td>
<td class="right">1/10/11</td>
<td class="right">11011</td>
<td class="center">27</td>
</tr>
<tr>
<td class="left">January 11th, 2011</td>
<td class="right">1/11/11</td>
<td class="right">11111</td>
<td class="center">31</td>
</tr>
<tr>
<td rowspan="2" class="left">October 1st, 2011</td>
<td class="right">10/1/11</td>
<td class="right">10111</td>
<td class="center">23</td>
</tr>
<tr>
<td class="right">10/01/11</td>
<td class="right">100111</td>
<td class="center">39</td>
</tr>
<tr>
<td class="left">October 10th, 2011</td>
<td class="right">10/10/11</td>
<td class="right">101011</td>
<td class="center">43</td>
</tr>
<tr>
<td class="left">October 11th, 2011</td>
<td class="right">10/11/11</td>
<td class="right">101111</td>
<td class="center">47</td>
</tr>
<tr>
<td rowspan="2" class="left">November 1st, 2011</td>
<td class="right">11/1/11</td>
<td class="right">11111</td>
<td class="center">31</td>
</tr>
<tr>
<td class="right">11/01/11</td>
<td class="right">110111</td>
<td class="center">55</td>
</tr>
<tr>
<td class="left">November 10th, 2011</td>
<td class="right">11/10/11</td>
<td class="right">111011</td>
<td class="center">59</td>
</tr>
<tr>
<td class="left">November 11th, 2011</td>
<td class="right">11/11/11</td>
<td class="right">111111</td>
<td class="center">63</td>
</tr>
</tbody>
</table>
<p class="break">Here they are in dd/mm/yy format:</p>
<table class="center" border="1" summary="Binary Dates in the Year 2011 (day/month/year)">
<caption><strong>Binary Dates in 2011 (dd/mm/yy)</strong></caption>
<tbody>
<tr>
<th class="center">Date</th>
<th class="center">dd/mm/yy</th>
<th class="center">Binary</th>
<th class="center">Decimal</th>
</tr>
<tr>
<td rowspan="2" class="left">January 1st, 2011</td>
<td class="right">1/1/11</td>
<td class="right">1111</td>
<td class="center">15</td>
</tr>
<tr>
<td class="right">1/01/11</td>
<td class="right">10111</td>
<td class="center">23</td>
</tr>
<tr>
<td rowspan="2" class="left">January 10th, 2011</td>
<td class="right">10/1/11</td>
<td class="right">10111</td>
<td class="center">23</td>
</tr>
<tr>
<td class="right">10/01/11</td>
<td class="right">100111</td>
<td class="center">39</td>
</tr>
<tr>
<td rowspan="2" class="left">January 11th, 2011</td>
<td class="right">11/1/11</td>
<td class="right">11111</td>
<td class="center">31</td>
</tr>
<tr>
<td class="right">11/01/11</td>
<td class="right">110111</td>
<td class="center">55</td>
</tr>
<tr>
<td class="left">October 1st, 2011</td>
<td class="right">1/10/11</td>
<td class="right">11011</td>
<td class="center">27</td>
</tr>
<tr>
<td class="left">October 10th, 2011</td>
<td class="right">10/10/11</td>
<td class="right">101011</td>
<td class="center">43</td>
</tr>
<tr>
<td class="left">October 11th, 2011</td>
<td class="right">11/10/11</td>
<td class="right">111011</td>
<td class="center">59</td>
</tr>
<tr>
<td class="left">November 1st, 2011</td>
<td class="right">1/11/11</td>
<td class="right">11111</td>
<td class="center">31</td>
</tr>
<tr>
<td class="left">November 10th, 2011</td>
<td class="right">10/11/11</td>
<td class="right">101111</td>
<td class="center">47</td>
</tr>
<tr>
<td class="left">November 11th, 2011</td>
<td class="right">11/11/11</td>
<td class="right">111111</td>
<td class="center">63</td>
</tr>
</tbody>
</table>
<p class="break">And in case you were wondering, 2010 = 11111011010 in binary, and 2011 = 11111011011 in binary.</p>
<p class="break">(If this blog existed in 2001 &#8212; if blogs existed in 2001 period &#8212; I would have made tables for those dates as well.)</p>
<h2>Palindromes</h2>
<p>Considering dates as numbers leads you to wonder which are <a href="http://www.exploringbinary.com/finding-numbers-that-are-palindromic-in-multiple-bases/" title="Read Rick Regan's Article &ldquo;Finding Numbers That Are Palindromic In Multiple Bases&rdquo;">number palindromes</a>. Numbers written with leading zeros are not palindromes, so numbers <em>ending</em> with zeros are not palindromes either. This means no dates in 2010 are palindromic. In 2011 however, there <em>are</em> palindromic dates. There are several consisting of all 1s, like 1/1/11, but those are not interesting. That leaves two palindromic dates made up of both 0s and 1s:</p>
<ul>
<li>January 10th, 2011, in mm/dd/yy format: 1/10/11.</li>
<li>October 1st, 2011, in dd/mm/yy format: 1/10/11.</li>
</ul>
<p>Both are the same binary number: 11011 (27 decimal).</p>
<h2>Binary Times</h2>
<p>People have also been <a href="http://twitter.com/vonec/status/7221144706" title="Tweet by user &ldquo;vonec&rdquo;">tweeting about binary <em>times</em></a>. There are binary times every day, but they&#8217;ll seem more significant on binary days. There are 12, not including seconds and duplicates for AM/PM: 1:00, 1:01, 1:10, 1:11, 10:00, 10:01, 10:10, 10:11, 11:00, 11:01, 11:10, 11:11. (I&#8217;ll leave it as an exercise to compute these as decimal values.)</p>
<p>Now having thrown time into the mix, we can consider some new palindromes. For example, 1/1/10 (mm/dd/yy) at 1:11 AM is 1110111. Have a toast for me!</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/binary-dates-in-2010-and-2011/">Binary Dates in 2010 and 2011</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/binary-dates-in-2010-and-2011/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Patterns in the Last Digits of the Positive Powers of Five</title>
		<link>http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/</link>
		<comments>http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 15:50:43 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Powers of two]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Modular arithmetic]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=259</guid>
		<description><![CDATA[The positive powers of five &#8212; 5, 25, 125, 625, 3125, 15625, &#8230; &#8212; have a compact, repeating pattern in their ending m digits, in the powers of five from 5m on. For example: starting with 5, their last digit is always 5; starting with 25, their last two digits are always 25; starting with [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">Patterns in the Last Digits of the Positive Powers of Five</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The positive powers of five &#8212; 5, 25, 125, 625, 3125, 15625, &#8230; &#8212; have a compact, repeating pattern in their ending <em>m</em> digits, in the powers of five from 5<sup>m</sup> on. For example: starting with 5, their last digit is always 5; starting with 25, their last two digits are always 25; starting with 125, their last three digits alternate between 125 and 625. These cycles come in lengths of powers of two.</p>
<div class="wp-caption aligncenter" style="width: 348px"><img src="http://www.exploringbinary.com/wp-content/uploads/PosPO5Cycles.jpg" alt="Cycles in the Ending Digits of the Powers of Five" width="338" height="375"/><p class="wp-caption-text">Cycles in the Ending Digits of the Powers of Five</p></div>
<p> I will show you why these cycles exist, how they are expressed mathematically, and how to visualize them.</p>
<p><span id="more-259"></span>(This article is the companion article to <a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Two&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-two/">&ldquo;Patterns in the Last Digits of the Positive Powers of Two&rdquo;</a>. It describes the digit patterns in the <em>negative</em> powers of two, indirectly, since <a title="Read Rick Regan's Article &ldquo;Seeing Powers of Five in Powers of Two and Vice Versa&rdquo;" href="http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/">positive powers of five look like negative powers of two</a>.)</p>
<h2>Cycles in the Last One to Four Digits</h2>
<p>You can use <a title="Wikipedia article on modular arithmetic" href="http://en.wikipedia.org/wiki/Modular_arithmetic">modular arithmetic</a> to show the repeating digit cycles of the positive powers of five. You can find the last m digits of a positive power by computing its <a title="Wolfram MathWorld definition of common residue" href="http://mathworld.wolfram.com/CommonResidue.html">common residue</a> mod 10<sup>m</sup>. You can find the last m digits of a <em>sequence</em> of powers by computing them incrementally, mod 10<sup>m</sup>. The cycle restarts when you get a result you&#8217;ve already seen.</p>
<h3>Last Digit</h3>
<p>Starting with 5, the last digit repeats in a cycle of period one: <strong>5</strong>. You show it with incremental calculations mod 10:</p>
<ol>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/37d926dfd0f791b1cff9bd68a62398cb.png' alt='\mbox{\footnotesize{\displaystyle{5^1 \equiv 5 \pmod{10}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/435972351628ded738ddb0f6ac58f955.png' alt='\mbox{\footnotesize{\displaystyle{5^2 \equiv 5^1 \cdot 5 \equiv 5 \cdot 5 \equiv 25 \equiv \textbf{5} \pmod{10}}}}'/></li>
<li>&#8230;</li>
</ol>
<p>In other words, <strong>every positive power of five ends in 5</strong>.</p>
<h3>Last Two Digits</h3>
<p>Starting with 5<sup>2</sup>, the last two digits repeat in a cycle of period one: <strong>25</strong>. You show it with incremental calculations mod 10<sup>2</sup>:</p>
<ol>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/88e8c50bd6fc3a5719debd1c3ebe3a30.png' alt='\mbox{\footnotesize{\displaystyle{5^1 \equiv 5 \pmod{100}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/a7f37d593e107718764f7619d0a6bb50.png' alt='\mbox{\footnotesize{\displaystyle{5^2 \equiv 5^1 \cdot 5 \equiv 5 \cdot 5 \equiv 25 \pmod{100}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/263beba8816cb6c55d4942f49834a53b.png' alt='\mbox{\footnotesize{\displaystyle{5^3 \equiv 5^2 \cdot 5 \equiv 25 \cdot 5 \equiv 125 \equiv \textbf{25} \pmod{100}}}}'/></li>
<li>&#8230;</li>
</ol>
<h3>Last Three Digits</h3>
<p>Starting with 5<sup>3</sup>, the last three digits repeat in a cycle of period two: <strong>125, 625</strong>. You show it with incremental calculations mod 10<sup>3</sup>:</p>
<ol>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/c78c390b686a7bcbcb38ef4f0c395438.png' alt='\mbox{\footnotesize{\displaystyle{5^1 \equiv 5 \pmod{1000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/d32574e530ca866d537b9d886bdb0696.png' alt='\mbox{\footnotesize{\displaystyle{5^2 \equiv 5^1 \cdot 5 \equiv 5 \cdot 5 \equiv 25 \pmod{1000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/00f6f2bc5b9df285ea62a4402c81ea7d.png' alt='\mbox{\footnotesize{\displaystyle{5^3 \equiv 5^2 \cdot 5 \equiv 25 \cdot 5 \equiv 125 \pmod{1000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/26b634db096bf579c6468e757f46cb43.png' alt='\mbox{\footnotesize{\displaystyle{5^4 \equiv 5^3 \cdot 5 \equiv 125 \cdot 5 \equiv 625 \pmod{1000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/2939e6a26e782515e64ff8aeece414b6.png' alt='\mbox{\footnotesize{\displaystyle{5^5 \equiv 5^4 \cdot 5 \equiv 625 \cdot 5 \equiv 3125 \equiv \textbf{125} \pmod{1000}}}}'/></li>
<li>&#8230;</li>
</ol>
<h3>Last Four Digits</h3>
<p>Starting with 5<sup>4</sup>, the last four digits repeat in a cycle of period four: <strong>0625, 3125, 5625, 8125</strong>. You show it with incremental calculations mod 10<sup>4</sup>:</p>
<ol>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/7275dc358cd2863b38e8aff7cf045084.png' alt='\mbox{\footnotesize{\displaystyle{5^1 \equiv 5 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/5e145e0b129ffec86aad237518cb6ec3.png' alt='\mbox{\footnotesize{\displaystyle{5^2 \equiv 5^1 \cdot 5 \equiv 5 \cdot 5 \equiv 25 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/4118f15b6c04d14c210eb6be756bde2e.png' alt='\mbox{\footnotesize{\displaystyle{5^3 \equiv 5^2 \cdot 5 \equiv 25 \cdot 5 \equiv 125 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/c632200dfe951823b4a0f8b778beeabb.png' alt='\mbox{\footnotesize{\displaystyle{5^4 \equiv 5^3 \cdot 5 \equiv 125 \cdot 5 \equiv 625 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/48b7c42691d7617290d3788cb764c596.png' alt='\mbox{\footnotesize{\displaystyle{5^5 \equiv 5^4 \cdot 5 \equiv 625 \cdot 5 \equiv 3125 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/9b463197e075045f5501e213334d51cb.png' alt='\mbox{\footnotesize{\displaystyle{5^6 \equiv 5^5 \cdot 5 \equiv 3125 \cdot 5 \equiv 15625 \equiv 5625 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/3f98b87da4b99c0c464037ad6bd42fb5.png' alt='\mbox{\footnotesize{\displaystyle{5^7 \equiv 5^6 \cdot 5 \equiv 5625 \cdot 5 \equiv 28125 \equiv 8125 \pmod{10000}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/patterns-in-the-last-digits-of-the-positive-powers-of-five/41386f7219e67c0a1227ffb9f3052859.png' alt='\mbox{\footnotesize{\displaystyle{5^8 \equiv 5^7 \cdot 5 \equiv 8125 \cdot 5 \equiv 40625 \equiv \textbf{625} \pmod{10000}}}}'/></li>
<li>&#8230;</li>
</ol>
<p>Whenever the residue is less than m digits, it is implicitly padded out with leading zeros. For example, the last 4 digits of 5<sup>8</sup> = 390625 are 0625.</p>
<h2>Cycles in the Last m Digits</h2>
<p>If you continue this process, you&#8217;ll see that the period doubles for each additional ending digit; I did the calculations through ten ending digits:</p>
<table class="center" border="1" summary="Cycle Length for Number of Ending Digits (1 to 10)">
<caption><strong>Cycle Length for Number of Ending Digits (1 to 10)</strong></caption>
<tbody>
<tr>
<th class="center">m</th>
<th class="center">Period</th>
<th class="center">Starts with</th>
</tr>
<tr>
<td class="center">1</td>
<td class="right">1</td>
<td class="right">5<sup>1</sup></td>
</tr>
<tr>
<td class="center">2</td>
<td class="right">1</td>
<td class="right">5<sup>2</sup></td>
</tr>
<tr>
<td class="center">3</td>
<td class="right">2</td>
<td class="right">5<sup>3</sup></td>
</tr>
<tr>
<td class="center">4</td>
<td class="right">4</td>
<td class="right">5<sup>4</sup></td>
</tr>
<tr>
<td class="center">5</td>
<td class="right">8</td>
<td class="right">5<sup>5</sup></td>
</tr>
<tr>
<td class="center">6</td>
<td class="right">16</td>
<td class="right">5<sup>6</sup></td>
</tr>
<tr>
<td class="center">7</td>
<td class="right">32</td>
<td class="right">5<sup>7</sup></td>
</tr>
<tr>
<td class="center">8</td>
<td class="right">64</td>
<td class="right">5<sup>8</sup></td>
</tr>
<tr>
<td class="center">9</td>
<td class="right">128</td>
<td class="right">5<sup>9</sup></td>
</tr>
<tr>
<td class="center">10</td>
<td class="right">256</td>
<td class="right">5<sup>10</sup></td>
</tr>
</tbody>
</table>
<p>This table implies that <strong>the ending m digits of the positive powers of five cycle with period 2<sup>m-2</sup>, m &ge; 2, starting at 5<sup>m</sup></strong>. (<a title="Read Rick Regan's Article &ldquo;Cycle Length of Powers of Five Mod Powers of Ten&rdquo;" href="http://www.exploringbinary.com/cycle-length-of-powers-of-five-mod-powers-of-ten/">The proof is here</a>.)</p>
<p>Powers of five with exponents that are congruent mod 2<sup>m-2</sup> are themselves congruent mod 10<sup>m</sup>. That is, 5<sup>i</sup> and 5<sup>i+(2<sup>m-2</sup>)&middot;k</sup>, i &ge; m, k &ge; 0, end with the same m digits.</p>
<h2>Visualizing the Nesting of Cycles</h2>
<p>The cycles in m digit, m-1 digit, m-2 digit, &#8230;, 1-digit endings can be viewed as nested, even though their starting points are staggered. You just have shift the starting points of the lesser digit cycles to make them coincide. For example, each copy of the length 4 cycle of four digit endings has within it two copies of the length 2 cycle of three digit endings; each copy of the length 2 cycle of three digit endings has within it 2 copies of the length 1 cycle of two digit endings.</p>
<p>This diagram shows the nesting by shading every other occurrence of a cycle (the millions place is fully shaded, because only one cycle of the last seven digits is shown):</p>
<div class="wp-caption aligncenter" style="width: 476px"><img src="http://www.exploringbinary.com/wp-content/uploads/PosPO5EndingDigits.jpg" alt="Nested 1-7 Digit Ending Patterns in Powers of 5 from 5^7to 5^38" width="466" height="740"/><p class="wp-caption-text">Nested 1-7 Digit Ending Patterns from 5<sup>7</sup> to 5<sup>38</sup></p></div>
<p>Not surprisingly, the nested powers of two in this pattern make it look similar to the pattern in <a title="Read Rick Regan's Article &ldquo;Visualizing Consecutive Binary Integers&rdquo;" href="http://www.exploringbinary.com/visualizing-consecutive-binary-integers/">consecutive binary integers</a>.</p>
<h3>Binary Tree</h3>
<p>Another way to show the nesting of powers of two is with a perfect binary tree. Here is a tree showing the ending 1-5 digits:</p>
<div class="wp-caption aligncenter" style="width: 517px"><img src="http://www.exploringbinary.com/wp-content/uploads/PO5Tree.digits.png" alt="Binary Tree Showing Nested 1-5 Digit Ending Patterns (Digits)" width="507" height="301"/><p class="wp-caption-text">Binary Tree Showing Nested 1-5 Digit Ending Patterns (Digits)</p></div>
<p>Each level contains the ending digits for a given cycle, from the last digit (the root, or first level) to the eight 5 digit endings (the leaves, or fifth level). Starting with the second level, each m digit ending is the suffix of two m+1 digit endings. </p>
<p>Here&#8217;s the same tree, except labeled with the smallest power of five corresponding to the ending digits:</p>
<div class="wp-caption aligncenter" style="width: 494px"><img src="http://www.exploringbinary.com/wp-content/uploads/PO5Tree.powers.jpg" alt="Binary Tree Showing Nested 1-5 Digit Ending Patterns (Powers)" width="484" height="318"/><p class="wp-caption-text">Binary Tree Showing Nested 1-5 Digit Ending Patterns (Powers)</p></div>
<p>Each level from level 2 down includes the endings for 5<sup>m</sup> through 5<sup>(m+2<sup>m-2</sup>-1)</sup>.</p>
<p>Notice two interesting things that become apparent with the binary tree representations:</p>
<ul>
<li><strong>The starting digits of each pair of children, for m &ge; 3, differ by 5</strong>. For example, <span class="highlight_gray3">3</span>125 and <span class="highlight_gray3">8</span>125 on level 4 (digits 3 and 8).</li>
<li><strong>The exponents of each pair of children, for m &ge; 3, differ by 2<sup>m-3</sup></strong>. For example, 5<sup>5</sup> and 5<sup>7</sup> on level 4.</li>
</ul>
<h2>Exploring Ending Digits with PARI/GP</h2>
<p>You can use <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a> to explore the cycles in the ending digits; here are two examples:</p>
<ul>
<li><strong>Print the first 20 positive powers of five</strong>:
<pre>
? <strong>for(i=1,20,print(&quot;5^&quot;,i,&quot;: &quot;,5^i))</strong>
5^1: 5
5^2: 25
5^3: 125
5^4: 625
5^5: 3125
5^6: 15625
5^7: 78125
5^8: 390625
5^9: 1953125
5^10: 9765625
5^11: 48828125
5^12: 244140625
5^13: 1220703125
5^14: 6103515625
5^15: 30517578125
5^16: 152587890625
5^17: 762939453125
5^18: 3814697265625
5^19: 19073486328125
5^20: 95367431640625
</pre>
</li>
<li><strong>Show the cycle in the last m (in this case 5) digits</strong>:
<pre>
? <strong>m=5; for(i=1,2^(m-2)+m,print(&quot;5^&quot;,i,&quot; mod 10^&quot;,m&quot;: &quot;,5^i%10^m))</strong>
5^1 mod 10^5: 5
5^2 mod 10^5: 25
5^3 mod 10^5: 125
5^4 mod 10^5: 625
5^5 mod 10^5: <strong>3125</strong>
5^6 mod 10^5: <strong>15625</strong>
5^7 mod 10^5: <strong>78125</strong>
5^8 mod 10^5: <strong>90625</strong>
5^9 mod 10^5: <strong>53125</strong>
5^10 mod 10^5: <strong>65625</strong>
5^11 mod 10^5: <strong>28125</strong>
5^12 mod 10^5: <strong>40625</strong>
5^13 mod 10^5: 3125
</pre>
<p>(The leading 0 of 3125 is not printed.)</p>
</li>
</ul>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/">Patterns in the Last Digits of the Positive Powers of Five</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-five/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seeing Powers of Five in Powers of Two and Vice Versa</title>
		<link>http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/</link>
		<comments>http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 18:26:14 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Powers of two]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Decimals]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Fractions]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=257</guid>
		<description><![CDATA[The decimal representations of oppositely signed powers of two and powers of five look alike, as seen in these examples: 2-3 = 0.125 and 53 = 125; 5-5 = 0.00032 and 25 = 32. The significant digits in each pair of powers is the same, even though one is a fraction and one is an [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/">Seeing Powers of Five in Powers of Two and Vice Versa</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The decimal representations of oppositely signed powers of two and powers of five look alike, as seen in these examples: 2<sup>-3</sup> = 0.125 and 5<sup>3</sup> = 125; 5<sup>-5</sup> = 0.00032 and 2<sup>5</sup> = 32. The significant digits in each pair of powers is the same, even though one is a fraction and one is an integer. In other words, a negative power of one base looks like a positive power of the other.</p>
<div class="wp-caption aligncenter" style="width: 431px"><img src="http://www.exploringbinary.com/wp-content/uploads/PO2s PO5.jpg" alt="Powers of Two and Powers of Five that Look Alike" width="421" height="91"/><p class="wp-caption-text">Powers of Two and Powers of Five that Look Alike</p></div>
<p>This relationship is not coincidence; it&#8217;s a by-product of how fractions are represented as decimals. I&#8217;ll show you simple algebra that proves it, as well as algebra that proves similar properties &#8212; in products involving negative powers.</p>
<p><span id="more-257"></span></p>
<h2>Positive Powers of Five in Negative Powers of Two</h2>
<p>A <a title="Read Rick Regan's Article &ldquo;A Table of Negative Powers of Two&rdquo;" href="http://www.exploringbinary.com/a-table-of-negative-powers-of-two/">negative power of two</a> in decimal form looks like a positive power of five, except that it has a decimal point and possibly some leading zeros. Here are some examples, the first eight pairs of positive powers of five and negative powers of two:</p>
<table class="center" border="1">
<tbody>
<tr>
<th class="center">n</th>
<th class="center">5<sup>n</sup></th>
<th class="center">2<sup>-n</sup></th>
</tr>
<tr>
<td class="right small">1</td>
<td class="right small">5</td>
<td class="right small">0.5</td>
</tr>
<tr>
<td class="right small">2</td>
<td class="right small">25</td>
<td class="right small">0.25</td>
</tr>
<tr>
<td class="right small">3</td>
<td class="right small">125</td>
<td class="right small">0.125</td>
</tr>
<tr>
<td class="right small">4</td>
<td class="right small">625</td>
<td class="right small">0.0625</td>
</tr>
<tr>
<td class="right small">5</td>
<td class="right small">3125</td>
<td class="right small">0.03125</td>
</tr>
<tr>
<td class="right small">6</td>
<td class="right small">15625</td>
<td class="right small">0.015625</td>
</tr>
<tr>
<td class="right small">7</td>
<td class="right small">78125</td>
<td class="right small">0.0078125</td>
</tr>
<tr>
<td class="right small">8</td>
<td class="right small">390625</td>
<td class="right small">0.00390625</td>
</tr>
</tbody>
</table>
<p>The powers that are paired have exponents that are <em>additive inverses</em> of each other. It&#8217;s easy to show this relationship holds for all such pairs of powers. A negative power of two is a <a href="http://www.exploringbinary.com/the-powers-of-two/" title="Read Rick Regan's Article &ldquo;The Powers of Two&rdquo;">number of the form 2<sup>n</sup></a>, n and integer less than 0, or equivalently, a number of the form 2<sup>-n</sup>, n &gt; 0. By the <a title="Read Rick Regan's Article &ldquo;The Laws of Exponents&rdquo;" href="http://www.exploringbinary.com/the-laws-of-exponents/">laws of exponents</a>, 2<sup>-n</sup> = 1/2<sup>n</sup> = (1/2)<sup>n</sup> = (5/10)<sup>n</sup> = <strong>5<sup>n</sup>/10<sup>n</sup></strong>. Written as a decimal, 5<sup>n</sup>/10<sup>n</sup> looks like a positive power of five, except that it&#8217;s a fraction with n decimal places.</p>
<p>A different transformation gives another way to look at this: 2<sup>-n</sup> = 1/2<sup>n</sup> = (1/2)<sup>n</sup> =  (0.5)<sup>n</sup>. If you were to multiply this out by hand, you would ignore the decimal point, multiply all the factors of five, and then place the decimal point when you were done. You&#8217;d end up with a positive power of five, but preceded with a decimal point and zero or more leading 0s &#8212; to pad it out to n decimal places.</p>
<h2>Positive Powers of Two in Negative Powers of Five</h2>
<p>Not surprisingly, a similar relationship exists in reverse; that is, between the <a title="Read Rick Regan's Article &ldquo;A Table of Nonnegative Powers of Two&rdquo; (positive powers of two start at 2)" href="http://www.exploringbinary.com/a-table-of-nonnegative-powers-of-two/">positive powers of two</a> and the negative powers of five. This makes sense, given the role of the numbers two and five in the decimal system.</p>
<p>A negative power of five in decimal form looks like a positive power of two, except that it has a decimal point and possibly some leading zeros. Here are some examples, the first eight pairs of positive powers of two and negative powers of five:</p>
<table class="center" border="1">
<tbody>
<tr>
<th class="center">n</th>
<th class="center">2<sup>n</sup></th>
<th class="center">5<sup>-n</sup></th>
</tr>
<tr>
<td class="right small">1</td>
<td class="right small">2</td>
<td class="right small">0.2</td>
</tr>
<tr>
<td class="right small">2</td>
<td class="right small">4</td>
<td class="right small">0.04</td>
</tr>
<tr>
<td class="right small">3</td>
<td class="right small">8</td>
<td class="right small">0.008</td>
</tr>
<tr>
<td class="right small">4</td>
<td class="right small">16</td>
<td class="right small">0.0016</td>
</tr>
<tr>
<td class="right small">5</td>
<td class="right small">32</td>
<td class="right small">0.00032</td>
</tr>
<tr>
<td class="right small">6</td>
<td class="right small">64</td>
<td class="right small">0.000064</td>
</tr>
<tr>
<td class="right small">7</td>
<td class="right small">128</td>
<td class="right small">0.0000128</td>
</tr>
<tr>
<td class="right small">8</td>
<td class="right small">256</td>
<td class="right small">0.00000256</td>
</tr>
</tbody>
</table>
<p>Again, the powers that are paired have exponents that are additive inverses of each other. Using a similar transformation as above, we can show this relationship holds for all such pairs of powers: 5<sup>-n</sup> = 1/5<sup>n</sup> = (1/5)<sup>n</sup> = (2/10)<sup>n</sup> = <strong>2<sup>n</sup>/10<sup>n</sup></strong>. Written as a decimal, 2<sup>n</sup>/10<sup>n</sup> looks like a positive power of two, except that it&#8217;s a fraction with n decimal places.</p>
<p>There&#8217;s also an alternate way to view this: 5<sup>-n</sup> = 1/5<sup>n</sup> = (1/5)<sup>n</sup> =  (0.2)<sup>n</sup>. As above, think of it as multiplying the factors to get a power of two and then prefixing a decimal point and leading zeros.</p>
<h2>Products of Negative Powers of Two and Positive Powers of Five</h2>
<p>Because a negative power of two looks like a positive power of five, multiplying it by a positive power of five makes it look like yet another positive power of five. The resulting power of five has an exponent that is the sum of the absolute values of the exponents of the multiplied powers. For example: 2<sup>-3</sup>&middot;5<sup>4</sup> = 78.125, which looks like 5<sup>7</sup> = 78125; 2<sup>-7</sup>&middot;5<sup>3</sup> = 0.9765625, which looks like 5<sup>10</sup> = 9765625. </p>
<p>Algebraically, the multiplication is expressed as 2<sup>-t</sup>&middot;5<sup>f</sup>, with t, f &gt; 0. The result looks like 5<sup>(t+f)</sup>; here&#8217;s why: 2<sup>-t</sup> = 5<sup>t</sup>/10<sup>t</sup>, so 2<sup>-t</sup>&middot;5<sup>f</sup> = <strong>5<sup>(t+f)</sup>/10<sup>t</sup></strong>. This expression describes a positive power of five, only shifted right t decimal places.</p>
<p>You can think of these as looking like negative powers of two as well. The product 2<sup>-t</sup>&middot;5<sup>f</sup> can be written equivalently as 2<sup>-t</sup>&middot;5<sup>f</sup>&middot;(2<sup>-f</sup>&middot;2<sup>f</sup>) = <strong>2<sup>-(t+f)</sup>&middot;10<sup>f</sup></strong>, which is 2<sup>-(t+f)</sup> shifted left f decimal places.</p>
<h2>Products of Negative Powers of Five and Positive Powers of Two</h2>
<p>Similarly, the product of a negative power of five and a positive power of two looks like a positive power of two. For example, 5<sup>-3</sup>&middot;2<sup>3</sup> = 0.064, which looks like 2<sup>6</sup> = 64, and 5<sup>-4</sup>&middot;2<sup>12</sup> = 6.5536, which looks like 2<sup>16</sup> = 65536.</p>
<p>Algebraically, the multiplication is expressed as 5<sup>-f</sup>&middot;2<sup>t</sup>, with f, t &gt; 0. The result looks like 2<sup>(f+t)</sup>; here&#8217;s why: 5<sup>-f</sup> = 2<sup>f</sup>/10<sup>f</sup>, so 5<sup>-f</sup>&middot;2<sup>t</sup> = <strong>2<sup>(f+t)</sup>/10<sup>f</sup></strong>. This expression describes a positive power of two, only shifted right f decimal places.</p>
<p>You can think of these as looking like negative powers of five as well. The product 5<sup>-f</sup>&middot;2<sup>t</sup> can be written equivalently as 5<sup>-f</sup>&middot;2<sup>t</sup>&middot;(5<sup>-t</sup>&middot;5<sup>t</sup>) = <strong>5<sup>-(f+t)</sup>&middot;10<sup>t</sup></strong>, which is 5<sup>-(f+t)</sup> shifted left t decimal places.</p>
<h2>Products of Negative Powers of Two and Negative Powers of Five</h2>
<p>A negative power of two times a negative power of five results in a string of digits that looks like <em>either</em> a positive power of five or a positive power of two, depending on which exponent is smaller. For example, 2<sup>-4</sup>&middot;5<sup>-2</sup> = 0.0025, which looks like a positive power of five, and 2<sup>-2</sup>&middot;5<sup>-4</sup> = 0.0004, which looks like a positive power of two.</p>
<p>Algebraically, the multiplication is expressed as 2<sup>-t</sup>&middot;5<sup>-f</sup>, with t, f &gt; 0, and f  &ne; t. There are two cases:</p>
<ul>
<li><strong>f &lt; t</strong>: the result looks like 5<sup>(t-f)</sup>, with t decimal places. Here&#8217;s why:
<p>Recall from above that 2<sup>-t</sup> = 5<sup>t</sup>/10<sup>t</sup> and that 5<sup>-f</sup> = 2<sup>f</sup>/10<sup>f</sup>. This gives 2<sup>-t</sup>&middot;5<sup>-f</sup> = (5<sup>t</sup>/10<sup>t</sup>)(2<sup>f</sup>/10<sup>f</sup>) = (5<sup>t</sup>&middot;2<sup>f</sup>)/10<sup>(t+f)</sup>. Now factor out 10<sup>f</sup> from the numerator, which we can do since f &lt; t: (5<sup>t</sup>&middot;2<sup>f</sup>)/10<sup>(t+f)</sup> = (10<sup>f</sup>&middot;5<sup>(t-f)</sup>)/10<sup>(t+f)</sup> = <strong>5<sup>(t-f)</sup>/10<sup>t</sup></strong>.
</p>
</li>
<li><strong>t &lt; f</strong>: the result looks like 2<sup>(f-t)</sup>, with f decimal places. Here&#8217;s why:
<p>Starting from (5<sup>t</sup>&middot;2<sup>f</sup>)/10<sup>(t+f)</sup>, factor out 10<sup>t</sup> from the numerator, which we can do since t &lt; f: (5<sup>t</sup>&middot;2<sup>f</sup>)/10<sup>(t+f)</sup> = (10<sup>t</sup>&middot;2<sup>(f-t)</sup>)/10<sup>(t+f)</sup> = <strong>2<sup>(f-t)</sup>/10<sup>f</sup></strong>.
</p>
</li>
</ul>
<p>(If you allow f = t, the answer would be 2<sup>0</sup> = 5<sup>0</sup> = 1, a nonnegative power, with f = t decimal places.)
</p>
<h2>Discussion</h2>
<p>I came up with the algebra for the multiplication cases <em>after</em> I played around with some numbers in <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a>. I noticed a pattern, and then sought to state it mathematically. </p>
<p>The patterns hold for quotients of powers as well, since divisions can be converted to multiplications by inverting exponents.</p>
<h3>Some Practical Uses</h3>
<p>If you know the positive powers of five, or if you learn them, you will recognize negative powers of two more readily &#8212; similarly for the positive powers of two and negative powers of five.</p>
<p>If you wanted to print a table of negative powers of two or negative powers of five using a computer program, you could do so easily, just by using integers: convert the positive powers to strings, and then prefix each with a decimal point and the appropriate number of leading zeros.</p>
<h2>References</h2>
<ul>
<li>learner.org article <a href="http://www.learner.org/courses/learningmath/number/session7/part_a/index.html" title="Read learner.org article discussing powers of two and powers of five">&ldquo;Fractions to Decimals&rdquo;</a>.</li>
</ul>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/">Seeing Powers of Five in Powers of Two and Vice Versa</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/seeing-powers-of-five-in-powers-of-two-and-vice-versa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cycle Length of Powers of Two Mod Powers of Ten</title>
		<link>http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/</link>
		<comments>http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 20:28:51 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Powers of two]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Exponents]]></category>
		<category><![CDATA[Modular arithmetic]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=255</guid>
		<description><![CDATA[In my article &#8220;Patterns in the Last Digits of the Positive Powers of Two&#8221; I noted that the positive powers of two modulo 10m cycle with period 4&#183;5m-1, starting at 2m. For example, the powers of two mod 10 cycle with period four: 2, 4, 8, 6, 2, 4, 8, 6, &#8230; . In this [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/">Cycle Length of Powers of Two Mod Powers of Ten</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In my article <a title="Read Rick Regan's Article &ldquo;Patterns in the Last Digits of the Positive Powers of Two&rdquo;" href="http://www.exploringbinary.com/patterns-in-the-last-digits-of-the-positive-powers-of-two/">&ldquo;Patterns in the Last Digits of the Positive Powers of Two&rdquo;</a> I noted that the positive powers of two modulo 10<sup>m</sup> cycle with period 4&middot;5<sup>m-1</sup>, starting at 2<sup>m</sup>. For example, the powers of two mod 10 cycle with period four: 2, 4, 8, 6, 2, 4, 8, 6, &#8230; . In this article, I&#8217;ll present my proof, which has two parts:</p>
<ul>
<li>Part 1 shows that the powers of two <strong>mod 5<sup>m</sup></strong> cycle with period 4&middot;5<sup>m-1</sup>, <strong>starting at 2<sup>0</sup></strong>.</li>
<li>Part 2 shows that the powers of two <strong>mod 10<sup>m</sup></strong> cycle with the same period as the powers of two mod 5<sup>m</sup>, <strong>starting at 2<sup>m</sup></strong>.</li>
</ul>
<p><span id="more-255"></span>To understand the proof, you&#8217;ll need to know some <a title="Wikipedia article on elementary number theory" href="http://en.wikipedia.org/wiki/Number_theory#Elementary_number_theory">elementary number theory</a>.</p>
<h2>Part 1: Period mod 5<sup>m</sup> is 4&middot;5<sup>m-1</sup></h2>
<p>The main goal of part 1 is to show that two is a <a title="Wikipedia article on primitive roots" href="http://en.wikipedia.org/wiki/Primitive_root_modulo_n">primitive root</a> mod powers of five, meaning that the <a title="Wikipedia article on multiplicative order" href="http://en.wikipedia.org/wiki/Multiplicative_order">order</a> of 2 mod 5<sup>m</sup> is the value of <a title="Wikipedia article on Euler's totient function" href="http://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler&#8217;s phi function</a> for 5<sup>m</sup>. I break this into three steps, showing that</p>
<ul>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/1f355f9fb4d31e4e805b5bca35a0b22a.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (5^m)}}}'/> = 4&middot;5<sup>m-1</sup>, where <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/184ae9c731fbfbc2f1b43ca72d014388.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (n)}}}'/> is Euler&#8217;s phi function (also known as Euler&#8217;s totient function).</li>
<li>2 is a primitive root mod 5.</li>
<li>2 is a primitive root mod 5<sup>m</sup>.</li>
</ul>
<p>In addition, I show that the powers of two mod 5<sup>m</sup> cycle starting at 2<sup>0</sup>; this is important because it will show that the cycle mod 10<sup>m</sup> always starts after the cycle mod 5<sup>m</sup>.</p>
<h3><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e49adba228c1034d35220364a784ab95.png' alt='\mbox{\footnotesize{\displaystyle{\bf{\varphi (5^m)}}}}'/> = 4&middot;5<sup>m-1</sup></h3>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/184ae9c731fbfbc2f1b43ca72d014388.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (n)}}}'/> is the number of positive integers less than or equal to n that are relatively prime to n. For a power of a prime number p, we can use <a title="Wikipedia article that shows the formula for Euler's Phi Function" href="http://en.wikipedia.org/wiki/Euler%27s_totient_function#Computing_Euler.27s_function">this formula</a> to compute it: <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/b00cb65570b30fb2762cf72221133b28.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (p^m) = (p \,$-$ 1) \cdot p^{m\,\textnormal{-}1}}}}'/>. For p=5, this gives <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e49adba228c1034d35220364a784ab95.png' alt='\mbox{\footnotesize{\displaystyle{\bf{\varphi (5^m)}}}}'/> = 4&middot;5<sup>m-1</sup>.</p>
<h3>2 is a Primitive Root Mod 5</h3>
<p>To prove this, we just have to show that the order (also known as multiplicative order) of 2 mod 5 is equal to <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/dc27f8621c4cb0a44cee2326909bf835.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (5)}}}'/>.</p>
<p><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/dc27f8621c4cb0a44cee2326909bf835.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (5)}}}'/> = 4, by the formula above. The order of 2 mod 5 &#8212; the smallest exponent <em>a</em> such that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/7a12e11a464d5c2f398cc07b12d8c068.png' alt='\mbox{\footnotesize{\displaystyle{2^a \equiv 1 \pmod{5}}}}'/>  &#8212; is four, as shown by the following sequence of four calculations:</p>
<ol>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/2c53ee0d065e7f59ccb35f4c12284f60.png' alt='\mbox{\footnotesize{\displaystyle{2^1 \equiv 2 \pmod{5}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/ed422d8632ac6032cca44f1af5abcede.png' alt='\mbox{\footnotesize{\displaystyle{2^2 \equiv 4 \pmod{5}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/6f970989bb37eb98565a64b18c96d2fe.png' alt='\mbox{\footnotesize{\displaystyle{2^3 \equiv 3 \pmod{5}}}}'/></li>
<li><img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/31dfa53f1b0fe93191ff997b66013fb0.png' alt='\mbox{\footnotesize{\displaystyle{2^4 \equiv \textbf{1} \pmod{5}}}}'/></li>
</ol>
<p>Thus, 2 is a primitive root mod 5.</p>
<h3>2 is a Primitive Root Mod 5<sup>m</sup></h3>
<p>To show that 2 is a primitive root mod <em>all</em> positive powers of 5, we can use this result from <a title="Google Books Samples of &ldquo;A Course in Computational Algebraic Number Theory&rdquo;" href="http://books.google.com/books?id=hXGr-9l1DXcC&#038;pg=PP1&#038;dq=A+Course+in+Computational+Algebraic+Number+Theory&#038;ei=98TxSsRYjeSTBP7p6egL#v=snippet&#038;q=primitive&#038;f=false">page 26 of &ldquo;A Course in Computational Algebraic Number Theory&rdquo;</a> by Henri Cohen:</p>
<p><em>&ldquo;&#8230;to find a primitive root modulo p<sup>a</sup> for p an odd prime and a &ge; 2, proceed as follows: first compute g a primitive root modulo p &#8230;, then compute g<sub>1</sub> = g<sup>p-1</sup> mod p<sup>2</sup>. If g<sub>1</sub> &#8800; 1, g is a primitive root modulo p<sup>a</sup> for every a&#8230;&rdquo; .</em></p>
<p>In our case g = 2 and p = 5. We&#8217;ve already shown 2 to be a primitive root mod 5, so now all we need to show is that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/543c2c73be226f796c06a88aea76f9d2.png' alt='\mbox{\footnotesize{\displaystyle{2^{5\,\textnormal{-}1} \not \equiv 1 \pmod{5^2}}}}'/>, which is trivial: <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/08661606ada56e75024a79726112d8a4.png' alt='\mbox{\footnotesize{\displaystyle{2^4 \equiv 16 \pmod{25}}}}'/>.</p>
<p>So, 2 is a primitive root mod 5<sup>m</sup>, which means that the order of 2 mod 5<sup>m</sup> is <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/1f355f9fb4d31e4e805b5bca35a0b22a.png' alt='\mbox{\footnotesize{\displaystyle{\varphi (5^m)}}}'/>. This proves that the powers of two mod 5<sup>m</sup> cycle with period 4&middot;5<sup>m-1</sup>.</p>
<h3>The Powers of Two Mod 5<sup>m</sup> Cycle Starting at 2<sup>0</sup></h3>
<p>Let the period p = 4&middot;5<sup>m-1</sup>. <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/251362c9141cdf5ea2259e3e88769feb.png' alt='\mbox{\footnotesize{\displaystyle{2^p \equiv 1 \pmod{5^m}}}}'/>, since 2 is a primitive root mod powers of 5 (<a title="Wikipedia article on Euler&rsquo;s theorem" href="http://en.wikipedia.org/wiki/Euler%27s_theorem">Euler&#8217;s theorem applies</a>). We also know, trivially, that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/287636d2cc571728a931b6802618bdcb.png' alt='\mbox{\footnotesize{\displaystyle{2^0 \equiv 1 \pmod{5^m}}}}'/>. The difference between the exponents p and 0 is p, showing a full cycle occurs starting at 2<sup>0</sup>.</p>
<h2>Part 2: Period mod 10<sup>m</sup> is 4&middot;5<sup>m-1</sup></h2>
<p>Part 2 shows, using the definition of <a title="Wikipedia article on modular arithmetic" href="http://en.wikipedia.org/wiki/Modular_arithmetic">modular arithmetic</a>, the <a href="http://www.exploringbinary.com/composing-powers-of-two-using-the-laws-of-exponents/" title="Read Rick Regan's Article &ldquo;Composing Powers of Two Using The Laws of Exponents&rdquo;">laws of exponents</a>, and simple algebra, that the powers of two mod 10<sup>m</sup> have the same period as the powers of two mod 5<sup>m</sup>. It&#8217;s broken into two halves, showing that</p>
<ul>
<li>The period mod 5<sup>m</sup> is a cycle mod 10<sup>m</sup>.</li>
<li>The cycle mod 10<sup>m</sup> is the period mod 5<sup>m</sup>.</li>
</ul>
<p>The first half only shows that the period mod 5<sup>m</sup> is <em>a multiple</em> of the period mod 10<sup>m</sup>. The second half shows that the period mod 10<sup>m</sup> is in fact the same as the period mod 5<sup>m</sup>.</p>
<p>I&#8217;ll use the variable <em>p</em> for the period mod 5<sup>m</sup>, as above, to simplify the notation in the proof that follows.</p>
<h4>The period mod 5<sup>m</sup> is a cycle mod 10<sup>m</sup></h4>
<p>For i &ge; 0, what we&#8217;ve shown is that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e77db816df140e7c827d0a78f1a1fb50.png' alt='\mbox{\footnotesize{\displaystyle{2^{i+p} \equiv 2^i \pmod{5^m}}}}'/>. That is, powers of two with exponents a period apart are congruent mod 5<sup>m</sup>.</p>
<p>Congruence mod 5<sup>m</sup> means that 2<sup>i+p</sup> &#8211; 2<sup>i</sup> is a multiple of 5<sup>m</sup>; that is, 2<sup>i+p</sup> &#8211; 2<sup>i</sup> = n&middot;5<sup>m</sup>, for some nonnegative integer n.</p>
<p>Factoring out 2<sup>i</sup> we get 2<sup>i</sup>(2<sup>p</sup> &#8211; 1) = n&middot;5<sup>m</sup>. <strong>When i&ge;m, n is a multiple of 2<sup>m</sup></strong>, so n = n&#8217;&middot;2<sup>m</sup>.</p>
<p>Substituting n&#8217;&middot;2<sup>m</sup> for n we get 2<sup>i</sup>(2<sup>p</sup> &#8211; 1) = n&#8217;&middot;2<sup>m</sup>5<sup>m</sup> = n&#8217;&middot;10<sup>m</sup>.</p>
<p>Reversing the process above, we can show congruence mod 10<sup>m</sup>: </p>
<p>2<sup>i</sup>(2<sup>p</sup> &#8211; 1) = 2<sup>i+p</sup> &#8211; 2<sup>i</sup> = n&#8217;&middot;10<sup>m</sup>.</p>
<p>This shows that for i&ge;m, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e2507013f02cbab05d150cbe86fecb65.png' alt='\mbox{\footnotesize{\displaystyle{2^{i+p} \equiv 2^i \pmod{10^m}}}}'/>, so p is a cycle mod 10<sup>m</sup>.</p>
<h4>The cycle mod 10<sup>m</sup> is the period mod 5<sup>m</sup></h4>
<p>We&#8217;ve shown that p is a cycle mod 10<sup>m</sup>, but not that p is the period mod 10<sup>m</sup>. In other words, it&#8217;s possible the period mod 10<sup>m</sup> is less than p &#8212; we have to show it isn&#8217;t.</p>
<p>For i &ge; m, we&#8217;ve shown that <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e2507013f02cbab05d150cbe86fecb65.png' alt='\mbox{\footnotesize{\displaystyle{2^{i+p} \equiv 2^i \pmod{10^m}}}}'/>. Using the argument above, congruence mod 10<sup>m</sup> means that:</p>
<p>2<sup>i+p</sup> &#8211; 2<sup>i</sup> = n&middot;10<sup>m</sup> = n&middot;2<sup>m</sup>5<sup>m</sup> = (n&middot;2<sup>m</sup>)5<sup>m</sup>.</p>
<p>This shows that for i&ge;m, <img class='align_middle' src='http://www.exploringbinary.com/wp-content/uploads/latexrender/pictures/cycle-length-of-powers-of-two-mod-powers-of-ten/e77db816df140e7c827d0a78f1a1fb50.png' alt='\mbox{\footnotesize{\displaystyle{2^{i+p} \equiv 2^i \pmod{5^m}}}}'/>, so p is the period mod 5<sup>m</sup> <em>and</em> mod 10<sup>m</sup>.</p>
<h2>Empirical Evidence that 2 is a Primitive Root mod 5<sup>m</sup></h2>
<p>This section is not part of the proof, but it shows some <a href="http://www.exploringbinary.com/exploring-binary-numbers-with-parigp-calculator/" title="Read Rick Regan's Article &ldquo;Exploring Binary Numbers With PARI/GP Calculator&rdquo;">PARI/GP</a> calculations that support it.</p>
<p>These two commands show indirectly that 2 is a primitive root mod 5 through mod 5<sup>20</sup>, by showing that the order equals Euler&#8217;s phi function:</p>
<pre>
? <strong>for(m=1,20,print(eulerphi(5^m)))</strong>
4
20
100
500
2500
12500
62500
312500
1562500
7812500
39062500
195312500
976562500
4882812500
24414062500
122070312500
610351562500
3051757812500
15258789062500
76293945312500

? <strong>for(m=1,20,print(znorder(Mod(2,5^m))))</strong>
4
20
100
500
2500
12500
62500
312500
1562500
7812500
39062500
195312500
976562500
4882812500
24414062500
122070312500
610351562500
3051757812500
15258789062500
76293945312500
</pre>
<p class="break">This command shows directly that 2 is a primitive root mod powers of 5, using PARI/GP&#8217;s <em>znprimroot() </em>function (znprimroot only returns the lowest primitive root, which luckily for us is 2):</p>
<pre>
? <strong>for(m=1,20,print(znprimroot(5^m)))</strong>
Mod(2, 5)
Mod(2, 25)
Mod(2, 125)
Mod(2, 625)
Mod(2, 3125)
Mod(2, 15625)
Mod(2, 78125)
Mod(2, 390625)
Mod(2, 1953125)
Mod(2, 9765625)
Mod(2, 48828125)
Mod(2, 244140625)
Mod(2, 1220703125)
Mod(2, 6103515625)
Mod(2, 30517578125)
Mod(2, 152587890625)
Mod(2, 762939453125)
Mod(2, 3814697265625)
Mod(2, 19073486328125)
Mod(2, 95367431640625)
</pre>
<h2>Summary</h2>
<p>To prove the period formula for the positive powers of two mod powers of ten, I first proved it for mod powers of five. I showed that two is a primitive root mod powers of five, meaning that the period formula is Euler&#8217;s phi function for the powers of five. I then showed that the period mod powers of five and mod powers of ten must be the same, after a certain starting point.</p>
<h2>References</h2>
<p>These are the main influences behind my proof:</p>
<ul>
<li><a title="Google Books Samples of &ldquo;Challenging Mathematical Problems with Elementary Solutions, Volume 1&rdquo;" href="http://books.google.com/books?id=aVLLYiu8hs0C&#038;dq=Challenging+mathematical+problems+with+elementary+solutions,+Volume+1&#038;printsec=frontcover&#038;source=bl&#038;ots=30xgjayaSv&#038;sig=q8qvXsBFG9zogCZWJ-S4YCxHXl0&#038;hl=en&#038;ei=Wk7oSonjJNXtlAeqxNSMCA&#038;sa=X&#038;oi=book_result&#038;ct=result&#038;resnum=3&#038;ved=0CBMQ6AEwAg#v=onepage&#038;q=&#038;f=false">Challenging Mathematical Problems with Elementary Solutions, Volume 1</a>. Page 198: &ldquo;It can be proved that the last k digits of the number 2<sup>n</sup> are repeated in groups of 4&middot;5<sup>k-1</sup>, starting with the number 2<sup>k</sup>.&rdquo;</li>
<li><a title="Google Books Samples of &ldquo;The USSR Olympiad Problem Book; Selected Problems and Theorems of Elementary Mathematics&rdquo;" href="http://books.google.com/books?id=ONCQUUWjUc8C&#038;dq=The+USSR+olympiad+problem+book;+selected+problems+and+theorems+of+elementary+mathematics&#038;printsec=frontcover&#038;source=bn&#038;hl=en&#038;ei=hVDoSqqMLdCTlAevlrz8Bw&#038;sa=X&#038;oi=book_result&#038;ct=result&#038;resnum=4&#038;ved=0CA8Q6AEwAw#v=onepage&#038;q=&#038;f=false">The USSR Olympiad Problem Book; Selected Problems and Theorems of Elementary Mathematics</a>. Page 57, problem 243, and its answer, on page 354, discuss the case for powers of two mod 10<sup>10</sup>.</li>
<li><a title="Question I asked at physicsforums.com" href="http://www.physicsforums.com/showthread.php?p=2400449#post2400449">Question I asked at physicsforums.com</a>. User  <em>hamster143</em>&#8217;s proof that the period for powers of five and ten are equal.</li>
<li><a title="Entry by user shmoe at physicsforums.com" href="http://www.physicsforums.com/archive/index.php/t-76662.html">Discussion at physicsforums.com</a>. User <em>shmoe</em>&#8217;s comment: &ldquo;a power of 2&#8217;s congruence class mod 10 is completely determined by its congruence class mod 5&rdquo;.</li>
<li><a title="Hakmem Item 57" href="http://inwap.com/pdp10/hbaker/hakmem/number.html">Hakmem Item 57</a>. Two comments by <em>Schroeppel</em>:
<ul>
<li>&ldquo;After the nth, they are all multiples of 2<sup>n</sup>.&rdquo;</li>
<li>&ldquo;They get into a loop of length 4&middot;5<sup>m-1</sup>. (Because 2 is a primitive root of powers of 5.)&rdquo;</li>
</ul>
</li>
<li><a title="Question I asked at mathoverflow.net" href="http://mathoverflow.net/questions/919/cycle-length-of-the-positive-powers-of-two-mod-powers-of-ten">Question I asked at mathoverflow.net</a>.</li>
</ul>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/">Cycle Length of Powers of Two Mod Powers of Ten</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/cycle-length-of-powers-of-two-mod-powers-of-ten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Floating-Point Error in the NPR Media Player</title>
		<link>http://www.exploringbinary.com/floating-point-error-in-the-npr-media-player/</link>
		<comments>http://www.exploringbinary.com/floating-point-error-in-the-npr-media-player/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 14:11:58 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Numbers in computers]]></category>
		<category><![CDATA[Floating-point binary]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=254</guid>
		<description><![CDATA[The NPR Media Player apparently uses floating-point numbers to represent timestamps, based on this image (click it to enlarge):
The counter displays NaN:NaN:NaN. It should show the current time count, like 01:12  (why it shows three NaNs instead of two I don&#8217;t know). NaN is the floating-point representation of &#8220;not a number,&#8221; which means something [...]<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/floating-point-error-in-the-npr-media-player/">Floating-Point Error in the NPR Media Player</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The NPR Media Player apparently uses floating-point numbers to represent timestamps, based on this image (click it to enlarge):</p>
<div class="wp-caption aligncenter" style="width: 460px"><a href="http://www.exploringbinary.com/wp-content/uploads/NPR-NaNs.jpg" title="Click to see full-size image of NaNs in NPR Media Player"><img src="http://www.exploringbinary.com/wp-content/uploads/NPR-NaNs-thumbnail.jpg" alt="NaNs in NPR Media Player (thumbnail)" width="450" height="391"/></a><p class="wp-caption-text">NaNs in NPR Media Player (click image to enlarge).</p></div>
<p><span id="more-254"></span>The counter displays <strong>NaN:NaN:NaN</strong>. It should show the current time count, like 01:12  (why it shows three NaNs instead of two I don&#8217;t know). <a title="Wikipedia article on Not-A-Number" href="http://en.wikipedia.org/wiki/NaN">NaN</a> is the floating-point representation of &#8220;not a number,&#8221; which means something went wrong in their calculation of the timestamp. </p>
<p>This happened after I hibernated my computer and powered up, while the media player was paused.</p>
<p>(Maybe my blogging &#8220;will bring change&#8221; to the NPR Media Player <img src='http://www.exploringbinary.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .)</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/floating-point-error-in-the-npr-media-player/">Floating-Point Error in the NPR Media Player</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/floating-point-error-in-the-npr-media-player/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My Custom Binary Themed Calendar for 2010</title>
		<link>http://www.exploringbinary.com/my-custom-binary-themed-calendar-for-2010/</link>
		<comments>http://www.exploringbinary.com/my-custom-binary-themed-calendar-for-2010/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 13:13:23 +0000</pubDate>
		<dc:creator>Rick Regan</dc:creator>
				<category><![CDATA[Binary code]]></category>
		<category><![CDATA[Geekware]]></category>

		<guid isPermaLink="false">http://www.exploringbinary.com/?p=253</guid>
		<description><![CDATA[I found twelve free stock images &#8212; all with binary themes &#8212; and made a custom calendar for 2010. Here&#8217;s May:

Here are thumbnail images of each month:
This is my personal calendar &#8212; it&#8217;s not for sale!
(Courtesy Stock.XCHNG, Dreamstime, Morguefile, and shutterfly.)
By Rick Regan (Copyright &#169; 2008-2010  Exploring Binary)My Custom Binary Themed Calendar for 2010
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/my-custom-binary-themed-calendar-for-2010/">My Custom Binary Themed Calendar for 2010</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I found twelve free stock images &#8212; all with binary themes &#8212; and made a custom calendar for 2010. Here&#8217;s May:</p>
<div class="wp-caption aligncenter" style="width: 367px"><img src="http://www.exploringbinary.com/wp-content/uploads/BinaryCalendar-May-2010.jpg" alt="My Custom Binary-Themed Calendar For 2010" width="357" height="561"/><p class="wp-caption-text">My Custom Binary-Themed Calendar For 2010</p></div>
<p><span id="more-253"></span><br />
Here are thumbnail images of each month:</p>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/January-2010.jpg" alt="January 2010" width="175" height="274"/><p class="wp-caption-text">January 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/February-2010.jpg" alt="February 2010" width="175" height="275"/><p class="wp-caption-text">February 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/March-2010.jpg" alt="March 2010" width="175" height="274"/><p class="wp-caption-text">March 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/April-2010.jpg" alt="April 2010" width="175" height="274"/><p class="wp-caption-text">April 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/May-2010.jpg" alt="May 2010" width="175" height="274"/><p class="wp-caption-text">May 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/June-2010.jpg" alt="June 2010" width="175" height="273"/><p class="wp-caption-text">June 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/July-2010.png" alt="July 2010" width="175" height="274"/><p class="wp-caption-text">July 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/August-2010.jpg" alt="August 2010" width="175" height="274"/><p class="wp-caption-text">August 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/September-2010.jpg" alt="September 2010" width="175" height="274"/><p class="wp-caption-text">September 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/October-2010.jpg" alt="October 2010" width="175" height="273"/><p class="wp-caption-text">October 2010</p></div><br />
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/November-2010.jpg" alt="November 2010" width="175" height="274"/><p class="wp-caption-text">November 2010</p></div>
<div class="wp-caption aligncenter" style="width: 185px"><img src="http://www.exploringbinary.com/wp-content/uploads/December-2010.jpg" alt="December 2010" width="175" height="275"/><p class="wp-caption-text">December 2010</p></div>
<p>This is my personal calendar &#8212; it&#8217;s not for sale!</p>
<p class="break">(Courtesy <a href="http://www.sxc.hu/" title="Go to sxc.hu">Stock.XCHNG</a>, <a href="http://dreamstime.com/" title="Go to dreamstime.com">Dreamstime</a>, <a href="http://www.morguefile.com/" title="Go to morguefile.com">Morguefile</a>, and <a href="http://www.shutterfly.com/ " title="Go to shutterfly.com">shutterfly</a>.)</p>
<p>By Rick Regan (Copyright &copy; 2008-2010  <a href="http://www.exploringbinary.com">Exploring Binary</a>)<br/><br/><a href="http://www.exploringbinary.com/my-custom-binary-themed-calendar-for-2010/">My Custom Binary Themed Calendar for 2010</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploringbinary.com/my-custom-binary-themed-calendar-for-2010/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
