<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Project_Euler%2F97</id>
	<title>Project Euler/97 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://charlesreid1.com/w/index.php?action=history&amp;feed=atom&amp;title=Project_Euler%2F97"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;action=history"/>
	<updated>2026-06-25T05:33:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30802&amp;oldid=prev</id>
		<title>Admin: Replace remaining em-dash with regular dash in Complexity section (via update-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30802&amp;oldid=prev"/>
		<updated>2026-06-24T11:28:53Z</updated>

		<summary type="html">&lt;p&gt;Replace remaining em-dash with regular dash in Complexity section (via update-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 11:28, 24 June 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l79&quot;&gt;Line 79:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 79:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The modulus is &amp;lt;math&amp;gt;10^{10} = 2^{10} \times 5^{10}&amp;lt;/math&amp;gt;. Since the base (2) shares a common factor with the modulus, &amp;lt;math&amp;gt;\gcd(2, 10^{10}) = 2 \neq 1&amp;lt;/math&amp;gt;, Euler&amp;#039;s theorem cannot be applied directly to reduce the exponent modulo &amp;lt;math&amp;gt;\varphi(10^{10})&amp;lt;/math&amp;gt;. The robust approach of repeated squaring with modular reduction at every step works regardless.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The modulus is &amp;lt;math&amp;gt;10^{10} = 2^{10} \times 5^{10}&amp;lt;/math&amp;gt;. Since the base (2) shares a common factor with the modulus, &amp;lt;math&amp;gt;\gcd(2, 10^{10}) = 2 \neq 1&amp;lt;/math&amp;gt;, Euler&amp;#039;s theorem cannot be applied directly to reduce the exponent modulo &amp;lt;math&amp;gt;\varphi(10^{10})&amp;lt;/math&amp;gt;. The robust approach of repeated squaring with modular reduction at every step works regardless.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==Complexity==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Binary exponentiation uses &amp;lt;math&amp;gt;O(\log e)&amp;lt;/math&amp;gt; modular multiplications. With only ~23 iterations and at most one extra modular multiplication per iteration, the algorithm is extremely fast. Memory usage is &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;- &lt;/ins&gt;only a handful of integers are stored, and each is immediately reduced modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Binary exponentiation uses &amp;lt;math&amp;gt;O(\log e)&amp;lt;/math&amp;gt; modular multiplications. With only ~23 iterations and at most one extra modular multiplication per iteration, the algorithm is extremely fast. Memory usage is &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;— &lt;/del&gt;only a handful of integers are stored, and each is immediately reduced modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Java Solution=&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Java Solution=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb:diff::1.12:old-30801:rev-30802 --&gt;
&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30801&amp;oldid=prev</id>
		<title>Admin: Replace em-dashes with regular dashes (via update-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30801&amp;oldid=prev"/>
		<updated>2026-06-24T11:28:46Z</updated>

		<summary type="html">&lt;p&gt;Replace em-dashes with regular dashes (via update-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 11:28, 24 June 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l13&quot;&gt;Line 13:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 13:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Why this problem==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Why this problem==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This problem is a classic exercise in modular arithmetic. The prime number in question has over two million digits, making it impossible to compute directly using standard integer types. The key insight is that only the last ten digits are required, which means we can work entirely modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;. This problem demonstrates the power of modular exponentiation &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;— &lt;/del&gt;specifically, the technique of exponentiation by squaring (also known as binary exponentiation) &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;— &lt;/del&gt;to efficiently compute enormous powers under a modulus.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This problem is a classic exercise in modular arithmetic. The prime number in question has over two million digits, making it impossible to compute directly using standard integer types. The key insight is that only the last ten digits are required, which means we can work entirely modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;. This problem demonstrates the power of modular exponentiation &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;- &lt;/ins&gt;specifically, the technique of exponentiation by squaring (also known as binary exponentiation) &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;- &lt;/ins&gt;to efficiently compute enormous powers under a modulus.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Solution Approach=&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Solution Approach=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb:diff::1.12:old-30800:rev-30801 --&gt;
&lt;/table&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
	<entry>
		<id>https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30800&amp;oldid=prev</id>
		<title>Admin: Create Project Euler/97 page with problem statement, solution approach, and Java solution (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/97&amp;diff=30800&amp;oldid=prev"/>
		<updated>2026-06-24T11:21:56Z</updated>

		<summary type="html">&lt;p&gt;Create Project Euler/97 page with problem statement, solution approach, and Java solution (via create-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This is problem 97 on Project Euler: https://projecteuler.net/problem=97&lt;br /&gt;
&lt;br /&gt;
=Overview of the Problem=&lt;br /&gt;
&lt;br /&gt;
==Question==&lt;br /&gt;
&lt;br /&gt;
The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 2&amp;lt;sup&amp;gt;6972593&amp;lt;/sup&amp;gt; − 1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2&amp;lt;sup&amp;gt;p&amp;lt;/sup&amp;gt; − 1, have been found which contain more digits.&lt;br /&gt;
&lt;br /&gt;
However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433 × 2&amp;lt;sup&amp;gt;7830457&amp;lt;/sup&amp;gt; + 1.&lt;br /&gt;
&lt;br /&gt;
Find the last ten digits of this prime number.&lt;br /&gt;
&lt;br /&gt;
==Why this problem==&lt;br /&gt;
&lt;br /&gt;
This problem is a classic exercise in modular arithmetic. The prime number in question has over two million digits, making it impossible to compute directly using standard integer types. The key insight is that only the last ten digits are required, which means we can work entirely modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;. This problem demonstrates the power of modular exponentiation — specifically, the technique of exponentiation by squaring (also known as binary exponentiation) — to efficiently compute enormous powers under a modulus.&lt;br /&gt;
&lt;br /&gt;
=Solution Approach=&lt;br /&gt;
&lt;br /&gt;
==Mathematical Reduction==&lt;br /&gt;
&lt;br /&gt;
Let the number in question be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
N = 28433 \times 2^{7830457} + 1&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We are asked for the last ten digits of N, which is equivalent to computing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
N \bmod 10^{10}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because congruences respect both multiplication and addition, we can reduce the power first:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
N \equiv 28433 \times \left( 2^{7830457} \bmod 10^{10} \right) + 1 \pmod{10^{10}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
P \equiv 2^{7830457} \pmod{10^{10}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then the answer is simply:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
(28433 \times P + 1) \bmod 10^{10}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The entire problem reduces to computing one value: the residue of 2&amp;lt;sup&amp;gt;7830457&amp;lt;/sup&amp;gt; modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Binary Exponentiation (Exponentiation by Squaring)==&lt;br /&gt;
&lt;br /&gt;
The exponent 7830457 is large (it would produce over 2.3 million decimal digits if fully expanded), but it has only 23 binary digits. Binary exponentiation processes the exponent by repeated halving, requiring only about &amp;lt;math&amp;gt;\lfloor \log_2 7830457 \rfloor + 1 = 23&amp;lt;/math&amp;gt; iterations.&lt;br /&gt;
&lt;br /&gt;
The algorithm maintains three values:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;result&amp;#039;&amp;#039;&amp;#039; r (initialized to 1)&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;base&amp;#039;&amp;#039;&amp;#039; b (initialized to 2 mod 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;)&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;exponent&amp;#039;&amp;#039;&amp;#039; e (initialized to 7830457)&lt;br /&gt;
&lt;br /&gt;
At each step:&lt;br /&gt;
&lt;br /&gt;
# If e is odd, multiply r by b (modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;)&lt;br /&gt;
# Square b (modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;)&lt;br /&gt;
# Halve e (integer division by 2)&lt;br /&gt;
&lt;br /&gt;
The loop invariant is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
r \times b^{e} \equiv 2^{7830457} \pmod{10^{10}}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the loop terminates (e = 0), the accumulated result r is exactly the desired residue P.&lt;br /&gt;
&lt;br /&gt;
==Why Not Euler&amp;#039;s Theorem?==&lt;br /&gt;
&lt;br /&gt;
The modulus is &amp;lt;math&amp;gt;10^{10} = 2^{10} \times 5^{10}&amp;lt;/math&amp;gt;. Since the base (2) shares a common factor with the modulus, &amp;lt;math&amp;gt;\gcd(2, 10^{10}) = 2 \neq 1&amp;lt;/math&amp;gt;, Euler&amp;#039;s theorem cannot be applied directly to reduce the exponent modulo &amp;lt;math&amp;gt;\varphi(10^{10})&amp;lt;/math&amp;gt;. The robust approach of repeated squaring with modular reduction at every step works regardless.&lt;br /&gt;
&lt;br /&gt;
==Complexity==&lt;br /&gt;
&lt;br /&gt;
Binary exponentiation uses &amp;lt;math&amp;gt;O(\log e)&amp;lt;/math&amp;gt; modular multiplications. With only ~23 iterations and at most one extra modular multiplication per iteration, the algorithm is extremely fast. Memory usage is &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt; — only a handful of integers are stored, and each is immediately reduced modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Java Solution=&lt;br /&gt;
&lt;br /&gt;
The Java implementation uses &amp;lt;code&amp;gt;java.math.BigInteger&amp;lt;/code&amp;gt; for arbitrary-precision arithmetic, specifically leveraging the built-in &amp;lt;code&amp;gt;modPow()&amp;lt;/code&amp;gt; method which implements binary exponentiation natively.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.math.BigInteger;&lt;br /&gt;
&lt;br /&gt;
public class Problem097 {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        BigInteger MOD = BigInteger.TEN.pow(10);&lt;br /&gt;
&lt;br /&gt;
        // Compute 2^7830457 mod 10^10 using built-in modular exponentiation&lt;br /&gt;
        BigInteger powerOfTwo = BigInteger.valueOf(2)&lt;br /&gt;
            .modPow(BigInteger.valueOf(7830457), MOD);&lt;br /&gt;
&lt;br /&gt;
        // Apply the formula: (28433 * powerOfTwo + 1) mod 10^10&lt;br /&gt;
        BigInteger result = powerOfTwo&lt;br /&gt;
            .multiply(BigInteger.valueOf(28433))&lt;br /&gt;
            .add(BigInteger.ONE)&lt;br /&gt;
            .mod(MOD);&lt;br /&gt;
&lt;br /&gt;
        // Print with leading zeros to ensure exactly 10 digits&lt;br /&gt;
        System.out.println(String.format(&amp;quot;%010d&amp;quot;, result));&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Explanation of the Code==&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Modulus setup&amp;#039;&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;BigInteger.TEN.pow(10)&amp;lt;/code&amp;gt; creates the modulus 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt; = 10000000000.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Modular exponentiation&amp;#039;&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;BigInteger.valueOf(2).modPow(BigInteger.valueOf(7830457), MOD)&amp;lt;/code&amp;gt; computes 2&amp;lt;sup&amp;gt;7830457&amp;lt;/sup&amp;gt; mod 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt; using the JVM&amp;#039;s highly optimized binary exponentiation.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Apply the formula&amp;#039;&amp;#039;&amp;#039;: Multiply by 28433, add 1, and reduce modulo 10&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt; one final time.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Format output&amp;#039;&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;String.format(&amp;quot;%010d&amp;quot;, result)&amp;lt;/code&amp;gt; ensures the output is zero-padded to exactly 10 digits (the leading digits could be zeros, and they must be included).&lt;br /&gt;
&lt;br /&gt;
{{ProjectEulerFlag}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Prime Numbers]]&lt;br /&gt;
[[Category:Modular Arithmetic]]&lt;br /&gt;
[[Category:Number Theory]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>