// called by ChainStalker when the work is done public static void RecieveTheChain(PrimeChain theChain) { if (ChainsStalker.IsBreaked) Console.WriteLine("\n\nYou stopped the Stalker from completing it's hunt. Here is what he was able to find:"); if (theChain != null) { Console.WriteLine("\n\nChains stalker has found his victim!"); Console.WriteLine("Max chain length is: " + theChain.ChainLength); Console.WriteLine("First element is: " + theChain.FirstElement); Console.WriteLine("First element displacement is: " + theChain.FirstElementDisplacement); Console.WriteLine("Last element is: " + theChain.LastElement); Console.WriteLine("Last element displacement is: " + theChain.LastElementDisplacement); Console.WriteLine(string.Format("\nSketch of the victim:\n...{0}-[{1}]...<{2}>...{3}-[{4}]...", theChain.FirstElementDisplacement, theChain.FirstElement, theChain.ChainLength, theChain.LastElementDisplacement, theChain.LastElement)); } else Console.WriteLine("\nChain stalker couldn't find any primes at all :("); timer.Stop(); Console.WriteLine("\nElapsed time, ms: " + timer.ElapsedMilliseconds); Console.ReadLine(); }
public static void FindTheChain() { PrimeChain currentChain = null; for (ulong i = 0; i < (ulong)dataSet.Length; i++) { if (isPrime[i]) { ulong displacement = i; if (currentChain == null) { currentChain = new PrimeChain(dataSet[i], displacement); } else if (dataSet[i] > currentChain.LastElement.GetValueOrDefault()) { currentChain.LastElement = dataSet[i]; currentChain.LastElementDisplacement = displacement; currentChain.ChainLength++; } else { primeChains.Add(currentChain); currentChain = new PrimeChain(dataSet[i], displacement); } } } if (currentChain != null) primeChains.Add(currentChain); Console.Write("\rStalking progress: 100% "); if (primeChains.Count > 0) { primeChains = primeChains .OrderByDescending(x => x.ChainLength) .ThenByDescending(x => x.FirstElement) .ThenBy(x => x.FirstElementDisplacement) // not mandatory, but has minimum impact on performance — leaving it just for sure .ToList(); Program.RecieveTheChain(primeChains[0]); } else Program.RecieveTheChain(null); }