Created
December 5, 2024 09:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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