C#, Lesson 23 of "Programming for Newbies": Division of Large Numbers (BigInteger Divide)

1 text format


/// <summary>
/// Compare the sizes of a and b, return 1, 0, -1
/// The data is stored from low bit (right) to high bit (left);
/// </summary>
/// < param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int big_integer_compare(int[] a, int[] b )
{     for (int i = a.Length - 1; i >= 0; i--)     {         if (a[i] > b[i]) return 1;         else if (a[i] < b[i] ) return -1;     }     //Two digits are equal     return 0; }







/// <summary>
/// Lesson 23 of "Xiaobai Programming": The fourth of the four arithmetic operations of big numbers (BigInteger), division
/// Large number division c = a / b % d
/// c is the quotient, and d is the remainder.
/// The common division algorithm on the Internet is: use the "divisor" to continuously subtract the "divisor". The "number" of subtractions is the quotient, and the remainder is the remainder.
/// Of course, this is slowly slowly slowly slowly slowly and slowly slowly and slowly slowly slowly slowly.
This article is released. It is Truffer's large number division implemented by pure addition and subtraction.
/// The core idea of ​​Truffer's large number division is to estimate a multiple, such as 10,000, based on the difference in digits between the two numbers, and then perform subtraction calculations;
/// Calculate the remaining numbers by analogy.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="d">Remainder< /param>
/// <returns>Commerce c</returns>
public static string big_integer_divide(string a, string b, out string d)
{     int n = a.Length;     int[] db = string_to_digitals(b,

    string q = a;
    List<string> p = new List<string>();
    int[] dq = string_to_digitals(q, n);
    while (big_integer_compare(dq, db) >= 0)
    {         // By bit difference Number construction 100... Such multiples are used as multiples of the number being subtracted.         int len ​​= q.Length - b.Length;         // Minuend = multiple * divisor         string v2 = b + String.Join("", new int[len]);         int[] dv = string_to_digitals(v2, n) ;         // If the length of the current number and the minuend are different, adjust the multiple         if (big_integer_compare(dq, dv) < 0)         {             len--;             v2 = b + String.Join("", new int[len]);             dv = string_to_digitals(v2, n);         }











        // Subtract the minuend once each time and record the multiple;
        string v1 = "1" + String.Join("", new int[len]);
        while (big_integer_compare(dq, dv) >= 0)
        {             p .Add(v1);             q = big_integer_subtract(q, v2);             dq = string_to_digitals(q, n);         }     }




    //The last thing left is the remainder!
    d = q;

    // The sum of the recorded subtracted multiples is the quotient
    string r = p[0];
    for (int i = 1; i < p.Count; i++)
    {         r = big_integer_plus(r, p[i]);     }     return r ; }



2 code format

/// <summary>
/// 比较a,b的大小,返回1,0,-1
/// 数据从低位(右)往高位(左)存储;
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int big_integer_compare(int[] a, int[] b)
{
    for (int i = a.Length - 1; i >= 0; i--)
    {
        if (a[i] > b[i]) return 1;
        else if (a[i] < b[i]) return -1;
    }
    //两位数相等
    return 0;
}

/// <summary>
/// 《小白学程序》第二十三课:大数(BigInteger)的四则运算之四,除法
/// 大数除法 c = a / b % d
/// c 为商,d 为余数。
/// 网上常见的除法算法是:用“被除数”不断地减去“除数”,减去的“次数”就是商,剩下的就是余数。
/// 这当然很慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢慢
/// 本文发布的是纯加、减法实现的 Truffer 大数除法。
/// Truffer 大数除法的核心思想是按两个数的位数差距,估算一个倍数,比如10000,再进行减法计算;
/// 以此类推计算剩余的数字。
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="d">余数</param>
/// <returns>商c</returns>
public static string big_integer_divide(string a, string b, out string d)
{
    int n = a.Length;
    int[] db = string_to_digitals(b, n);

    string q = a;
    List<string> p = new List<string>();
    int[] dq = string_to_digitals(q, n);
    while (big_integer_compare(dq, db) >= 0)
    {
        // 按相差的位数构造 100... 这样的倍数,作为 被减的数的倍数。
        int len = q.Length - b.Length;
        // 被减数 = 倍数 * 除数
        string v2 = b + String.Join("", new int[len]);
        int[] dv = string_to_digitals(v2, n);
        // 如果当前数与被减数长度系统,调整倍数
        if (big_integer_compare(dq, dv) < 0)
        {
            len--;
            v2 = b + String.Join("", new int[len]);
            dv = string_to_digitals(v2, n);
        }

        // 每次减去一次被减数,并记录倍数;
        string v1 = "1" + String.Join("", new int[len]);
        while (big_integer_compare(dq, dv) >= 0)
        {
            p.Add(v1);
            q = big_integer_subtract(q, v2);
            dq = string_to_digitals(q, n);
        }
    }

    // 最后剩下的就是 余数!
    d = q;

    // 记录的 被减倍数 之和就是 商
    string r = p[0];
    for (int i = 1; i < p.Count; i++)
    {
        r = big_integer_plus(r, p[i]);
    }
    return r;
}

3 Calculation results

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132841994