Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong>: for those who do not know what R is, here is a <a href="http://www.r-project.org/" rel="noreferrer">link</a>.</p> <p>My C being a bit rusty... I will write a solution in R, it should not be difficult to adapt. The solution runs very fast in R, so it should be even faster in C.</p> <pre><code># Create an hash table of cubes from 1 to 100 numbers &lt;- 1:100 cubes &lt;- numbers ^ 3 # The possible pairs of numbers pairs &lt;- combn(numbers, 2) # Now sum the cubes of the combinations # This takes every couple and sums the values of the cubes # with the appropriate index sums &lt;- apply(pairs, 2, function(x){sum(cubes[x])}) </code></pre> <p>So:</p> <p><code>numbers</code> will be: <code>1, 2, 3, 4, ..., 98, 99, 100</code><br> <code>cubes</code> will be: <code>1, 8, 27, 64, ..., 941192, 970299, 1000000</code><br> <code>pairs</code> will contain:</p> <pre><code> [,1] [,2] [,3] [,4] [,5] ... [,4949] [,4950] [1,] 1 1 1 1 1 ... 98 99 [2,] 2 3 4 5 6 ... 100 100 </code></pre> <p><code>sums</code> will be: <code>9 28 65 126 217 344 ... 1911491 1941192 1970299</code></p> <p>A quick check that we are on the right track...</p> <pre><code>&gt; which(sums == 1729) [1] 11 765 &lt;--- the ids of the couples summing to 1729 &gt; pairs[,11] [1] 1 12 &gt; pairs[,765] [1] 9 10 </code></pre> <p>Now, let's find which are the couples with the same sums.</p> <p><code>table(sums)</code> gives us a neat summary like</p> <pre><code>&gt; 9 28 35 65 72 91 ... 1674 1729 1736 1 1 1 1 1 1 .... &lt;lots of 1s here&gt; ... 1 2 1 </code></pre> <p>So let's just find which elements of <code>table(sums)</code> are == 2</p> <pre><code>doubles &lt;- which(table(sums) == 2) taxi.numbers &lt;- as.integer(names(doubles)) [1] 1729 4104 13832 20683 32832 39312 40033 46683 64232 65728 [11] 110656 110808 134379 149389 165464 171288 195841 216027 216125 262656 [21] 314496 320264 327763 373464 402597 439101 443889 513000 513856 515375 [31] 525824 558441 593047 684019 704977 805688 842751 885248 886464 920673 [41] 955016 984067 994688 1009736 1016496 </code></pre> <p>And finally (to be read two-by-two), the corresponding integer pairs</p> <pre><code>&gt; pairs[,doubles] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [1,] 1 9 2 9 2 18 10 19 4 18 2 15 9 16 3 [2,] 12 10 16 15 24 20 27 24 32 30 34 33 34 33 36 [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [1,] 27 17 26 12 31 4 36 6 27 12 38 8 29 20 [2,] 30 39 36 40 33 48 40 48 45 51 43 53 50 54 [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [1,] 38 17 24 9 22 3 22 5 45 8 36 4 30 18 [2,] 48 55 54 58 57 60 59 60 50 64 60 68 66 68 [,44] [,45] [,46] [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57] [1,] 32 30 51 6 54 42 56 5 48 17 38 10 45 34 [2,] 66 67 58 72 60 69 61 76 69 76 73 80 75 78 [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [1,] 52 15 54 24 62 30 57 7 63 51 64 2 41 11 [2,] 72 80 71 80 66 81 72 84 70 82 75 89 86 93 [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [1,] 30 23 63 8 72 12 54 20 33 24 63 35 59 29 [2,] 92 94 84 96 80 96 90 97 96 98 89 98 92 99 [,86] [,87] [,88] [,89] [,90] [1,] 60 50 59 47 66 [2,] 92 96 93 97 90 </code></pre> <p>So:</p> <p>1,12 and 9,10 give 1729<br> 2,16 and 9,15 give 4104<br> 2,24 and 18,20 give 13832<br> and so on!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload