public class GoldbachsConjecture { public IEnumerable<int[]> TwoPrimeCalculation(int number) { if (number < 3 || number%2 != 0) return null; IList<int[]> results = new List<int[]>(); for (int i = 1; i <= number/2; i++) { if (!IsPrimeNumber(i) || !IsPrimeNumber(number - i)) continue; int[] result = new int[2]; result[0] = i; result[1] = number - i; results.Add(result); } return results; } public IEnumerable<int[]> ThreePrimeCalculation(int number) { if (number < 6 || number % 2 == 0) return null; IList<int[]> results = new List<int[]>(); for (int i = 1; i <= number / 2; i++) { if (!IsPrimeNumber(i)) continue; int remain = number - i; int startValue = i; while (true) { bool breakWhile = true; for (int j = startValue; j <= remain / 2; j++) { if (!IsPrimeNumber(j) || !IsPrimeNumber(remain - j)) continue; int[] result = new int[3]; result[0] = i; result[1] = j; result[2] = remain - j; results.Add(result); breakWhile = false; startValue = j + 1; break; } if (breakWhile) break; } } return results; } private bool IsPrimeNumber(int number) { if (number == 1) return false; for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) return false; } return true; } }
Test:
[TestFixture] public class GoldbachsConjectureTests { [TestCase(4, "2,2" )] [TestCase(6, "3,3")] [TestCase(8, "3,5")] [TestCase(12, "5,7")] public void TwoPrimeCalculationSimple(int value, string expected) { // arrange GoldbachsConjecture gc = new GoldbachsConjecture(); // act string actual = PrintResult(gc.TwoPrimeCalculation(value)); // assert Assert.AreEqual(expected, actual); } [TestCase(10, "3,7 | 5,5")] [TestCase(14, "3,11 | 7,7")] [TestCase(16, "3,13 | 5,11")] [TestCase(22, "3,19 | 5,17 | 11,11")] [TestCase(34, "3,31 | 5,29 | 11,23 | 17,17")] [TestCase(48, "5,43 | 7,41 | 11,37 | 17,31 | 19,29")] public void TwoPrimeCalculationMultiple(int value, string expected) { // arrange GoldbachsConjecture gc = new GoldbachsConjecture(); // act string actual = PrintResult(gc.TwoPrimeCalculation(value)); // assert Assert.AreEqual(expected, actual); } [TestCase(-1, "")] [TestCase(0, "")] [TestCase(1, "")] [TestCase(2, "")] [TestCase(3, "")] [TestCase(5, "")] [TestCase(9, "")] public void ErrorHandling(int value, string expected) { // arrange GoldbachsConjecture gc = new GoldbachsConjecture(); // act string actual = PrintResult(gc.TwoPrimeCalculation(value)); // assert Assert.AreEqual(expected, actual); } [TestCase(7, "2,2,3")] public void ThreePrimeCalculationSimple(int value, string expected) { // arrange GoldbachsConjecture gc = new GoldbachsConjecture(); // act string actual = PrintResult(gc.ThreePrimeCalculation(value)); // assert Assert.AreEqual(expected, actual); } [TestCase(9, "2,2,5 | 3,3,3")] [TestCase(11, "2,2,7 | 3,3,5")] [TestCase(13, "3,3,7 | 3,5,5")] [TestCase(15, "2,2,11 | 3,5,7 | 5,5,5")] [TestCase(17, "2,2,13 | 3,3,11 | 3,7,7 | 5,5,7")] public void ThreePrimeCalculationMultiple(int value, string expected) { // arrange GoldbachsConjecture gc = new GoldbachsConjecture(); // act string actual = PrintResult(gc.ThreePrimeCalculation(value)); // assert Assert.AreEqual(expected, actual); } public string PrintResult(IEnumerable<int[]> results) { string print = string.Empty; if (results == null || !results.Any()) return print; foreach (int[] result in results) { foreach (int num in result) { print += num + ","; } print = print.Substring(0, print.Length - 1) + " | "; } print = print.Substring(0, print.Length - 3); return print; } }