Exemple #1
0
        public int Test_SteineGcd(int a, int b)
        {
            var res = GCD.SteineGcdTime(a, b);

            Console.WriteLine($"Completed with: {res.Item2}");
            return(res.Item1);
        }
Exemple #2
0
        public void CanGetSameResultFromGcdRecursiveAndNonRecursive()
        {
            int result  = GCD.gcd(2, 8);
            int result2 = GCD.GcdNonRecursive(2, 8);

            Assert.Equal(result, result2);
        }
Exemple #3
0
        public void CanProcessArrayOfInteger(int[] data)
        {
            int result  = GCD.FindGcd(data);
            int result2 = GCD.FindGcdNonRecursive(data);

            Assert.Equal(result, result2);
        }
Exemple #4
0
        public void GCDByStein_InputNegativeNumberAnd2_ThrowsArgumentException()
        {
            var firstNumber  = -4;
            var secondNumber = 2;

            GCD.GCDByStein(firstNumber, secondNumber, out TimeSpan time);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="n"></param>
        /// <param name="iterations"></param>
        /// <returns>true if n is prime, false if n is possibly composite</returns>
        public static bool Lucas(ulong n, ulong iterations)
        {
            ulong[] factorisation = Factorise.Optimised(n - 1);
            for (ulong i = 0; i < iterations; i++)
            {
c:
                ulong a = (ulong)random.Next(2, (int)n);
                if (GCD.Standard(a, n) > 1)
                {
                    return(false);
                }
                if (Power.BinaryMod(a, n - 1, n) == 1)
                {
                    foreach (uint prime in factorisation)
                    {
                        if (Power.BinaryMod(a, (n - 1) / prime, n) == 1)
                        {
                            goto c;
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
Exemple #6
0
        public int GCD_Binnary_Test(int a, int b, params int[] array)
        {
            int actual = new GCD().GetMultipleGCDBinnary(out int time, a, b, array);

            Assert.Positive(time);
            return(actual);
        }
Exemple #7
0
        public int Test_SteineGcd(params int[] arg)
        {
            var res = GCD.SteineGcdTime(arg);

            Console.WriteLine($"Completed with: {res.Item2}");
            return(res.Item1);
        }
        public void GCD_Smoke_Test()
        {
            Assert.AreEqual(3, GCD.Find(-9, 3));
            Assert.AreEqual(15, GCD.Find(45, 30));

            Assert.AreEqual(1, GCD.Find(3, 5));
        }
Exemple #9
0
        public void GridSubValueComputesGCD([Values(1, 2, 3, 4, 5, 6, 7, 8, 9)] int x, [Values(1, 2, 3, 4, 5, 6, 7, 8, 9)] int y)
        {
            var a = new GridPoint(1, 4, x);
            var b = new GridPoint(3, 2, y);

            new GridSubValue(a, b).GCDValue.Should().Be(GCD.Of(x, y));
        }
        public void EuclideanCalculationMethod_ArrayOfIntegers_CalculationTimeNotNull()
        {
            double time      = 0;
            int    resultGCD = GCD.EuclideanCalculation(out time, new int[] { 0, 1, 0, 1, 0 });

            Assert.NotNull(time);
        }
        public void BinaryGCDCalculationMethod_ArrayOgIntegers_CalculationTimeNotNull()
        {
            double time      = 0;
            int    resultGCD = GCD.BinaryGCDCalculation(out time, new int[] { 0, 1, 0, 1, 0 });

            Assert.NotNull(time);
        }
        public void BinaryGCDCalculationMethod_TwoIntegers_CalculationTimeNotNull()
        {
            double time      = 0;
            int    resultGCD = GCD.BinaryGCDCalculation(5, 0, out time);

            Assert.NotNull(time);
        }
        public void EuclideanCalculationMethod_OrdinaryValues_CalculationTimeNotNull()
        {
            double time      = 0;
            int    resultGCD = GCD.EuclideanCalculation(5, 0, out time);

            Assert.NotNull(time);
        }
Exemple #14
0
        public void Stein_LeadTime_Less1s(int number1, int number2, params int[] numbers)
        {
            TimeSpan time   = new TimeSpan();
            int      result = GCD.Stein(out time, number1, number2, numbers);

            Assert.IsTrue(time.Seconds <= 1, $"Lead time is {time.ToString()}");
        }
Exemple #15
0
        public void CloneTest()
        {
            var exp   = new GCD(Variable.X, new Number(0));
            var clone = exp.Clone();

            Assert.Equal(exp, clone);
        }
Exemple #16
0
        public void Test_SteinRecursion_Correct_Data(int expected, params int[] numbers)
        {
            //int expected = 16;
            TimeSpan time = new TimeSpan();

            Assert.AreEqual(expected, GCD.GCDStein(out time, numbers));
        }
        public void GetEstimatedComputingTime()
        {
            long time     = GCD.GetEstimatedComputingTime(32, 24, null);
            long expected = 1;

            Assert.AreEqual(expected, time);
        }
        public void GreatestCommonDivisorEuclideanOneNegative()
        {
            int gcd      = GCD.GreatestCommonDivisorEuclidean(30, -15);
            int expected = 15;

            Assert.AreEqual(expected, gcd);
        }
        public void GreatestCommonDivisorBinaryBothNegative()
        {
            int gcd      = GCD.GreatestCommonDivisorBinary(-1, -15);
            int expected = 1;

            Assert.AreEqual(expected, gcd);
        }
        public void GreatestCommonDivisorBinaryBothPositive()
        {
            int gcd      = GCD.GreatestCommonDivisorBinary(1024, 384);
            int expected = 128;

            Assert.AreEqual(expected, gcd);
        }
        public void GreatestCommonDivisorNullArray()
        {
            int gcd      = GCD.GreatestCommonDivisor(GCD.GreatestCommonDivisorEuclidean, null);
            int expected = 1;

            Assert.AreEqual(expected, gcd);
        }
        public void GreatestCommonDivisorOneNumber()
        {
            int gcd      = GCD.GreatestCommonDivisor(GCD.GreatestCommonDivisorBinary, 32);
            int expected = 1;

            Assert.AreEqual(expected, gcd);
        }
        public void GreatestCommonDivisoEuclideanrBothPositive()
        {
            int gcd      = GCD.GreatestCommonDivisorEuclidean(1024, 384);
            int expected = 128;

            Assert.AreEqual(expected, gcd);
        }
        public void ElapsedTimeTest()
        {
            GCD.MultiGCD(GCD.SteinsGCD, new int[] { 2, 4, 18, 6, 12, 20, 40, 180, 60, 120, Int32.MinValue, Int32.MinValue });
            long time = GCD.ElapsedTime;

            Assert.IsTrue(time != 0);
        }
        public static bool Lucas(ulong n)
        {
            var random        = new System.Random();
            var factorisation = Factorise.Optimised(n - 1);

            for (ulong a = 2; a < n;)
            {
c:
                if (GCD.Standard(a, n) > 1)
                {
                    return(false);
                }
                if (Power.BinaryMod(a, n - 1, n) == 1)
                {
                    foreach (uint prime in factorisation)
                    {
                        if (Power.BinaryMod(a, (n - 1) / prime, n) == 1)
                        {
                            a++;
                            goto c;
                        }
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
        public void FindGcdStein_WithManyParams()
        {
            TimeSpan time;
            int      result = GCD.FindGcdStein(out time, 5, 25, 15, 45, 85, 125);

            Assert.AreEqual(5, result);
            Assert.That(time, Is.GreaterThan(TimeSpan.Zero));
        }
Exemple #27
0
        public void TestBinaryEuclidGCDMax()
        {
            //arrange
            int[] valueException = { Int32.MaxValue, Int32.MinValue };

            //assert
            Assert.ThrowsException <ArgumentException>(() => GCD.BinaryEuclidGCD(valueException));
        }
        public void FindGcd()
        {
            TimeSpan time;
            int      result = GCD.FindGcdEuclid(5, -25, out time);

            Assert.AreEqual(5, result);
            Assert.That(time, Is.GreaterThan(TimeSpan.Zero));
        }
Exemple #29
0
 private long GetModularMultiplicativeInverse(long exponent, long modulus)
 {
     var res = GCD.EuclidExtended(exponent, modulus);
     if (res.D != 1)
         throw new InvalidOperationException("The greatest common divisor is not 1");
 
     return (res.X % modulus + modulus) % modulus;
 }
Exemple #30
0
        public void GetGcdByEuclideanAlgorithm_All_Positive_Numbers()
        {
            var value1   = 1071;
            var value2   = 462;
            var expected = 21;

            Assert.AreEqual(expected, GCD.GetGcdByEuclideanAlgorithm(value1, value2));
        }
 private static int GCDTimeDiagnostic(GCD algorithm, params int[] args)
 {
     int result = 0;
     Stopwatch watch = Stopwatch.StartNew();
     result = GCDParams(algorithm, args);
     watch.Stop();
     lastExecutionTime = (double)watch.ElapsedTicks / Stopwatch.Frequency;
     return result;
 }
 private static int GCDParams(GCD algorithm, params int[] args)
 {
     if (args == null)
         throw new ArgumentNullException("args");
     if (args.Length == 0)
         throw new ArgumentException("args is empty");
     int gcd = args[0];
     for (int i = 1; i < args.Length; i++)
     {
         gcd = algorithm(gcd, args[i]);
     }
     return gcd;
 }