<?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%2F44</id>
	<title>Project Euler/44 - 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%2F44"/>
	<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/44&amp;action=history"/>
	<updated>2026-06-18T23:39:07Z</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/44&amp;diff=30593&amp;oldid=prev</id>
		<title>Admin: Create Project Euler/44 page with problem statement, approach, and solution link (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://charlesreid1.com/w/index.php?title=Project_Euler/44&amp;diff=30593&amp;oldid=prev"/>
		<updated>2026-06-16T14:24:48Z</updated>

		<summary type="html">&lt;p&gt;Create Project Euler/44 page with problem statement, approach, and solution link (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;==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
Pentagonal numbers are generated by the formula:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
P_n = \frac{n(3n-1)}{2}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first ten pentagonal numbers are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, \dots&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be seen that &amp;lt;math&amp;gt;P_4 + P_7 = 22 + 70 = 92 = P_8&amp;lt;/math&amp;gt;. However, their difference, &amp;lt;math&amp;gt;70 - 22 = 48&amp;lt;/math&amp;gt;, is not pentagonal.&lt;br /&gt;
&lt;br /&gt;
Find the pair of pentagonal numbers, &amp;lt;math&amp;gt;P_j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;P_k&amp;lt;/math&amp;gt;, for which their sum and difference are both pentagonal and &amp;lt;math&amp;gt;D = |P_k - P_j|&amp;lt;/math&amp;gt; is minimised; what is the value of &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
==Approach==&lt;br /&gt;
&lt;br /&gt;
===Pentagonal Number Generation===&lt;br /&gt;
&lt;br /&gt;
Pentagonal numbers are generated directly from the closed-form formula &amp;lt;math&amp;gt;P_n = \frac{n(3n-1)}{2}&amp;lt;/math&amp;gt;. The solution generates these on the fly rather than pre-computing a fixed set, since the search bound is not known in advance.&lt;br /&gt;
&lt;br /&gt;
===Inverse Pentagonal Check===&lt;br /&gt;
&lt;br /&gt;
To test whether a number &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is pentagonal, solve the quadratic equation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
\frac{n(3n-1)}{2} = x \implies 3n^2 - n - 2x = 0&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Applying the quadratic formula yields:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&lt;br /&gt;
n = \frac{1 + \sqrt{1 + 24x}}{6}&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is a positive integer, then &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; is pentagonal. The implementation truncates the result to a long and verifies that plugging it back into the pentagonal formula reproduces &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Search Strategy===&lt;br /&gt;
&lt;br /&gt;
The algorithm uses a doubly-nested loop over indices, generating pentagonal numbers incrementally:&lt;br /&gt;
&lt;br /&gt;
* Outer loop: iterate &amp;lt;math&amp;gt;n = 1, 2, 3, \dots&amp;lt;/math&amp;gt; to produce &amp;lt;math&amp;gt;P_n&amp;lt;/math&amp;gt; (the larger pentagonal number, &amp;lt;math&amp;gt;P_j&amp;lt;/math&amp;gt;).&lt;br /&gt;
* Inner loop: for each &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, iterate &amp;lt;math&amp;gt;k = n-1&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;, producing &amp;lt;math&amp;gt;P_k&amp;lt;/math&amp;gt; (the smaller candidate).&lt;br /&gt;
* Compute the sum &amp;lt;math&amp;gt;S = P_n + P_k&amp;lt;/math&amp;gt; and difference &amp;lt;math&amp;gt;D = P_n - P_k&amp;lt;/math&amp;gt;.&lt;br /&gt;
* If both &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; are pentagonal, return &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; as the answer.&lt;br /&gt;
&lt;br /&gt;
Because the outer loop increases the larger pentagonal number &amp;lt;math&amp;gt;P_n&amp;lt;/math&amp;gt; monotonically, the first valid pair encountered minimizes &amp;lt;math&amp;gt;D&amp;lt;/math&amp;gt; — any later pair would involve a larger &amp;lt;math&amp;gt;P_n&amp;lt;/math&amp;gt; and therefore a potentially larger difference.&lt;br /&gt;
&lt;br /&gt;
===Optimization Notes===&lt;br /&gt;
&lt;br /&gt;
* The inner loop counts &amp;#039;&amp;#039;&amp;#039;downward&amp;#039;&amp;#039;&amp;#039; from &amp;lt;math&amp;gt;n-1&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;, checking larger values of &amp;lt;math&amp;gt;P_k&amp;lt;/math&amp;gt; first. This tends to find small differences sooner.&lt;br /&gt;
* The inverse pentagonal check uses floating-point &amp;lt;code&amp;gt;Math.sqrt&amp;lt;/code&amp;gt; and is O(1), making the overall search efficient without precomputed hash tables.&lt;br /&gt;
&lt;br /&gt;
==Solution==&lt;br /&gt;
&lt;br /&gt;
Link: https://git.charlesreid1.com/cs/euler/src/branch/main/java/Problem044.java&lt;br /&gt;
&lt;br /&gt;
==Flags==&lt;br /&gt;
&lt;br /&gt;
{{ProjectEulerFlag}}&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>