TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Write all solutions for a^3 + b^3 = c^3 + d^3

2 pointsby saltcookiealmost 11 years ago
this runs the fastest without memory overhead<p>int Search(){ int MAX = 10000000; for(int a = 0; a &lt; MAX; a++){ int a3 = a * a * a; if(a3 &gt; MAX) break; for(int b = a; b &lt; MAX; b ++){ int b3 = b * b * b; if(a3 + b3 &gt; MAX)break; for(int c = 0; c &lt; a; c++){ int c3 = c<i>c</i>c; int m = a3 - c3; int d = b+1; while(true){ int d3 = d * d * d; if(d3-b3 &lt;= m){ if((d3 - b3) == m){ count++; PUSH_SOLUTION(a3, b3, c3, b3, a, b, c, d); } d++; continue; } else break; } } } } return 0; }

2 comments

dalkealmost 11 years ago
As one of the comments points out, these are the so-called &quot;taxicab numbers&quot;, based on a story about Hardy trying to make small talk with Ramanujan. Hardy&#x27;s taxicab&#x27;s number was 1729, which he found boring. Ramanujan pointed out it was the first number which was the sum of two different cubes in two different ways.<p>There are six known taxicab numbers; see <a href="http://en.wikipedia.org/wiki/Taxicab_number" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Taxicab_number</a> for a list and their decompositions. The solutions at <a href="http://math.stackexchange.com/questions/2815/find-taxicab-numbers-in-on-time" rel="nofollow">http:&#x2F;&#x2F;math.stackexchange.com&#x2F;questions&#x2F;2815&#x2F;find-taxicab-nu...</a> are much more knowledgeable about math than the ones at StackOverflow, as one might expect.<p>Note that in your&#x2F;saltcookie&#x27;s proposed answer there is mistranslation of the problem. The posed question says &quot;a, b, c, d lie between [0, 10^5]&quot; but does not restrict their cubes, while you have a test for &quot;if(a3 &gt; MAX)&quot; and &quot;if(a3 + b3 &gt; MAX)&quot;.<p>Even if those checks are removed, there is still a likely overflow error because most systems use 32 bit integers for &#x27;int&#x27;. a * a * a as an int may be a number &lt;= 10000000, including a negative number.<p>That is, your output should include 48988659276962496 = 38787^3 + 365757^3 as an answer because 38787 &lt; 365757 &lt; 10000000 so fits the input constraints; note that 48988659276962496 cannot be represented as a 32 bit integer.
saltcookiealmost 11 years ago
<a href="http://stackoverflow.com/a/20901679/924801" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;20901679&#x2F;924801</a>