Exemple #1
0
        public static (bigint quotient, bigint reminder) Divide(bigint dividend, bigint divisor)
        {
            if (dividend == bigint.Zero)
            {
                return(bigint.Zero, bigint.Zero);
            }

            if (divisor == bigint.Zero)
            {
                throw new DivideByZeroException();
            }


            if (dividend == bigint.One)
            {
                return(bigint.Zero, divisor);
            }

            if (divisor == bigint.One)
            {
                return(dividend, bigint.Zero);
            }

            return(Implementation());

            (bigint quotient, bigint reminder) Implementation()
            {
                throw new NotImplementedException();
            }
        }
Exemple #2
0
        public void BigInt(int value)
        {
            var something = new big(value);
            var another   = new big(something);

            Assert.Equal(something, another);
        }
        //[InlineData(@"1001 1010 1000 0100
        //                0011 0100 1110 1100
        //                    1000 1110 0010 0010 0101", 2718281828459045)]
        public void BinaryParseWithoutIntOverflow(string str, long expectedResult)
        {
            var bigint = big.GetParser().Parse(str);
            var result = new big(expectedResult);

            Assert.Equal(result, bigint);
        }
Exemple #4
0
        public void IntegerMinusInteger(int lhsValue, int rhsValue, long resultValue)
        {
            var lhs = new big(lhsValue);
            var rhs = new big(rhsValue);

            var result = lhs - rhs;

            Assert.Null(result.Tail.NextDigit);
            Assert.Equal(resultValue, (int)result);
        }
Exemple #5
0
        public int CompareTo(big other)
        {
            if (this.Sign == Sign.Positive && other.Sign == Sign.Negative)
            {
                return(1);
            }

            if (this.Sign == Sign.Negative && other.Sign == Sign.Positive)
            {
                return(-1);
            }

            return(this.Tail.CompareTo(other.Tail));
        }
        public big Parse(string str)
        {
            var result       = new big(0);
            var current      = result.Tail;
            var currentDigit = -1;

            for (int i = str.Length - 1; i >= 0; i--)
            {
                var symbol = str[i];

                if (AllowedSeparators.Contains(symbol))
                {
                    continue;
                }

                currentDigit++;

                // create new block on int overflow
                const int BitsInInt32 = 32;
                if (currentDigit != 0 && currentDigit % BitsInInt32 == 0)
                {
                    current.NextDigit = new BigIntBlock();
                    current           = current.NextDigit;
                }

                switch (symbol)
                {
                case '1':
                    current.Digit = current.Digit.Increase((int)Math.Pow(2, currentDigit % BitsInInt32));
                    continue;

                case '0':
                    continue;

                default:
                    throw new FormatException();
                }
            }

            return(result);
        }
Exemple #7
0
        public static bigint Multiple(bigint lhs, bigint rhs)
        {
            if (lhs == bigint.Zero || rhs == bigint.Zero)
            {
                return(bigint.Zero);
            }

            if (lhs == bigint.One)
            {
                return(rhs);
            }

            if (rhs == bigint.One)
            {
                return(lhs);
            }

            return(Implementation());

            bigint Implementation()
            {
                throw new NotImplementedException();
            }
        }
Exemple #8
0
        public static bigint Add(bigint left, bigint right)
        {
            if (left == bigint.Zero)
            {
                return(right);
            }

            if (right == bigint.Zero)
            {
                return(left);
            }

            return(Implementation(left.Tail, right.Tail));

            bigint Implementation(BigIntBlock lhs, BigIntBlock rhs)
            {
                var result       = new BigIntBlock();
                var currentBlock = result;

                while (!ReferenceEquals(lhs, null) && !ReferenceEquals(rhs, null))
                {
                    if (!(ReferenceEquals(lhs.NextDigit, null) && ReferenceEquals(rhs.NextDigit, null)))
                    {
                        if (ReferenceEquals(lhs.NextDigit, null))
                        {
                            lhs.NextDigit = bigint.Zero.Tail;
                        }

                        if (ReferenceEquals(rhs.NextDigit, null))
                        {
                            rhs.NextDigit = bigint.Zero.Tail;
                        }
                    }

                    if (currentBlock == null)
                    {
                        currentBlock = new BigIntBlock();
                    }

                    long leftValue = lhs.Digit.Value;
                    if (left.Sign == Sign.Negative)
                    {
                        leftValue *= -1;
                    }

                    long rightValue = rhs.Digit.Value;
                    if (right.Sign == Sign.Negative)
                    {
                        rightValue *= -1;
                    }

                    long resultValue = leftValue + rightValue;

                    if (resultValue <= uint.MaxValue)
                    {
                        currentBlock.Digit = new Digit(resultValue);
                    }
                    else
                    {
                        resultValue -= uint.MaxValue;

                        var d = lhs.NextDigit.Digit;
                        d.Increase();
                        lhs.NextDigit.Digit = d;

                        currentBlock.Digit = new Digit(resultValue);
                    }

                    lhs          = lhs.NextDigit;
                    rhs          = rhs.NextDigit;
                    currentBlock = currentBlock.NextDigit;
                }

                return(new bigint
                {
                    Tail = result,
                });
            }
        }
Exemple #9
0
 public BigInt(big bigInt)
 {
     this.Tail = bigInt.Tail.DeepClone();
     this.Sign = bigInt.Sign;
 }
Exemple #10
0
 public bool Equals(big bigInt)
 {
     return(this.CompareTo(bigInt) == 0);
 }
Exemple #11
0
        public void Integer(int value)
        {
            var something = new big(value);

            Assert.Equal(value, (int)something);
        }
Exemple #12
0
        public void Empty()
        {
            var something = new big();

            Assert.Equal(big.Zero, something);
        }