Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. internal interface Solution
  8. {
  9. /// <summary>
  10. /// Simple interface for solutions to math problems with 2 factors
  11. /// </summary>
  12. int Factor1 { get; set; }
  13. int Factor2 { get; set; }
  14. int Target { get; set; }
  15. double DistanceToTarget { get; }
  16. string Visualization { get; }
  17. }
  18.  
  19. internal class Multiplication : Solution
  20. {
  21. /// <summary>
  22. /// Multiplies factor1 and factor 2
  23. /// </summary>
  24. public int Factor1 { get; set; }
  25. public int Factor2 { get; set; }
  26. public int Target { get; set; }
  27. public double DistanceToTarget => System.Math.Abs(Target - (Factor1 * Factor2));
  28. public string Visualization => $"{Factor1} * {Factor2} = {Factor1 * Factor2}";
  29. }
  30.  
  31. internal class Division : Solution
  32. {
  33. /// <summary>
  34. /// Divides factor1 over factor 2
  35. /// </summary>
  36. public int Factor1 { get; set; }
  37. public int Factor2 { get; set; }
  38. public int Target { get; set; }
  39. public double DistanceToTarget => System.Math.Abs(Target - (Factor1 / Factor2));
  40. public string Visualization => $"{Factor1} / {Factor2} = {Factor1 / Factor2}";
  41. }
  42.  
  43. internal class Addition : Solution
  44. {
  45. /// <summary>
  46. /// Adds factor1 to factor 2
  47. /// </summary>
  48. public int Factor1 { get; set; }
  49. public int Factor2 { get; set; }
  50. public int Target { get; set; }
  51. public double DistanceToTarget => System.Math.Abs(Target - (Factor1 + Factor2));
  52. public string Visualization => $"{Factor1} + {Factor2} = {Factor1 + Factor2}";
  53. }
  54.  
  55. internal class Subtraction : Solution
  56. {
  57. /// <summary>
  58. /// Subtracts factor2 from factor1
  59. /// </summary>
  60. public int Factor1 { get; set; }
  61. public int Factor2 { get; set; }
  62. public int Target { get; set; }
  63. public double DistanceToTarget => System.Math.Abs(Target - (Factor1 - Factor2));
  64. public string Visualization => $"{Factor1} - {Factor2} = {Factor1 - Factor2}";
  65. }
  66.  
  67. class Program
  68. {
  69. /// <summary>
  70. /// Simple program that tries to find solutions that come as close as posible to a target number.
  71. /// </summary>
  72. /// <param name="args"></param>
  73. static void Main(string[] args)
  74. {
  75. // The factors we'll use
  76. var factors = new[] { 1, 3, 3, 7, 10, 5 };
  77. // The number we're trying to be close to
  78. const int target = 999;
  79. // Our solutions
  80. var solutions = new List<Solution>();
  81. // The factors we've already used so we won't end up with duplicate solutions
  82. var usedFactors = new List<int>();
  83.  
  84. foreach (var factor1 in factors)
  85. {
  86. // Avoiding duplicate solutions
  87. foreach (var factor2 in factors.Where(x => !usedFactors.Contains(x)))
  88. {
  89. // Add each type of solution
  90. solutions.AddRange(new Solution[]
  91. {
  92. // A constructor would be nice, but whatever.
  93. new Multiplication { Factor1 = factor1, Factor2 = factor2, Target = target },
  94. new Division { Factor1 = factor1, Factor2 = factor2, Target = target },
  95. new Addition { Factor1 = factor1, Factor2 = factor2, Target = target },
  96. new Subtraction { Factor1 = factor1, Factor2 = factor2, Target = target }
  97. });
  98. }
  99.  
  100. usedFactors.Add(factor1);
  101. }
  102.  
  103. foreach (var solution in solutions.OrderBy(x => x.DistanceToTarget).Take(10))
  104. {
  105. Console.WriteLine(solution.Visualization);
  106. }
  107. Console.WriteLine("Press any key to exit");
  108. Console.ReadKey();
  109. }
  110. }
  111. }
Add Comment
Please, Sign In to add comment