public void GivenInvalidMultiplesToDouble_ThrowsArgumentException() { var range = new Range { Min = 1, Max = 5 }; int[] multiplesToDouble = null; Assert.Throws<ArgumentNullException>(() => SummingAlgorithm.SumWithDoubling(range, multiplesToDouble)); }
public void GivenInvalidRange_ThrowsArgumentException() { var range = new Range { Min = 1, Max = 0 }; var multiplesToDouble = new int[] { 3, 5 }; Assert.Throws<ArgumentException>(() => SummingAlgorithm.SumWithDoubling(range, multiplesToDouble)); }
public void GivenRange1To1000AndDoubles3And5_ReturnsTotalOf734668() { var range = new Range { Min = 1, Max = 1000 }; var multiplesToDouble = new int[] { 3, 5 }; var sum = SummingAlgorithm.SumWithDoubling(range, multiplesToDouble); Assert.That(sum, Is.EqualTo(734668)); }
public void GivenRange1To5AndDoublesNone_ReturnsTotalOf15() { var range = new Range { Min = 1, Max = 5 }; var multiplesToDouble = new int[] {}; var sum = SummingAlgorithm.SumWithDoubling(range, multiplesToDouble); Assert.That(sum, Is.EqualTo(15)); }
static void Main(string[] args) { var range = new Range { Min = 1, Max = 1000 }; var multiplesToDouble = new int[] { 3, 5 }; var sum = SummingAlgorithm.SumWithDoubling(range, multiplesToDouble); Console.WriteLine("The solution is: " + sum); Console.ReadKey(); }
public static int SumWithDoubling(Range range, int[] multiplesToDouble) { if (range == null) throw new ArgumentNullException("range", "The range parameter should not be null."); if (multiplesToDouble == null) throw new ArgumentNullException("multiplesToDouble", "The multiplesToDouble parameter should not be null."); if (range.Min > range.Max) throw new ArgumentException("The minimum range should not be greater than the maximum range.", "range"); var rangeCount = range.Max - range.Min + 1; return Enumerable.Range(range.Min, rangeCount) .Select(num => multiplesToDouble.Any(multiple => num % multiple == 0) ? num * 2 : num) .Sum(); }