protected override void Solve(out string answer) { PentagonManager pentagonManager = new PentagonManager(); //Generate numbers up to 5000 long maxCount = 5000; foreach (long j in Enumerable64.Range(1, maxCount)) { PentagonNumber pj = new PentagonNumber(j); pentagonManager.Add(pj); } long count = maxCount * maxCount; long progressDone = 0; //Create all pairs 5000x5000 Parallelization.GetParallelRanges(1, maxCount, 50).ForAll(sequence => { foreach (long j in sequence) { foreach (long k in Enumerable64.Range(1, maxCount)) { PentagonPair pjk = pentagonManager.CreatePair(j, k); if (pentagonManager.IsSumPentagonal(pjk)) { if (pentagonManager.IsDifPentagonal(pjk)) { lock (pentagonManager) { pentagonManager.StorePair(pjk); } } } lock (this) { progressDone++; if (progressDone % 100_000 == 0) { int percent = (int)(progressDone * 100.0 / count); UpdateProgress($"Pairs checked out of {count}: Done {percent}%. Hits: {pentagonManager.storedPairs.Count}..."); } } } } }); var solution = pentagonManager.storedPairs.OrderBy(pair => pair.AbsDifValue).First(); answer = $"Candiates = {pentagonManager.storedPairs.Count}, D = {solution.AbsDifValue}."; }
/// <summary> /// Tests whether the absolute difference of pentagons in the pair is also a pentagonal. /// </summary> /// <param name="pair">Pair whose diff |Pj-Pk| is to be checked for pentagonality.</param> /// <returns>True if the absolute value of difference is a pentagonal number</returns> internal bool IsDifPentagonal(PentagonPair pair) { var difValue = pair.AbsDifValue; return(dictionary.Values.Any(pentagon => pentagon.Value == difValue)); }
/// <summary> /// Records a pair for later use. /// </summary> /// <param name="pair">Pair to be stored.</param> internal void StorePair(PentagonPair pair) { storedPairs.Add(pair); }
/// <summary> /// Tests whether the sum of pentagons in the pair is also a pentagonal. /// </summary> /// <param name="pair">Pair whose sum is to be checked for pentagonality.</param> /// <returns>True if the sum is a pentagonal number</returns> internal bool IsSumPentagonal(PentagonPair pair) { var sumValue = pair.SumValue; return(dictionary.Values.Any(pentagon => pentagon.Value == sumValue)); }