示例#1
0
        internal FactorCollection Copy()
        {
            FactorCollection result = new FactorCollection();

            result.AddRange(this.Select(factor => new Factor {
                Prime = factor.Prime, Cardinality = factor.Cardinality
            }));
            return(result);
        }
示例#2
0
        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);
        }
示例#3
0
        internal FactorCollection Square()
        {
            FactorCollection result = this.Copy();

            foreach (Factor factor in result)
            {
                factor.Cardinality *= 2;
            }
            return(result);
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        public bool Equals(FactorCollection other)
        {
            if (other.Count != Count)
            {
                return(false);
            }

            foreach (Factor factor in this)
            {
                if (!other.Contains(factor))
                {
                    return(false);
                }
            }
            return(true);
        }