Skip to content

Instantly share code, notes, and snippets.

@shinyzhu
Created August 19, 2010 02:25
Show Gist options
  • Save shinyzhu/536820 to your computer and use it in GitHub Desktop.
Save shinyzhu/536820 to your computer and use it in GitHub Desktop.
/// <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 &lt;= 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