/// <summary> /// Calculates a Subtraction of a two ALong absolute values, a.k.a "School" method. /// </summary> /// <param name="num1">Minuend</param> /// <param name="num2">Subtrahend</param> /// <returns>ALong instance representing a difference between absolute values of minuend and subtrahend.</returns> private static ALong AbsSub(ALong num1, ALong num2) { if (num1 == num2) { return(new ALong(0)); } var big = AMath.Max(Abs(num1), Abs(num2)); var small = AMath.Min(Abs(num1), Abs(num2)); var result = String.Empty; int carry = 0; for (int i = 0; i < big.Length(); i++) { var n1 = big.GetRNumAtIndex(i); var n2 = small.GetRNumAtIndex(i); var ns = n1 - carry; carry = 0; if (ns < n2) { ns += 10; carry = 1; } ns = ns - n2; result = ns.ToString() + result; } return(new ALong(result)); }
/// <summary> /// Converts string representation of a number in a given base to ALong number. /// Extension does not check if you provide invalid numbers, e.g. '1010102' in base 2 - you will just get incorrect answer. /// </summary> /// <param name="number">String representation of a number</param> /// <param name="aBase">Base number is presented in</param> /// <param name="sym">String of symbols used to represent a number.</param> /// <returns>ALong number</returns> public static ALong FromArbitraryBase(this string number, int aBase, string sym = null) { if (String.IsNullOrEmpty(sym)) { sym = GetSymbols(aBase); } if (aBase < 37) { number = number.ToLower(); } // Ignore case if base <= 36 var r = number.Reverse(); var resp = new ALong(0); var i = 0; foreach (var c in r) { var index = sym.IndexOf(c); resp = resp + AMath.Pow(new ALong(aBase), i) * index; i++; } return(resp); }