public int CompareTo(HighComposite other) { // A null value means that this object is greater. return((other == null) ? 1 : this.Value.CompareTo(other.Value)); }
public void FindHcns(int orderOfHcnToFind) { do { // Generate the next round of leaves leafNodeCandidates = leafNodeCandidates .SelectMany( selector: existingCandidate => { var newNodes = new List <HighComposite>(); // Always add a right node with a new exponent for one more prime var rightNode = new HighComposite( new List <int>(existingCandidate.Exponents) { 1 }); // ... if it doesn't overflow. if (!rightNode.Overflows) { newNodes.Add(rightNode); } // Prepare a left node incrementing the last exponent but add it only if it doesn't exceed the previous exponent. // (Or there is only one exponent.) // This because HCN must satisfy the condition that the sequence of exponents is non-increasing. var newLeftList = new List <int>(existingCandidate.Exponents); newLeftList[newLeftList.Count - 1]++; var leftNode = new HighComposite(newLeftList); if ((newLeftList.Count == 1 || newLeftList[newLeftList.Count - 2] >= newLeftList[newLeftList.Count - 1]) && !leftNode.Overflows) { newNodes.Add(leftNode); } return(newNodes); }) .ToList(); if (leafNodeCandidates.Count == 0) { throw new OverflowException("Cannot find further highly composable number - reached the limits of C#'s 64-bit long."); } stagedCandidates.AddRange(leafNodeCandidates); // 1. Of the new leaves, order them by smallest to largest value leafNodeCandidates.Sort(); // 2. Any staged candidate of even lesser value (and greater count of divisors than other less values) is a confirmed HCN // as we won't be able to generate any further candidate of lesser value. // Remove confirmed HCNs from the staged list. // Also remove staged candidates which count of divisors is less than the best confirmed HCN or better candidates. // Using a for iteration with index so we can modify the list as we walk it, patching the index along the way. stagedCandidates.Sort(); int currentHighestCountOfDivisors = this.highComposites.Last().CountOfDivisors; for (int index = 0; index < stagedCandidates.Count; index++) { HighComposite candidate = stagedCandidates[index]; // Filter out higher value candidates which count of divisors is less or equal than best confirmed HCN or lower candidates if (candidate.CountOfDivisors <= currentHighestCountOfDivisors) { stagedCandidates.RemoveAt(index); index--; continue; } currentHighestCountOfDivisors = candidate.CountOfDivisors; // We already know this candidate has a higher count of divisors. Is it also of lower value than the new generation of leaves? if (candidate.Value <= leafNodeCandidates.First().Value) { this.highComposites.Add(candidate); stagedCandidates.RemoveAt(index); index--; } } } while (this.highComposites.Count < orderOfHcnToFind); }
public bool Equals(HighComposite other) { return(this == other); }