Taxicab numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Lua}}: added Lua solution)
Line 191: Line 191:
2006:1677646971 = 990^3 + 891^3 = 1188^3 + 99^3
2006:1677646971 = 990^3 + 891^3 = 1188^3 + 99^3
</pre>
</pre>

=={{header|C++}}==
{{trans|C#}}
<lang cpp>#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

template <typename T>
size_t indexOf(const std::vector<T> &v, const T &k) {
auto it = std::find(v.cbegin(), v.cend(), k);

if (it != v.cend()) {
return it - v.cbegin();
}
return -1;
}

int main() {
std::vector<size_t> cubes;

auto dump = [&cubes](const std::string &title, const std::map<int, size_t> &items) {
std::cout << title;
for (auto &item : items) {
std::cout << "\n" << std::setw(4) << item.first << " " << std::setw(10) << item.second;
for (auto x : cubes) {
auto y = item.second - x;
if (y < x) {
break;
}
if (std::count(cubes.begin(), cubes.end(), y)) {
std::cout << " = " << std::setw(4) << indexOf(cubes, y) << "^3 + " << std::setw(3) << indexOf(cubes, x) << "^3";
}
}
}
};

std::vector<size_t> sums;

// create sorted list of cube sums
for (size_t i = 0; i < 1190; i++) {
auto cube = i * i * i;
cubes.push_back(cube);
for (auto j : cubes) {
sums.push_back(cube + j);
}
}
std::sort(sums.begin(), sums.end());

// now seek consecutive sums that match
auto nm1 = sums[0];
auto n = sums[1];
int idx = 0;
std::map<int, size_t> task;
std::map<int, size_t> trips;

auto it = sums.cbegin();
auto end = sums.cend();
it++;
it++;

while (it != end) {
auto np1 = *it;

if (nm1 == np1) {
trips.emplace(idx, n);
}
if (nm1 != n && n == np1) {
if (++idx <= 25 || idx >= 2000 == idx <= 2006) {
task.emplace(idx, n);
}
}
nm1 = n;
n = np1;

it++;
}

dump("First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:", task);

std::stringstream ss;
ss << "\n\nFound " << trips.size() << " triple Taxicabs under 2007:";
dump(ss.str(), trips);

return 0;
}</lang>
{{out}}
<pre>First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:
1 1729 = 12^3 + 1^3 = 10^3 + 9^3
2 4104 = 16^3 + 2^3 = 15^3 + 9^3
3 13832 = 24^3 + 2^3 = 20^3 + 18^3
4 20683 = 27^3 + 10^3 = 24^3 + 19^3
5 32832 = 32^3 + 4^3 = 30^3 + 18^3
6 39312 = 34^3 + 2^3 = 33^3 + 15^3
7 40033 = 34^3 + 9^3 = 33^3 + 16^3
8 46683 = 36^3 + 3^3 = 30^3 + 27^3
9 64232 = 39^3 + 17^3 = 36^3 + 26^3
10 65728 = 40^3 + 12^3 = 33^3 + 31^3
11 110656 = 48^3 + 4^3 = 40^3 + 36^3
12 110808 = 48^3 + 6^3 = 45^3 + 27^3
13 134379 = 51^3 + 12^3 = 43^3 + 38^3
14 149389 = 53^3 + 8^3 = 50^3 + 29^3
15 165464 = 54^3 + 20^3 = 48^3 + 38^3
16 171288 = 55^3 + 17^3 = 54^3 + 24^3
17 195841 = 58^3 + 9^3 = 57^3 + 22^3
18 216027 = 60^3 + 3^3 = 59^3 + 22^3
19 216125 = 60^3 + 5^3 = 50^3 + 45^3
20 262656 = 64^3 + 8^3 = 60^3 + 36^3
21 314496 = 68^3 + 4^3 = 66^3 + 30^3
22 320264 = 68^3 + 18^3 = 66^3 + 32^3
23 327763 = 67^3 + 30^3 = 58^3 + 51^3
24 373464 = 72^3 + 6^3 = 60^3 + 54^3
25 402597 = 69^3 + 42^3 = 61^3 + 56^3
2000 1671816384 = 1168^3 + 428^3 = 944^3 + 940^3
2001 1672470592 = 1187^3 + 29^3 = 1124^3 + 632^3
2002 1673170856 = 1164^3 + 458^3 = 1034^3 + 828^3
2003 1675045225 = 1153^3 + 522^3 = 1081^3 + 744^3
2004 1675958167 = 1159^3 + 492^3 = 1096^3 + 711^3
2005 1676926719 = 1188^3 + 63^3 = 1095^3 + 714^3
2006 1677646971 = 1188^3 + 99^3 = 990^3 + 891^3

Found 10 triple Taxicabs under 2007:
455 87539319 = 436^3 + 167^3 = 423^3 + 228^3 = 414^3 + 255^3
535 119824488 = 493^3 + 11^3 = 492^3 + 90^3 = 428^3 + 346^3
588 143604279 = 522^3 + 111^3 = 460^3 + 359^3 = 423^3 + 408^3
655 175959000 = 560^3 + 70^3 = 552^3 + 198^3 = 525^3 + 315^3
888 327763000 = 670^3 + 300^3 = 661^3 + 339^3 = 580^3 + 510^3
1299 700314552 = 872^3 + 334^3 = 846^3 + 456^3 = 828^3 + 510^3
1398 804360375 = 930^3 + 15^3 = 927^3 + 198^3 = 920^3 + 295^3
1515 958595904 = 986^3 + 22^3 = 984^3 + 180^3 = 856^3 + 692^3
1660 1148834232 = 1044^3 + 222^3 = 920^3 + 718^3 = 846^3 + 816^3
1837 1407672000 = 1120^3 + 140^3 = 1104^3 + 396^3 = 1050^3 + 630^3</pre>


=={{header|C sharp}}==
=={{header|C sharp}}==