Ejemplo n.º 1
0
        /// <summary>
        /// Calculates y from y^2 = x^3 + ax + b (mod p) by having x and the first byte indicating whether y is odd or even.
        /// Return value indicates success.
        /// </summary>
        /// <param name="x">X coordinate</param>
        /// <param name="firstByte">The first byte indicating whether y is odd or even (must be 2 or 3)</param>
        /// <param name="y">Calculated y (0 if fails to calculate)</param>
        /// <returns>True if y was found, otherwise false.</returns>
        public bool TryFindY(BigInteger x, byte firstByte, out BigInteger y)
        {
            if (firstByte != 2 && firstByte != 3)
            {
                return(false);
            }
            if (x.Sign < 1)
            {
                return(false);
            }

            // Curve.A is zero
            BigInteger right = (BigInteger.Pow(x, 3) + curve.B) % curve.P;

            if (SquareRoot.TryFind(right, curve.P, out y))
            {
                if (firstByte == 2 && !y.IsEven)
                {
                    // We know point is already on curve
                    y = PointNegChecked(new EllipticCurvePoint(x, y)).Y;
                }
                else if (firstByte == 3 && y.IsEven)
                {
                    y = PointNegChecked(new EllipticCurvePoint(x, y)).Y;
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public void CheckExpressions()
        {
            // Arrange
            Expression opr1 = new TerminalExpression(3.5);
            Expression opr2 = new TerminalExpression(2);

            Expression add  = new Addition(opr1, opr2);
            Expression subs = new Substraction(opr1, opr2);
            Expression mult = new Multiplication(opr1, opr2);
            Expression div  = new Division(opr1, opr2);
            Expression pow  = new Power(opr1, opr2);
            Expression sqrt = new SquareRoot(opr1);
            Expression mod  = new Modulus(opr1, opr2);

            // Act
            double addRes  = add.solve();
            double subsRes = subs.solve();
            double multRes = mult.solve();
            double divRes  = div.solve();
            double powRes  = pow.solve();
            double sqrtRes = sqrt.solve();
            double modRes  = mod.solve();

            // Assert
            Assert.AreEqual(3.5 + 2, addRes);
            Assert.AreEqual(3.5 - 2, subsRes);
            Assert.AreEqual(3.5 * 2, multRes);
            Assert.AreEqual(3.5 / 2, divRes);
            Assert.AreEqual(Math.Pow(3.5, 2), powRes);
            Assert.AreEqual(Math.Pow(3.5, 0.5), sqrtRes);
            Assert.AreEqual(3.5 % 2, modRes);
        }
Ejemplo n.º 3
0
        public void ValidSquareRootNumber()
        {
            var variant = new SquareRoot(new Number(81));

            Assert.That(variant.Integer, Is.EqualTo(9));
            Assert.That(variant.Description, Is.EqualTo("sqrt(81)"));
        }
Ejemplo n.º 4
0
        public static ICalculable GetOperationClass(ModelCalculator.Operators selectedOperator)
        {
            ICalculable calculateClass = default(ICalculable);

            switch (selectedOperator)
            {
            case ModelCalculator.Operators.Addition:
                calculateClass = new Addition();
                break;

            case ModelCalculator.Operators.Division:
                calculateClass = new Division();
                break;

            case ModelCalculator.Operators.Multiplication:
                calculateClass = new Multiplication();
                break;

            case ModelCalculator.Operators.Subtraction:
                calculateClass = new Subtraction();
                break;

            case ModelCalculator.Operators.SquareRoot:
                calculateClass = new SquareRoot();
                break;
            }

            return(calculateClass);
        }
Ejemplo n.º 5
0
        public void CalculateTest()
        {
            var calculator = new SquareRoot();
            var actual     = calculator.CalculateTwo(36);

            Assert.AreEqual(1296, actual, 0.25);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            // 1. SORTED ARRAY SMALLEST TO HIGHEST
            int[] nums = new int[] { 9, 12, 4, 7, 8, 11 };
            var   a    = new SortArray();
            var   ar   = a.SortedArrayByLowestNumber(nums);

            foreach (var i in ar)
            {
                Console.WriteLine(i);
            }

            // 2. FIBONNACI CALCULATE NTH SEQUENCE

            var f = Fibonacci.CalculateNthFibonacci(13);

            Console.WriteLine(f);

            // 3. PALINDROME

            var p  = new Palindromes();
            var ps = p.IsPalindromes("A man a plan a canal Panama");

            Console.WriteLine(ps);

            // 4. SQUARE ROOT

            var r  = new SquareRoot();
            var sq = r.SquareRootNumber(60);

            Console.WriteLine(sq);
        }
Ejemplo n.º 7
0
        public void NegativeValueTest()
        {
            var x      = -5;
            var solver = new SquareRoot();

            Assert.Throws <ArgumentOutOfRangeException>(() => solver.FindSqrt(x));
        }
        public void GetSquareRoot(string latex, IList <Element> container, Marker marker)
        {
            if (marker.Position < latex.Length - 8)
            {
                if (latex.Substring(marker.Position, 5) == "\\sqrt")
                {
                    marker.Position += 5;

                    var squareRoot = new SquareRoot();

                    var subsection1 = GetSubsection(latex, container, marker);

                    if (subsection1 != null)
                    {
                        var line1 = new MathematicsLine();

                        line1.Elements = subsection1.Item2;

                        squareRoot.InnerExpression = line1;

                        marker.Position += subsection1.Item1;

                        container.Add(squareRoot);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void SquareRootOfANumber()
        {
            var result = new SquareRoot(new Number(1));

            Assert.That(result.TechnicalDescription, Is.EqualTo("sqrt(1)"));
            Assert.That(result.Description, Is.EqualTo("sqrt(1)"));
            Assert.That(result.InnerDescription, Is.EqualTo("sqrt(1)"));
        }
Ejemplo n.º 10
0
        public void SquareRootOf60()
        {
            var r      = new SquareRoot();
            var sq     = r.SquareRootNumber(60);
            var actual = 7.745966692414834;

            Assert.Equal(sq, actual);
        }
Ejemplo n.º 11
0
        public void NormalBehaviourMultiTest(double x)
        {
            //AAA
            var solver = new SquareRoot();

            var result = solver.FindSqrt(x);

            Assert.AreEqual(result, Math.Sqrt(x), 0.0000001);
        }
Ejemplo n.º 12
0
        public void SquareRootOfAnAdditionResult()
        {
            var subResult = new Addition(new Number(1), new Number(2));
            var result    = new SquareRoot(subResult);

            Assert.That(result.TechnicalDescription, Is.EqualTo("sqrt(add(1, 2))"));
            Assert.That(result.Description, Is.EqualTo("sqrt(1 + 2)"));
            Assert.That(result.InnerDescription, Is.EqualTo("sqrt(1 + 2)"));
        }
Ejemplo n.º 13
0
        public void SquareRootOfNine()
        {
            var r  = new SquareRoot();
            var sq = r.SquareRootNumber(9);

            var actual = 3;

            Assert.Equal(sq, actual);
        }
Ejemplo n.º 14
0
 public void SqrRootDoubleArrayTest()
 {
     double[] x = { 1.2, 2.5, 6.2 };
     double[] c = SquareRoot.Root(arrayC);
     for (int i = 0; i < c.Length; i++)
     {
         Assert.AreEqual(x[i], c[i]);
     }
 }
Ejemplo n.º 15
0
        public void NegativeRootExceptions()
        {
            // Assign
            Expression neg     = new TerminalExpression(-25);
            Expression negRoot = new SquareRoot(neg);

            // Act
            double negRootRes = negRoot.solve();
        }
Ejemplo n.º 16
0
        public override object Clone()
        {
            var returnObject = new SquareRoot
            {
                FunctionName = string.Copy(FunctionName),
                Middle       = Middle
            };

            return(returnObject);
        }
Ejemplo n.º 17
0
        public void Newton_NegativeRootDegree_NanAnswer()
        {
            //Arrange
            var expectedAnswer = double.NaN;
            //Act
            var target = SquareRoot.Newton(number: 3, rootDegree: -1);

            //Assert
            Assert.AreEqual(expectedAnswer, target);
        }
Ejemplo n.º 18
0
        public void CalculateTest(
            double firstValue,
            double expected)

        {
            var calculator   = new SquareRoot();
            var actualResult = calculator.Calculate(firstValue);

            Assert.AreEqual(expected, actualResult, 0.001);
        }
Ejemplo n.º 19
0
        public void Newton_ZeroNumber_ZeroAnswer()
        {
            //Arrange
            var expectedAnswer = 0d;
            //Act
            var target = SquareRoot.Newton(number: 0, rootDegree: 2);

            //Assert
            Assert.AreEqual(expectedAnswer, target);
        }
Ejemplo n.º 20
0
        public void Newton_NormalInput_CorrectAnswer()
        {
            //Arrange
            var expectedAnswer = 3d;
            //Act
            var target = SquareRoot.Newton(number: 9, rootDegree: 2);

            //Assert
            Assert.AreEqual(expectedAnswer, target);
        }
Ejemplo n.º 21
0
        public double Execute_WhenValueIsNotNegative_ThenReturnSquareRoot(double value)
        {
            //Arrange
            var squareRoot = new SquareRoot();

            //Act
            var result = squareRoot.Execute(value);

            //Assert
            return(result);
        }
Ejemplo n.º 22
0
        public void RootDoubleArrayTest()
        {
            double[] y = SquareRoot.Root(e);
            int      i = 0;

            foreach (double w in y)
            {
                Assert.AreEqual(SquareRoot.Root(e[i]), w);
                i++;
            }
        }
Ejemplo n.º 23
0
        public void GetExecutableInfo_WhenValueIsNegative_ThenCannotExecuteAndReturnErrorMessage()
        {
            //Arrange
            var squareRoot = new SquareRoot();

            //Act
            var executableInfo = squareRoot.GetExecutableInfo(-1);

            //Assert
            Assert.That(executableInfo.Message, Is.EqualTo(ErrorMessages.InvalidInput));
        }
Ejemplo n.º 24
0
        public void GetExecutableInfo_WhenValueIsNotNegative_ThenCanExecute(double value)
        {
            //Arrange
            var squareRoot = new SquareRoot();

            //Act
            var executableInfo = squareRoot.GetExecutableInfo(value);

            //Assert
            Assert.That(executableInfo.CanBeExecuted, Is.True);
        }
Ejemplo n.º 25
0
        public void SimplifyZero()
        {
            var sqrt = new SquareRoot(new ConstantInt64(3));
            var c = sqrt + Constant.Factory.Create(0);
            var baseForm = c.GetBaseForm();
            Assert.AreEqual(sqrt, baseForm);

            c = new ConstantInt64(0) + sqrt;
            baseForm = c.GetBaseForm();
            Assert.AreEqual(sqrt, baseForm);
        }
Ejemplo n.º 26
0
        public void RootIntArrayTest()
        {
            double[] d = SquareRoot.Root(f);
            int      i = 0;

            foreach (double z in d)
            {
                Assert.AreEqual(SquareRoot.Root(f[i]), z);
                i++;
            }
        }
Ejemplo n.º 27
0
        static void Main()
        {
            SquareRoot sq = new SquareRoot();

            int startNum = 1;
            int endNum = 100;

            for (int i = 0; i < 10; i++)
            {
                startNum = ReadNumber(startNum, endNum);
            }
        }
Ejemplo n.º 28
0
        public void SquareRoot_SqrtRoot2_WithRealNumerInput_ShouldFindTheSquare()
        {
            //Arrange
            var input          = 5d;
            var expectedResult = Math.Sqrt(input);

            //Act
            var actualResult = SquareRoot.SqrtRoot2(input);

            //Assert
            Assert.Equal(expectedResult, actualResult);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Calculates y from y^2 = x^3 + ax + b (mod p) by having x for Schnorr encoding.
        /// Return value indicates success.
        /// </summary>
        /// <param name="x">X coordinate</param>
        /// <param name="y">Calculated y (0 if fails to calculate)</param>
        /// <returns>True if y was found, otherwise false.</returns>
        public bool TryFindYSchnorr(BigInteger x, out BigInteger y)
        {
            if (x.Sign < 1 || x > curve.P)
            {
                return(false);
            }

            // Curve.A is zero
            BigInteger right = (BigInteger.Pow(x, 3) + curve.B) % curve.P;

            return(SquareRoot.TryFind(right, curve.P, out y));
        }
Ejemplo n.º 30
0
        public void SimplifyZero()
        {
            var sqrt     = new SquareRoot(new ConstantInt64(3));
            var c        = sqrt + Constant.Factory.Create(0);
            var baseForm = c.GetBaseForm();

            Assert.AreEqual(sqrt, baseForm);


            c        = new ConstantInt64(0) + sqrt;
            baseForm = c.GetBaseForm();
            Assert.AreEqual(sqrt, baseForm);
        }
Ejemplo n.º 31
0
        public static double StandardDev(dynamic values)
        {
            double mean       = StatisticMean.Mean(values);
            double difference = 0;
            double variance   = 0;

            foreach (int a in values)
            {
                difference = Subtraction.Subtract((int)mean, a);
                difference = Math.Pow(difference, 2);
                variance   = addition.Sum(variance, (int)difference);
            }
            int ValueCount = Helpers.Arrays.Length(values);

            variance = Division.Quotient(variance, ValueCount);
            double standardDev = SquareRoot.Root(variance);

            return(standardDev);
        }
Ejemplo n.º 32
0
        internal IntegerMath(MainForm UseForm, int UsePrimeArrayLength)
        {
            MForm            = UseForm;
            PrimeArrayLength = UsePrimeArrayLength;
            if (PrimeArrayLength < 100)
            {
                PrimeArrayLength = 100;
            }

            Divider       = new Division(MForm, this);
            SquareRt      = new SquareRoot(MForm, this);
            Multiplier    = new Multiplication(MForm);
            ModReduction  = new ModularReduction(MForm, this);
            StdAlgorithms = new StandardAlgorithms(MForm, this);

            SignedD = new long[Integer.DigitArraySize];

            MakePrimeArray();
        }
Ejemplo n.º 33
0
	public static void Test()
	{
		int n = 5;
		SquareRoot sr = new SquareRoot();
		Console.Write(sr.FindRoot(n));
	}