Example #1
0
        public void GetSqrtResults(Sqrt sqrt)
        {
            try
            {
                string[] values = new string[] { sqrt.Number };
                // Check if the values are numbers
                var allNumbers = CalculatorService.Server.Helpers.Numbers.AllNumbers(values);
                if (allNumbers)
                {
                    if (Convert.ToDouble(sqrt.Number) >= 0)
                    {
                        var result = Math.Sqrt(Convert.ToDouble(sqrt.Number));

                        sqrt.Total = result;
                    }
                    else
                    {
                        throw new Exception("The value cannot be negative");
                    }
                }
                else
                {
                    throw new Exception("Some of the elements are not numerics");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #2
0
        public Response <Sqrt> Sqrt([FromBody] SqrtDto sqrtdto)
        {
            return(Extension.Try <Sqrt>(() =>
            {
                string TrackingId = null;


                Sqrt Restul = this.serviceCalculators.Sqrt(sqrtdto.Number);

                //If a 'TrackingId’ was provided, the server should store this request’s details inside it’s
                //journal, indexed by the given Id.
                TrackingId = ValidateTrackingId();

                if (!string.IsNullOrEmpty(TrackingId))
                {
                    Operations operation = new Operations()
                    {
                        Id = TrackingId,
                        Operation = "Sqrt",
                        Date = DateTime.UtcNow.ToString("s") + "Z",
                        Calculation = sqrtdto.Number + " square " + "=" + Restul.Square
                    };

                    this.serviceCalculators.SaveJournal(operation);
                }

                return Restul;
            }, _logger));
        }
Example #3
0
        public void CalculateTest(double argument, double result, double accuracy)
        {
            var    calculator = new Sqrt();
            double testResult = calculator.Calculate(argument);

            Assert.AreEqual(testResult, result, accuracy);
        }
Example #4
0
        public void CalculateTest(double argument, double result)
        {
            var calculator = new Sqrt();
            var testResult = calculator.Calculate(argument);

            Assert.AreEqual(result, testResult);
        }
Example #5
0
        public void TestSqrt(double firstArgument, double output)
        {
            var calculator = new Sqrt();
            var testResult = calculator.Calculate(firstArgument);

            Assert.AreEqual(output, testResult, 0.0000001);
        }
Example #6
0
        public void SqrtTest2(double firstValue, double expected)
        {
            IOneArgumentCalculate calculator = new Sqrt();
            double result = calculator.Calculate(firstValue);

            Assert.AreEqual(expected, result, 0.00001);
        }
Example #7
0
        public void SimpleTest(double arg, double expected)
        {
            Sqrt   calc   = new Sqrt();
            double result = calc.Action(arg);

            Assert.AreEqual(expected, result);
        }
        public void CalculateTest()
        {
            Sqrt calculator = new Sqrt();

            Assert.AreEqual(5, calculator.Calculate(25));
            Assert.AreEqual(3, calculator.Calculate(9));
        }
Example #9
0
        public void SimpleTest(double firstarg, double expected)
        {
            Sqrt   calc   = new Sqrt();
            double result = calc.Calc(firstarg);

            Assert.AreEqual(expected, result);
        }
        public IActionResult Sqrt(Sqrt sqrt)
        {
            try
            {
                // Methods
                IMethods methods = new Methods();
                methods.GetSqrtResults(sqrt);

                // If have tracking-id, save log
                _logOperations.SetOperationsLog(Request.Headers["X-Evi-Tracking-Id"], new Operations()
                {
                    Calculation = "Sqrt", Operation = String.Format(Helpers.OperationString.ConvertArrayInText(new string[] { sqrt.Number }, String.Empty), "v~", sqrt.Total)
                });

                _logs.SetLogs(DateTime.Now.ToString("dd/MM/yyyy"), JsonConvert.SerializeObject(new { Type = "Success", User = Request.Headers["X-Evi-Tracking-Id"], EntryData = JsonConvert.SerializeObject(sqrt.Number), Operation = new Operations()
                                                                                                     {
                                                                                                         Calculation = "Sqrt", Operation = String.Format(Helpers.OperationString.ConvertArrayInText(new string[] { sqrt.Number }, String.Empty), "v~", sqrt.Total)
                                                                                                     } }));

                return(Ok(new { Square = sqrt.Total }));
            }
            catch (Exception ex)
            {
                Errors error = new Errors
                {
                    ErrorMessage = ex.Message,
                    ErrorCode    = "Internal Server Error",
                    ErrorStatus  = 400
                };

                _logs.SetLogs(DateTime.Now.ToString("dd/MM/yyyy"), JsonConvert.SerializeObject(new { Type = "Error", User = Request.Headers["X-Evi-Tracking-Id"], EntryData = JsonConvert.SerializeObject(sqrt.Number), Operation = error }));

                return(BadRequest(error));
            }
        }
Example #11
0
        public void WhenNegative_ShouldThrowArgumentException(
            [Values(-1)] double x
            )
        {
            var operation = new Sqrt(x);

            Assert.Throws <ArgumentException>(() => { var result = operation.Operate(); });
        }
Example #12
0
        public void Calculate(double argument)
        {
            var calculator = new Sqrt();
            var testResult = calculator.Calculate(argument);
            var result     = Math.Sqrt(argument);

            Assert.AreEqual(testResult, result);
        }
Example #13
0
        public void WhenX16_ShouldResult4(
            [Values(16)] double x
            )
        {
            var operation = new Sqrt(x);

            Assert.AreEqual(4, operation.Operate());
        }
Example #14
0
        public void Calculate(int input, int output)
        {
            var calculator = new Sqrt();
            var testResult = calculator.Calculate(input);
            var result     = output;

            Assert.AreEqual(testResult, result);
        }
Example #15
0
        public void SqrtWithInvalidArgumentReturnsPoundValue()
        {
            var func           = new Sqrt();
            var parsingContext = ParsingContext.Create();
            var args           = FunctionsHelper.CreateArgs();
            var result         = func.Execute(args, parsingContext);

            Assert.AreEqual(eErrorType.Value, ((ExcelErrorValue)result.Result).Type);
        }
Example #16
0
        public void TestSqrt()
        {
            var r = Sqrt.SimpleSqrt(100);

            Assert.AreEqual(r, 10);

            r = Sqrt.SimpleSqrt_w2(731026);
            Assert.AreEqual(r, 855);
        }
 static bool Prob066MinimalPellSolution(int n, ref BigInt rst)
 {
     Output("working on {0}: ", n);
     ContinuedFraction cf = new Sqrt(n).ContinuedFraction;
     int counter = 0;
     Fraction expend = cf.ToFraction(counter);
     BigInt x = expend.Numerator, y = expend.Denominator;
     while ((x * x) - (n * y * y) != 1)
     {
         counter++;
         expend = cf.ToFraction(counter);
         x = expend.Numerator; y = expend.Denominator;
     }
     OutputLine("{0}^2 - {1}*{2}^2 = 1", x, n, y);
     if (rst < x)
     {
         rst = x;
         return true;
     }
     else
     {
         return false;
     }
 }
Example #18
0
 public Formula(int num, int div, Sqrt sqrt, int sequence)
 {
     this.num = num;
     this.div = div;
     this.sqrt = sqrt;
     this.sequence = sequence;
 }
Example #19
0
        static int GetPeriod(int num)
        {
            var map = new Dictionary<Formula, int>();
            var formula = new Sqrt(num).First;
            int count = 0;
            while (!map.ContainsKey(formula))
            {
                count++;
                map.Add(formula, 0);
                formula = formula.Next;
            }

            return count - map[formula] -1;
        }