Taxicab numbers: Difference between revisions

→‎{{header|C sharp}}: added alternate algorithm
m (use wiki markup for links)
(→‎{{header|C sharp}}: added alternate algorithm)
Line 269:
}
}</lang>
 
===Alternate Algorithm===
Based on the second Python example where only the sums are stored and sorted. Also shows the first 10 Taxicab Number triples.
<lang csharp>using System; using static System.Console;
using System.Collections.Generic; using System.Linq;
 
class Program {
 
static void Main(string[] args) {
 
List<uint> cubes = new List<uint>(), sums = new List<uint>();
 
void dump(string title, Dictionary <int, uint> items) {
Write(title); foreach (var item in items) {
Write("\n{0,4} {1,10}", item.Key, item.Value);
foreach (uint x in cubes) { uint y = item.Value - x;
if (y < x) break; if (cubes.Contains(y))
Write(" = {0,4}³ + {1,3}³", cubes.IndexOf(y), cubes.IndexOf(x));
} } }
 
DateTime st = DateTime.Now;
// create sorted list of cube sums
for (uint i = 0, cube; i < 1190; i++) { cube = i * i * i;
cubes.Add(cube); foreach (uint j in cubes)
sums.Add(cube + j); } sums.Sort();
// now seek consecutive sums that match
uint nm1 = sums[0], n = sums[1]; int idx = 0;
Dictionary <int, uint> task = new Dictionary <int, uint>(),
trips = new Dictionary <int, uint>();
foreach (var np1 in sums.Skip(2)) {
if (nm1 == np1) trips.Add(idx, n); if (nm1 != n && n == np1)
if (++idx <= 25 || idx >= 2000 == idx <= 2006)
task.Add(idx, n); nm1 = n; n = np1; }
// show results
dump("First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:", task);
dump(string.Format("\n\nFound {0} triple Taxicabs under {1}:", trips.Count, 2007), trips);
Write("\n\nElasped: {0}ms", (DateTime.Now - st).TotalMilliseconds); }
}</lang>
{{out}} (from TIO.run)
<pre>First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:
1 1729 = 12³ + 1³ = 10³ + 9³
2 4104 = 16³ + 2³ = 15³ + 9³
3 13832 = 24³ + 2³ = 20³ + 18³
4 20683 = 27³ + 10³ = 24³ + 19³
5 32832 = 32³ + 4³ = 30³ + 18³
6 39312 = 34³ + 2³ = 33³ + 15³
7 40033 = 34³ + 9³ = 33³ + 16³
8 46683 = 36³ + 3³ = 30³ + 27³
9 64232 = 39³ + 17³ = 36³ + 26³
10 65728 = 40³ + 12³ = 33³ + 31³
11 110656 = 48³ + 4³ = 40³ + 36³
12 110808 = 48³ + 6³ = 45³ + 27³
13 134379 = 51³ + 12³ = 43³ + 38³
14 149389 = 53³ + 8³ = 50³ + 29³
15 165464 = 54³ + 20³ = 48³ + 38³
16 171288 = 55³ + 17³ = 54³ + 24³
17 195841 = 58³ + 9³ = 57³ + 22³
18 216027 = 60³ + 3³ = 59³ + 22³
19 216125 = 60³ + 5³ = 50³ + 45³
20 262656 = 64³ + 8³ = 60³ + 36³
21 314496 = 68³ + 4³ = 66³ + 30³
22 320264 = 68³ + 18³ = 66³ + 32³
23 327763 = 67³ + 30³ = 58³ + 51³
24 373464 = 72³ + 6³ = 60³ + 54³
25 402597 = 69³ + 42³ = 61³ + 56³
2000 1671816384 = 1168³ + 428³ = 944³ + 940³
2001 1672470592 = 1187³ + 29³ = 1124³ + 632³
2002 1673170856 = 1164³ + 458³ = 1034³ + 828³
2003 1675045225 = 1153³ + 522³ = 1081³ + 744³
2004 1675958167 = 1159³ + 492³ = 1096³ + 711³
2005 1676926719 = 1188³ + 63³ = 1095³ + 714³
2006 1677646971 = 1188³ + 99³ = 990³ + 891³
 
Found 10 triple Taxicabs under 2007:
455 87539319 = 436³ + 167³ = 423³ + 228³ = 414³ + 255³
535 119824488 = 493³ + 11³ = 492³ + 90³ = 428³ + 346³
588 143604279 = 522³ + 111³ = 460³ + 359³ = 423³ + 408³
655 175959000 = 560³ + 70³ = 552³ + 198³ = 525³ + 315³
888 327763000 = 670³ + 300³ = 661³ + 339³ = 580³ + 510³
1299 700314552 = 872³ + 334³ = 846³ + 456³ = 828³ + 510³
1398 804360375 = 930³ + 15³ = 927³ + 198³ = 920³ + 295³
1515 958595904 = 986³ + 22³ = 984³ + 180³ = 856³ + 692³
1660 1148834232 = 1044³ + 222³ = 920³ + 718³ = 846³ + 816³
1837 1407672000 = 1120³ + 140³ = 1104³ + 396³ = 1050³ + 630³
 
Elasped: 78.7948ms</pre>
 
=={{header|Clojure}}==