-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify sum-of-multiples exercise. Fixes #65
- Loading branch information
1 parent
2677324
commit 95f9b2f
Showing
2 changed files
with
20 additions
and
50 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,12 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public class SumOfMultiples | ||
public static class SumOfMultiples | ||
{ | ||
private List<int> multiples; | ||
|
||
public SumOfMultiples() | ||
{ | ||
multiples = new List<int> { 5, 3 }; | ||
} | ||
|
||
public SumOfMultiples(IEnumerable<int> multiplesToCheck) | ||
{ | ||
multiples = multiplesToCheck.ToList(); | ||
} | ||
|
||
public int To(int limit) | ||
{ | ||
return Enumerable.Range(1, limit - 1).Where(IsMultiple).Sum(); | ||
} | ||
|
||
private bool IsMultiple(int input) | ||
public static int To(IEnumerable<int> multiples, int max) | ||
{ | ||
return multiples.Any(multiple => input % multiple == 0); | ||
return Enumerable.Range(1, max - 1) | ||
.Where(i => multiples.Any(m => i % m == 0)) | ||
.Sum(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,47 @@ | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
[TestFixture] | ||
public class SumOfMultiplesTest | ||
{ | ||
private SumOfMultiples sumOfMultiples; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
sumOfMultiples = new SumOfMultiples(); | ||
} | ||
|
||
[Test] | ||
public void Sum_to_1() | ||
{ | ||
Assert.That(sumOfMultiples.To(1), Is.EqualTo(0)); | ||
Assert.That(SumOfMultiples.To(new[] { 3, 5 }, 1), Is.EqualTo(0)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Sum_to_3() | ||
{ | ||
Assert.That(sumOfMultiples.To(4), Is.EqualTo(3)); | ||
Assert.That(SumOfMultiples.To(new[] { 3, 5 }, 4), Is.EqualTo(3)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Sum_to_10() | ||
{ | ||
Assert.That(sumOfMultiples.To(10), Is.EqualTo(23)); | ||
Assert.That(SumOfMultiples.To(new[] { 3, 5 }, 10), Is.EqualTo(23)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Sum_to_100() | ||
{ | ||
Assert.That(sumOfMultiples.To(100), Is.EqualTo(2318)); | ||
Assert.That(SumOfMultiples.To(new[] { 3, 5 }, 100), Is.EqualTo(2318)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Sum_to_1000() | ||
{ | ||
Assert.That(sumOfMultiples.To(1000), Is.EqualTo(233168)); | ||
Assert.That(SumOfMultiples.To(new[] { 3, 5 }, 1000), Is.EqualTo(233168)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Configurable_7_13_17_to_20() | ||
public void Sum_to_20() | ||
{ | ||
Assert.That(new SumOfMultiples(new List<int> { 7, 13, 17 }).To(20), Is.EqualTo(51)); | ||
Assert.That(SumOfMultiples.To(new [] { 7, 13, 17 }, 20), Is.EqualTo(51)); | ||
} | ||
|
||
[Ignore("Remove to run test")] | ||
|
||
[Test] | ||
public void Configurable_43_47_to_10000() | ||
public void Sum_to_10000() | ||
{ | ||
Assert.That(new SumOfMultiples(new List<int> { 43, 47 }).To(10000), Is.EqualTo(2203160)); | ||
Assert.That(SumOfMultiples.To(new [] { 43, 47 }, 10000), Is.EqualTo(2203160)); | ||
} | ||
} |