Exemplo n.º 1
0
        public void CompareToTest()
        {
            // Higher powers of the same prime are greater
            Assert.That((new PrimeFactors(4)).CompareTo(new PrimeFactors(2)) > 0);
            // Higher primes are greater
            Assert.That((new PrimeFactors(3)).CompareTo(new PrimeFactors(2)) > 0);

            // Equality semantics
            for (var i = 1; i < 1000; i++)
            {
                var iFactors = new PrimeFactors(i);
                for (var j = 1; j < 1000; j++)
                {
                    var jFactors = new PrimeFactors(j);
                    if (i == j)
                    {
                        Assert.That(iFactors.CompareTo(jFactors), Is.EqualTo(0), "Prime Factors of equal numbers must compare equal.");
                    }
                    else
                    {
                        Assert.That(iFactors.CompareTo(jFactors), Is.Not.EqualTo(0), "Prime Factors of distinct numbers must compare not-equal.");
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void InverseFactorTest()
 {
     for (var i = 1; i < 1000; i++)
     {
         var powers = PrimeFactors.Factor(i);
         Assert.That(PrimeFactors.Value(powers), Is.EqualTo(i));
     }
 }
Exemplo n.º 3
0
        public void FactorTest()
        {
            var r32  = new[] { 5 };
            var r210 = new[] { 1, 1, 1, 1 };

            Assert.That(PrimeFactors.Factor(32), Is.EqualTo(r32));
            Assert.That(PrimeFactors.Factor(210), Is.EqualTo(r210));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generate an iterator over the proper divisors of the number passed in,
        /// other than the number itself.
        /// Iteration will proceed as for <see cref="Divisors(int[])"/>.
        /// </summary>
        /// <param name="value">Value whose divisors are desired</param>
        /// <returns>An enumerator over all proper divisors of the number.</returns>
        public static IEnumerable <int> ProperDivisors(int value)
        {
            var factors = PrimeFactors.Factor(value);

            foreach (var i in Divisors(factors))
            {
                if (i < value)
                {
                    yield return(i);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get the perfection of a number.  A number whose proper divisors
        /// add up to the number is called "perfect".  If the sum is less then the
        /// number it is called "deficient".  Otherwise it is called "abundant".
        /// </summary>
        /// <param name="factors">Prime factor powers of the number</param>
        /// <returns>-1 if deficient, 0 if perfect, 1 if abundant</returns>
        public static int Perfection(int[] factors)
        {
            var sum   = 0;
            var value = PrimeFactors.Value(factors);

            foreach (var i in ProperDivisors(factors))
            {
                sum += i;
                if (sum > value)
                {
                    return(1);
                }
            }
            return((sum < value) ? -1 : 0);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Count the number of possible divisors of a number, including 1 and the number itself.
 /// </summary>
 /// <param name="value">The number to test.</param>
 /// <returns>the number of possible divisors of the original number.</returns>
 public static int CountDivisors(int value)
 {
     return(CountDivisors(PrimeFactors.Factor(value)));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Get the perfection of a number.  A number whose proper divisors
 /// add up to the number is called "perfect".  If the sum is less then the
 /// number it is called "deficient".  Otherwise it is called "abundant".
 /// </summary>
 /// <param name="value">The number</param>
 /// <returns>-1 if deficient, 0 if perfect, 1 if abundant</returns>
 public static int Perfection(int value)
 {
     return(Perfection(PrimeFactors.Factor(value)));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Generate an iterator over the divisors of the number passed in.
 /// Iteration will proceed as for <see cref="Divisors(int[])"/>.
 /// </summary>
 /// <param name="value">Value whose divisors are desired</param>
 /// <returns>An enumerator over all divisors of the number.</returns>
 public static IEnumerable <int> Divisors(int value)
 {
     return(Divisors(PrimeFactors.Factor(value)));
 }
Exemplo n.º 9
0
        public void InverseFactorValue(int value)
        {
            var powers = PrimeFactors.Factor(value);

            Assert.That(PrimeFactors.Value(powers), Is.EqualTo(value));
        }