public IEnumerable<IEnumerable<int>> GetResults(BooleanMatrix pairs, int count, ImmutableSequence<int> current) { int length = pairs.Width; int start; if (!current.HasValue) { start = 0; } else { start = current.First(); } var results = new Range(start, length) .Where(i => current .All(x => pairs[x, i])); if (count == 1) { return results .Select(i => new ImmutableSequence<int>(i, current)); } else { return results .SelectMany(i => GetResults(pairs, count - 1, new ImmutableSequence<int>(i, current))); } }
public override object CalculateResult() { int maximum = 10000; long[] primes = PrimeGenerator.Instance .TakeWhile(p => p < maximum) .ToArray(); int length = primes.Length; var pairs = new BooleanMatrix(length, length); for (int i = 0; i < length; i++) { long alpha = primes[i]; long alphaMultiplier = MathUtilities.Pow(10, (long)Math.Log10(alpha) + 1); for (int j = i + 1; j < length; j++) { long bravo = primes[j]; if (PrimeGenerator.Instance.IsPrime(bravo * alphaMultiplier + alpha) && PrimeGenerator.Instance.IsPrime(alpha * MathUtilities.Pow(10, (long)Math.Log10(bravo) + 1) + bravo)) { pairs[i, j] = true; pairs[j, i] = true; } } } return GetResults(pairs, 5, ImmutableSequence<int>.Empty) .Select(g => g .Select(p => primes[p]) .Sum()) .Min(); }
public BooleanMatrix(BooleanMatrix source) { if (source == null) { throw new ArgumentNullException("source"); } _width = source._width; _height = source._height; byte[] sourceData = source._data; int length = sourceData.Length; _data = new byte[length]; for (int i = 0; i < length; i++) { _data[i] = sourceData[i]; } }