internal FactorCollection Copy() { FactorCollection result = new FactorCollection(); result.AddRange(this.Select(factor => new Factor { Prime = factor.Prime, Cardinality = factor.Cardinality })); return(result); }
public static FactorCollection Factorize(this long number) { long initNumber = number; if (FactorCollections.ContainsKey(initNumber)) { return(FactorCollections[initNumber]); } FactorCollection results = new FactorCollection(); int sqrt = (int)Math.Sqrt(number); for (int i = 0; i < PrimeManager.Primes.Count; i++) { if (number == 1) { break; } if (PrimeManager.Primes[i] > sqrt) { results.Add(new Factor { Prime = number, Cardinality = 1 }); number = 1; break; } if (number % PrimeManager.Primes[i] == 0) { results.Add(new Factor { Prime = PrimeManager.Primes[i], Cardinality = 1 }); number /= PrimeManager.Primes[i]; } if (FactorCollections.ContainsKey(number)) { results = results.Append(FactorCollections[number]); FactorCollections.Add(initNumber, results); return(results); } while (number % PrimeManager.Primes[i] == 0) { results[results.Count - 1].Cardinality++; number /= PrimeManager.Primes[i]; } } if (number != 1) { results.Add(new Factor { Prime = number, Cardinality = 1 }); } FactorCollections.Add(initNumber, results); return(results); }
internal FactorCollection Square() { FactorCollection result = this.Copy(); foreach (Factor factor in result) { factor.Cardinality *= 2; } return(result); }
internal FactorCollection Append(FactorCollection that) { //assumes well ordering by primes FactorCollection result = new FactorCollection(); int tindex = 0; int findex = 0; while ((findex < that.Count) && (tindex < this.Count)) { if (this[tindex].Prime < that[findex].Prime) { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = this[tindex].Cardinality }); tindex++; } else if (this[tindex].Prime > that[findex].Prime) { result.Add(new Factor { Prime = that[findex].Prime, Cardinality = that[findex].Cardinality }); findex++; } else { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = this[tindex].Cardinality + that[findex].Cardinality }); tindex++; findex++; } } for (; tindex < this.Count; tindex++) { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = this[tindex].Cardinality }); } for (; findex < that.Count; findex++) { result.Add(new Factor { Prime = that[findex].Prime, Cardinality = that[findex].Cardinality }); } return(result); }
internal FactorCollection Combine(FactorCollection that) { FactorCollection result = new FactorCollection(); int tindex = 0; int findex = 0; while ((findex < that.Count) && (tindex < this.Count)) { if (this[tindex].Prime < that[findex].Prime) { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = this[tindex].Cardinality }); tindex++; } else if (this[tindex].Prime > that[findex].Prime) { result.Add(new Factor { Prime = that[findex].Prime, Cardinality = that[findex].Cardinality }); findex++; } else { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = Math.Max(this[tindex].Cardinality, that[findex].Cardinality) }); tindex++; findex++; } } for (; tindex < this.Count; tindex++) { result.Add(new Factor { Prime = this[tindex].Prime, Cardinality = this[tindex].Cardinality }); } for (; findex < that.Count; findex++) { result.Add(new Factor { Prime = that[findex].Prime, Cardinality = that[findex].Cardinality }); } return(result); }
public bool Equals(FactorCollection other) { if (other.Count != Count) { return(false); } foreach (Factor factor in this) { if (!other.Contains(factor)) { return(false); } } return(true); }