Exemple #1
0
        /// <summary>
        /// Returns a copy of this prime factorization object.
        /// </summary>
        public PrimeFactorization Copy()
        {
            var result = new PrimeFactorization();

            foreach (var entry in entriesList)
            {
                result.AddEntry(entry.Prime, entry.Power);
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Calculates all positive divisors of the integer provided.
        /// <example>For example, 18 returns IEnumerable<1, 2, 3, 6, 9, 18>.</example>
        /// </summary>
        /// <param name="n">The integer to calculate divisors for.</param>
        /// <param name="proper">If true, the integer n will be excluded.</param>
        /// <returns>All positive divisors of the integer n.</returns>
        public static IEnumerable <long> Divisors(long n, bool proper = false)
        {
            var factorization        = PrimeFactorization(n);
            var divisorFactorization = new PrimeFactorization();

            foreach (var item in factorization.Entries)
            {
                divisorFactorization.AddEntry(item.Prime, 0);
            }

            var product = 1L;

            while (true)
            {
                if (!proper || product != n)
                {
                    yield return(product);
                }

                var incremented = false;
                foreach (var item in divisorFactorization.Entries)
                {
                    if (item.Power < factorization.Entry(item.Prime).Power)
                    {
                        product *= item.Prime;
                        ++item.Power;
                        incremented = true;
                        break;
                    }
                    else
                    {
                        for (var i = 0; i < item.Power; ++i)
                        {
                            product /= item.Prime;
                        }
                        item.Power = 0;
                    }
                }
                if (!incremented)
                {
                    break;
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Returns true if this factorization is equivalent to the other factorization provided, false otherwise.
 /// </summary>
 public bool Equals(PrimeFactorization other)
 {
     if (other != null)
     {
         foreach (var entry1 in entriesList)
         {
             if (!entry1.Equals(other.Entry(entry1.Prime)))
             {
                 return(false);
             }
         }
         foreach (var entry2 in other.entriesList)
         {
             if (!entry2.Equals(Entry(entry2.Prime)))
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
Exemple #4
0
        /// <summary>
        /// Constructs a prime factorization of the integer provided.
        /// <example>For example, 60 = 2^2 * 3 * 5.</example>
        /// </summary>
        /// <param name="n">The integer for which a prime factorization will be constructed.</param>
        public static PrimeFactorization PrimeFactorization(long n)
        {
            var result = new PrimeFactorization();
            var power  = 0;

            while (n % 2L == 0)
            {
                n /= 2L;
                ++power;
            }
            if (power > 0)
            {
                result.AddEntry(2L, power);
            }

            for (var i = 3L; i <= System.Math.Sqrt(n); i += 2L)
            {
                power = 0;
                while (n % i == 0)
                {
                    n /= i;
                    ++power;
                }
                if (power > 0)
                {
                    result.AddEntry(i, power);
                }
            }

            if (n > 2)
            {
                result.AddEntry(n, 1);
            }

            return(result);
        }