<p>在第一个八分体<code>y>0</code>,<code>x<y</code>内搜索就足够了(四个解<code>(±n, ±n)</code>很明显,通过对称,一个解<code>(x, y)</code>产生8个拷贝<code>(±x, ±y)</code>,<code>(±y, ±x)</code>)。你知道吗</p>
<p>根据单调性,对于一个给定的<code>y</code>,至多有一个<code>x</code>。您可以通过循序渐进地跟随圆弧,减少<code>y</code>,然后调整<code>x</code>来找到它。如果您尽可能严格地维护条件<code>x²+y²≤2n²</code>,那么下面的代码将被优化为只使用基本整数算术(为了效率,使用<code>2x</code>而不是<code>x</code>)。你知道吗</p>
<pre><code> x, y, d= 2 * n, 2 * n, 0
while y > 0:
y, d= y - 2, d - y + 1
if d < 0:
x, d= x + 2, d + x + 1
if d == 0:
print(x >> 1, '² + ', y >> 1, '² = 2.', n, '²', sep='')
</code></pre>
<p>以下是<code>1</code>和<code>100</code>之间<code>n</code>的所有解决方案:</p>
<pre><code>7² + 1² = 2.5²
14² + 2² = 2.10²
17² + 7² = 2.13²
21² + 3² = 2.15²
23² + 7² = 2.17²
28² + 4² = 2.20²
31² + 17² = 2.25²
35² + 5² = 2.25²
34² + 14² = 2.26²
41² + 1² = 2.29²
42² + 6² = 2.30²
46² + 14² = 2.34²
49² + 7² = 2.35²
47² + 23² = 2.37²
51² + 21² = 2.39²
56² + 8² = 2.40²
49² + 31² = 2.41²
63² + 9² = 2.45²
62² + 34² = 2.50²
70² + 10² = 2.50²
69² + 21² = 2.51²
68² + 28² = 2.52²
73² + 17² = 2.53²
77² + 11² = 2.55²
82² + 2² = 2.58²
84² + 12² = 2.60²
71² + 49² = 2.61²
79² + 47² = 2.65²
85² + 35² = 2.65²
89² + 23² = 2.65²
91² + 13² = 2.65²
92² + 28² = 2.68²
98² + 14² = 2.70²
103² + 7² = 2.73²
94² + 46² = 2.74²
93² + 51² = 2.75²
105² + 15² = 2.75²
102² + 42² = 2.78²
112² + 16² = 2.80²
98² + 62² = 2.82²
97² + 71² = 2.85²
113² + 41² = 2.85²
115² + 35² = 2.85²
119² + 17² = 2.85²
123² + 3² = 2.87²
119² + 41² = 2.89²
126² + 18² = 2.90²
119² + 49² = 2.91²
133² + 19² = 2.95²
137² + 7² = 2.97²
124² + 68² = 2.100²
140² + 20² = 2.100²
</code></pre>