/// <summary>
        /// Composite N-point approximation of the definite integral in the provided interval by Simpson's rule.
        /// </summary>
        public double IntegrateComposite(
            CustomFunction f,
            double intervalBegin,
            double intervalEnd,
            int numberOfPartitions)
        {
            if (numberOfPartitions <= 0)
            {
                throw new ArgumentOutOfRangeException("numberOfPartitions", Properties.LocalStrings.ArgumentPositive);
            }

            if (IntegerTheory.IsOdd(numberOfPartitions))
            {
                throw new ArgumentException(Properties.LocalStrings.ArgumentEven, "numberOfPartitions");
            }

            double step   = (intervalEnd - intervalBegin) / numberOfPartitions;
            double factor = step / 3;

            double offset = step;
            int    m      = 4;
            double sum    = f(intervalBegin) + f(intervalEnd);

            for (int i = 0; i < numberOfPartitions - 1; i++)
            {
                // NOTE (ruegg, 2009-01-07): Do not combine intervalBegin and offset (numerical stability!)
                sum    += m * f(intervalBegin + offset);
                m       = 6 - m;
                offset += step;
            }

            return(factor * sum);
        }
        public void TestEvenOdd64()
        {
            Assert.IsTrue(IntegerTheory.IsEven((long)0), "0 is even");
            Assert.IsFalse(IntegerTheory.IsOdd((long)0), "0 is not odd");

            Assert.IsFalse(IntegerTheory.IsEven((long)1), "1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd((long)1), "1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven((long)-1), "-1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd((long)-1), "-1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(Int64.MaxValue), "Int64.Max is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(Int64.MaxValue), "Int64.Max is odd");

            Assert.IsTrue(IntegerTheory.IsEven(Int64.MinValue), "Int64.Min is even");
            Assert.IsFalse(IntegerTheory.IsOdd(Int64.MinValue), "Int64.Min is not odd");
        }
        public void TestEvenOdd32()
        {
            Assert.IsTrue(IntegerTheory.IsEven(0), "0 is even");
            Assert.IsFalse(IntegerTheory.IsOdd(0), "0 is not odd");

            Assert.IsFalse(IntegerTheory.IsEven(1), "1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(1), "1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(-1), "-1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(-1), "-1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(Int32.MaxValue), "Int32.Max is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(Int32.MaxValue), "Int32.Max is odd");

            Assert.IsTrue(IntegerTheory.IsEven(Int32.MinValue), "Int32.Min is even");
            Assert.IsFalse(IntegerTheory.IsOdd(Int32.MinValue), "Int32.Min is not odd");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Run example
        /// </summary>
        public void Run()
        {
            // 1. Find out whether the provided number is an even number
            Console.WriteLine(@"1. Find out whether the provided number is an even number");
            Console.WriteLine(@"{0} is even = {1}. {2} is even = {3}", 1, IntegerTheory.IsEven(1), 2, 2.IsEven());
            Console.WriteLine();

            // 2. Find out whether the provided number is an odd number
            Console.WriteLine(@"2. Find out whether the provided number is an odd number");
            Console.WriteLine(@"{0} is odd = {1}. {2} is odd = {3}", 1, 1.IsOdd(), 2, IntegerTheory.IsOdd(2));
            Console.WriteLine();

            // 3. Find out whether the provided number is a perfect power of two
            Console.WriteLine(@"2. Find out whether the provided number is a perfect power of two");
            Console.WriteLine(@"{0} is power of two = {1}. {2} is power of two = {3}", 5, 5.IsPowerOfTwo(), 16, IntegerTheory.IsPowerOfTwo(16));
            Console.WriteLine();

            // 4. Find the closest perfect power of two that is larger or equal to 97
            Console.WriteLine(@"4. Find the closest perfect power of two that is larger or equal to 97");
            Console.WriteLine(97.CeilingToPowerOfTwo());
            Console.WriteLine();

            // 5. Raise 2 to the 16
            Console.WriteLine(@"5. Raise 2 to the 16");
            Console.WriteLine(16.PowerOfTwo());
            Console.WriteLine();

            // 6. Find out whether the number is a perfect square
            Console.WriteLine(@"6. Find out whether the number is a perfect square");
            Console.WriteLine(@"{0} is perfect square = {1}. {2} is perfect square = {3}", 37, 37.IsPerfectSquare(), 81, IntegerTheory.IsPerfectSquare(81));
            Console.WriteLine();

            // 7. Compute the greatest common divisor of 32 and 36
            Console.WriteLine(@"7. Returns the greatest common divisor of 32 and 36");
            Console.WriteLine(IntegerTheory.GreatestCommonDivisor(32, 36));
            Console.WriteLine();

            // 8. Compute the greatest common divisor of 492, -984, 123, 246
            Console.WriteLine(@"8. Returns the greatest common divisor of 492, -984, 123, 246");
            Console.WriteLine(IntegerTheory.GreatestCommonDivisor(492, -984, 123, 246));
            Console.WriteLine();

            // 9. Compute the extended greatest common divisor "z", such that 45*x + 18*y = z
            Console.WriteLine(@"9. Compute the extended greatest common divisor Z, such that 45*x + 18*y = Z");
            long x, y;
            var  z = IntegerTheory.ExtendedGreatestCommonDivisor(45, 18, out x, out y);

            Console.WriteLine(@"z = {0}, x = {1}, y = {2}. 45*{1} + 18*{2} = {0}", z, x, y);
            Console.WriteLine();

            // 10. Compute the least common multiple of 16 and 12
            Console.WriteLine(@"10. Compute the least common multiple of 16 and 12");
            Console.WriteLine(IntegerTheory.LeastCommonMultiple(16, 12));
            Console.WriteLine();
        }