Beispiel #1
0
        ///<summary>
        ///Returns the greatest common denominator between the value of this instance and the value of another instance
        /// </summary>
        /// <remarks>
        /// X cannot be negative but can be zero; whereas y cannot be negative or zero
        /// </remarks>
        public Natural Gcd(Natural value)
        {
            BigInteger x = this.value, y = value.value, temp = 0;

            try
            {
                if (x < 0 || y <= 0)
                {
                    throw new ArgumentException("An invalid argument was detected.");
                }
                else if (y < x)
                {
                    temp = x;
                    x    = y;
                    y    = temp;
                }
                while (y != 0)
                {
                    temp = x % y;
                    x    = y;
                    y    = temp;
                }
                this.value = x;
                return(this);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Beispiel #2
0
        ///<summary>
        ///Returns the next prime number greater than the value of this instance
        /// </summary>
        public Natural NextProbablePrime()
        {
            Natural n = new Natural(++this.value);

            if (n.value < 0)
            {
                throw new ArgumentException("Number should be within the domain of natural numbers.");
            }
            bool PrimeCheck = n.IsPrime();

            try
            {
                while (!PrimeCheck)
                {
                    n.value++;
                    PrimeCheck = n.IsPrime();
                    if (PrimeCheck)
                    {
                        PrimeCheck = true;
                        return(n);
                    }
                }
                return(n);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Beispiel #3
0
        ///<summary>
        ///Return the value of the expression this raised to value % mod
        /// </summary>
        public Natural ModPow(Natural exponent, Natural mod)
        {
            BigInteger exp = exponent.value, m = mod.value;

            this.value = this.Pow(exp).Modulo(mod).value;
            return(this);
        }
Beispiel #4
0
        ///<summary>
        ///Returns the least common multiple between the value of this instance and the value of another instance.
        /// </summary>
        /// <remarks>
        /// Calls the result of GCD
        /// </remarks>
        public Natural Lcm(Natural value)
        {
            Natural    temp = new Natural(this.value);
            BigInteger gcd  = temp.Gcd(value).value;

            this.value = (this.value / gcd) * value.value;
            return(this);
        }
Beispiel #5
0
        public BigInteger MODULO(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.Modulo(operand2).GetIntValue();

            return(result);
        }
Beispiel #6
0
        public BigInteger MULTIPLICATION(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.Multiply(operand2).GetIntValue();

            return(result);
        }
Beispiel #7
0
        public BigInteger DIVISION(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.Divide(operand2).GetIntValue();

            return(result);
        }
Beispiel #8
0
        public BigInteger SUBTRACTION(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.Subtract(operand2).GetIntValue();

            return(result);
        }
Beispiel #9
0
        public BigInteger ADDITION(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.Add(operand2).GetIntValue();

            return(result);
        }
Beispiel #10
0
        public BigInteger COMPARE(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);
            int     result   = operand1.CompareTo(operand2);

            return(result);
        }
Beispiel #11
0
        public string STRINGBASE(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural ibase    = new Natural(input2);
            string  result   = operand1.ToString(ibase.GetIntValue());

            return(result);
        }
Beispiel #12
0
        public Natural[] DIVIDEREMAIN(BigInteger input, BigInteger input2)
        {
            Natural operand1 = new Natural(input);
            Natural operand2 = new Natural(input2);

            Natural[] result = operand1.DivideAndRemainder(operand2);
            return(result);
        }
Beispiel #13
0
        public BigInteger POWER(BigInteger input, BigInteger input2)
        {
            Natural    operand1 = new Natural(input);
            BigInteger power    = input2;
            int        result   = operand1.Pow(power).GetIntValue();

            return(result);
        }
Beispiel #14
0
        public BigInteger MODPOWER(BigInteger input, BigInteger input2, BigInteger input3)
        {
            Natural operand1 = new Natural(input);
            Natural exponent = new Natural(input2);
            Natural mod      = new Natural(input3);
            int     result   = operand1.ModPow(exponent, mod).GetIntValue();

            return(result);
        }
Beispiel #15
0
        ///<summary>
        ///This returns a string formatted representation of the division algorithm
        /// </summary>
        public String DivisionAlgorithm(Natural value)
        {
            Natural X = new Natural(this.value); Natural X2 = new Natural(this.value);
            Natural Y = new Natural(value.value);

            if (X.value <= 0 || Y.value <= 0)
            {
                throw new ArgumentException("Invalid arguments detected!");
            }
            BigInteger q = X.Divide(value).value, r = X2.Modulo(Y).value;

            return(string.Format("{0} = {1} * {2} + {3}", X.value, Y.value, q, r));
        }
Beispiel #16
0
 ///<summary>
 ///Divide the value of this instance by the value of another instance of Natural then return the remainder.
 /// </summary>
 public Natural Modulo(Natural value)
 {
     try
     {
         if (value.value <= 0 || this.value <= 0)
         {
             throw new ArgumentException("You cannot divide by zero!");
         }
         this.value %= value.value;
         return(this);
     }
     catch (ArgumentException e)
     {
         Console.WriteLine(e);
         this.value = 0;
         return(this);
     }
 }
Beispiel #17
0
 ///<summary>
 ///Divide the value of this instance by the value of another instance of Natural.
 /// </summary>
 public Natural Divide(Natural value)
 {
     try
     {
         if (this.value <= 0 || value.value <= 0)
         {
             throw new ArgumentException("You cannot divide by zero!");
         }
         else
         {
             this.value /= value.value;
             return(this);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
Beispiel #18
0
 ///<summary>
 ///Subtract the value of this instance by the value of another instance of Natural.
 ///<para>
 /// <see cref="Natural()"/> Post-condition: The methods may return negative numbers or 0.
 /// </para>
 /// </summary>
 public Natural Subtract(Natural value)
 {
     try
     {
         if ((this.value - value.value) < 0)
         {
             throw new ArgumentException("Natural numbers cannot be negative");
         }
         else
         {
             this.value -= value.value;
         }
         return(this);
     }
     catch (ArgumentException e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
Beispiel #19
0
 ///<summary>
 ///Divide the value of this instance by the value of another instance of Natural then return the quotient and remainder as an array.
 /// </summary>
 /// <remarks>
 /// Due to the nature of BigInteger, the decimal or floating point precision is omitted.
 /// </remarks>
 public Natural[] DivideAndRemainder(Natural value)
 {
     Natural[] divided = new Natural[2];
     try
     {
         if (value.value <= 0 || this.value <= 0)
         {
             throw new ArgumentException("You cannot divide by zero!");
         }
         BigInteger quo = this.value / value.value;
         BigInteger mod = this.value % value.value;
         divided[0] = new Natural(quo);
         divided[1] = new Natural(mod);
         return(divided);
     }
     catch (ArgumentException e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
Beispiel #20
0
 ///<summary>
 ///Adds the value of this instance by the value of another instance of Natural.
 /// </summary>
 /// <remarks>
 /// Due to the nature of BigInteger, System.OverflowException is not to be thrown.
 /// </remarks>
 public Natural Add(Natural value)
 {
     this.value += value.value;
     return(this);
 }
Beispiel #21
0
 ///<summary>
 ///Multiply the value of this instance by the value of another instance of Natural.
 /// </summary>
 public Natural Multiply(Natural value)
 {
     this.value *= value.value;
     return(this);
 }
Beispiel #22
0
 ///<summary>
 ///Returns a boolean check true or false, whether the gcd of two values is equal to 1.
 /// </summary>
 /// <returns>
 /// @True: 1
 /// </returns>
 public Boolean IsRelativelyPrimeTo(Natural value)
 {
     return((this.Gcd(value).value == 1) ? true : false);
 }
Beispiel #23
0
 ///<summary>
 ///Returns the maximum value between this instance and another instance.
 /// </summary>
 public Natural Max(Natural value)
 {
     return(this.value > value.value ? this : value);
 }
Beispiel #24
0
 ///<summary>
 ///Returns the minimum value between this instance and another instance.
 /// </summary>
 public Natural Min(Natural value)
 {
     return(this.value < value.value ? this : value);
 }
Beispiel #25
0
 ///<summary>
 ///Returns different values depending on the compared instance.
 /// </summary>
 /// <returns>
 /// 1 if this instance value is greater than another instance
 /// 0 if this instance value is equal to another instance value
 ///-1 if this instance value is less than another instance value
 /// </returns>
 public int CompareTo(Natural value)
 {
     return(this.value == value.value ? 0 : this.value < value.value ? -1 : 1);
 }