Created
August 19, 2010 02:25
-
-
Save shinyzhu/536820 to your computer and use it in GitHub Desktop.
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
/// <summary> | |
/// Solution for calculate xy=total, x-y -> 0. | |
/// Reference: http://www.wolframalpha.com/input/?i=xy%3D200,+x%3E0 | |
/// </summary> | |
/// <param name="total">The integer to calculate.</param> | |
/// <returns>x,y pair, x <= y.</returns> | |
public static KeyValuePair<int, int> MinXPlusY(int total) | |
{ | |
var allXplusY = Enumerable.Range(1, total) | |
.Where(i => total % i == 0 && i <= total / i) | |
.ToDictionary(i => i, i => total / i); | |
var min = allXplusY | |
.Select(d => d.Value - d.Key) | |
.Min(); | |
//xy is what you finding. | |
return allXplusY.FirstOrDefault(d => d.Value - d.Key == min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment