Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
76
Task/Taxicab-numbers/C-sharp/taxicab-numbers-1.cs
Normal file
76
Task/Taxicab-numbers/C-sharp/taxicab-numbers-1.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TaxicabNumber
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
IDictionary<long, IList<Tuple<int, int>>> taxicabNumbers = GetTaxicabNumbers(2006);
|
||||
PrintTaxicabNumbers(taxicabNumbers);
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
private static IDictionary<long, IList<Tuple<int, int>>> GetTaxicabNumbers(int length)
|
||||
{
|
||||
SortedList<long, IList<Tuple<int, int>>> sumsOfTwoCubes = new SortedList<long, IList<Tuple<int, int>>>();
|
||||
|
||||
for (int i = 1; i < int.MaxValue; i++)
|
||||
{
|
||||
for (int j = 1; j < int.MaxValue; j++)
|
||||
{
|
||||
long sum = (long)(Math.Pow((double)i, 3) + Math.Pow((double)j, 3));
|
||||
|
||||
if (!sumsOfTwoCubes.ContainsKey(sum))
|
||||
{
|
||||
sumsOfTwoCubes.Add(sum, new List<Tuple<int, int>>());
|
||||
}
|
||||
|
||||
sumsOfTwoCubes[sum].Add(new Tuple<int, int>(i, j));
|
||||
|
||||
if (j >= i)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Found that you need to keep going for a while after the length, because higher i values fill in gaps
|
||||
if (sumsOfTwoCubes.Count(t => t.Value.Count >= 2) >= length * 1.1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
IDictionary<long, IList<Tuple<int, int>>> values = (from t in sumsOfTwoCubes where t.Value.Count >= 2 select t)
|
||||
.Take(2006)
|
||||
.ToDictionary(u => u.Key, u => u.Value);
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
private static void PrintTaxicabNumbers(IDictionary<long, IList<Tuple<int, int>>> values)
|
||||
{
|
||||
int i = 1;
|
||||
|
||||
foreach (long taxicabNumber in values.Keys)
|
||||
{
|
||||
StringBuilder output = new StringBuilder().AppendFormat("{0,10}\t{1,4}", i, taxicabNumber);
|
||||
|
||||
foreach (Tuple<int, int> numbers in values[taxicabNumber])
|
||||
{
|
||||
output.AppendFormat("\t= {0}^3 + {1}^3", numbers.Item1, numbers.Item2);
|
||||
}
|
||||
|
||||
if (i <= 25 || (i >= 2000 && i <= 2006))
|
||||
{
|
||||
Console.WriteLine(output.ToString());
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Task/Taxicab-numbers/C-sharp/taxicab-numbers-2.cs
Normal file
35
Task/Taxicab-numbers/C-sharp/taxicab-numbers-2.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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); }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue