Skip to content

Instantly share code, notes, and snippets.

@aannenko
Created December 5, 2024 09:39
Show Gist options
  • Save aannenko/c2da387515b4d019579a48b1c97b57e8 to your computer and use it in GitHub Desktop.
Save aannenko/c2da387515b4d019579a48b1c97b57e8 to your computer and use it in GitHub Desktop.
Find all positive solutions to the equation a^3 + b^3 = c^3 + d^3 where a, b, c, and d are integers between 1 and 1000.
// Find all positive solutions to the equation a^3 + b^3 = c^3 + d^3
// where a, b, c, and d are integers between 1 and 1000.
FindCubicSumPairs(1, 1000).Dump();
static IEnumerable<(int A, int B, int C, int D)> FindCubicSumPairs(int min, int max)
{
var sumToPairs = new Dictionary<double, List<(int, int)>>();
for (int a = min; a <= max; a++)
{
for (int b = min; b <= max; b++)
{
var sum = Math.Pow(a, 3) + Math.Pow(b, 3);
if (!sumToPairs.TryGetValue(sum, out var pairs))
sumToPairs[sum] = pairs = new();
pairs.Add((a, b));
}
}
foreach (var list in sumToPairs.Values)
{
foreach (var (a, b) in list)
{
foreach (var (c, d) in list)
{
yield return (a, b, c, d);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment