///<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; } }
///<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; } }
///<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); }
///<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); }
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); }
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); }
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); }
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); }
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); }
public BigInteger COMPARE(BigInteger input, BigInteger input2) { Natural operand1 = new Natural(input); Natural operand2 = new Natural(input2); int result = operand1.CompareTo(operand2); return(result); }
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); }
public Natural[] DIVIDEREMAIN(BigInteger input, BigInteger input2) { Natural operand1 = new Natural(input); Natural operand2 = new Natural(input2); Natural[] result = operand1.DivideAndRemainder(operand2); return(result); }
public BigInteger POWER(BigInteger input, BigInteger input2) { Natural operand1 = new Natural(input); BigInteger power = input2; int result = operand1.Pow(power).GetIntValue(); return(result); }
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); }
///<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)); }
///<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); } }
///<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; } }
///<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; } }
///<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; } }
///<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); }
///<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); }
///<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); }
///<summary> ///Returns the maximum value between this instance and another instance. /// </summary> public Natural Max(Natural value) { return(this.value > value.value ? this : value); }
///<summary> ///Returns the minimum value between this instance and another instance. /// </summary> public Natural Min(Natural value) { return(this.value < value.value ? this : value); }
///<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); }