Exemplo n.º 1
0
        public long FibonacciNumber(long n)
        {
            long fib   = 0;
            long temp  = 1;
            var  count = n;

            if (n < 0)
            {
                count = n * -1;
            }

            try
            {
                checked
                {
                    for (var i = 0; i < count; i++)
                    {
                        fib  = fib + temp;
                        temp = fib - temp;
                    }
                }
            }
            catch (OverflowException ex)
            {
                var fault = new ArgumentOutOfRangeExceptionFault();

                fault.FaultMessage = "Fib(>92) will cause a 64-bit integer overflow.";

                throw new FaultException <ArgumentOutOfRangeExceptionFault>(fault, fault.FaultMessage);
            }


            if (n < 0)
            {
                if ((n * -1) % 2 != 0)
                {
                    fib = fib * -1;
                }
            }

            return(fib);
        }
Exemplo n.º 2
0
        public long FibonacciNumber(long n)
        {
            long fib = 0;
            long temp = 1;
            var count = n;

            if(n < 0)
            {
                count = n * -1;
            }

            try
            {
                checked
                {
                    for (var i = 0; i < count; i++)
                    {
                        fib = fib + temp;
                        temp = fib - temp;
                    }
                }
            }
            catch (OverflowException ex)
            {
                var fault = new ArgumentOutOfRangeExceptionFault();

                fault.FaultMessage = "Fib(>92) will cause a 64-bit integer overflow.";

                throw new FaultException<ArgumentOutOfRangeExceptionFault>(fault, fault.FaultMessage);
            }

            if (n < 0)
            {
                if ((n * -1) % 2 != 0)
                {
                    fib = fib * -1;
                }
            }

            return fib;
        }