C#,《小白学程序》第二十五课:大数乘法(BigInteger Multiply)的Karatsuba算法及源代码

1 文本格式

/// <summary>

/// 《小白学程序》第二十五课:大数(BigInteger)的Karatsuba乘法

/// Multiplies two bit strings X and Y and returns result as long integer

/// </summary>

/// <param name="a"></param>

/// <param name="b"></param>

/// <returns></returns>

public static string karatsuba_multiply(string a, string b)

{

// Find the maximum of lengths of x and Y and make

// length of smaller string same as that of larger string

int n = Math.Max(a.Length, b.Length);

if (a.Length != n) a = a.PadLeft(n, '0');

if (b.Length != n) b = b.PadLeft(n, '0');

// Base cases

if (n == 0)

{

return "0";

}

else if (n == 1)

{

return ((a[0] - '0') * (b[0] - '0')).ToString();

}

else if (n <= 9)

{

// int max 21474 83647

// long max 9223 37203 68547 75807

return (ulong.Parse(a) * ulong.Parse(b)).ToString();

}

int fh = n / 2; // First half of string

int sh = n - fh; // Second half of string

// Find the first half and second half of first string.

string x1 = a.Substring(0, fh);

string x2 = a.Substring(fh);

// Find the first half and second half of second string

string y1 = b.Substring(0, fh);

string y2 = b.Substring(fh);

// Recursively calculate the three products of

// inputs of size n/2

string p1 = karatsuba_multiply(x1, y1);

string p2 = karatsuba_multiply(x2, y2);

//string P3 = Karatsuba(AddBitStrings(Xl, Xr), AddBitStrings(Yl, Yr));

string p3 = karatsuba_multiply(big_integer_plus(x1, x2), big_integer_plus(y1, y2));

// Combine the three products to get the final result.

//return P1 * (1L << (2 * sh)) + (P3 - P1 - P2) * (1L << sh) + P2;

int[] t1 = new int[sh];

string w1 = String.Join("", t1);

string v1 = p1 + w1 + w1;

string v2 = big_integer_subtract(p3, big_integer_plus(p1, p2)) + w1;

return big_integer_plus(v1, big_integer_plus(v2, p2));

}

2 代码格式

cs 复制代码
/// <summary>
/// 《小白学程序》第二十五课:大数(BigInteger)的Karatsuba乘法
/// Multiplies two bit strings X and Y and returns result as long integer
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static string karatsuba_multiply(string a, string b)
{
    // Find the maximum of lengths of x and Y and make
    // length of smaller string same as that of larger string
    int n = Math.Max(a.Length, b.Length);
    if (a.Length != n) a = a.PadLeft(n, '0');
    if (b.Length != n) b = b.PadLeft(n, '0');

    // Base cases
    if (n == 0)
    {
        return "0";
    }
    else if (n == 1)
    {
        return ((a[0] - '0') * (b[0] - '0')).ToString();
    }
    else if (n <= 9)
    {
        // int max 21474 83647
        // long max 9223 37203 68547 75807
        return (ulong.Parse(a) * ulong.Parse(b)).ToString();
    }

    int fh = n / 2;  // First half of string
    int sh = n - fh; // Second half of string

    // Find the first half and second half of first string.
    string x1 = a.Substring(0, fh);
    string x2 = a.Substring(fh);

    // Find the first half and second half of second string
    string y1 = b.Substring(0, fh);
    string y2 = b.Substring(fh);

    // Recursively calculate the three products of
    // inputs of size n/2
    string p1 = karatsuba_multiply(x1, y1);
    string p2 = karatsuba_multiply(x2, y2);
    //string P3 = Karatsuba(AddBitStrings(Xl, Xr), AddBitStrings(Yl, Yr));
    string p3 = karatsuba_multiply(big_integer_plus(x1, x2), big_integer_plus(y1, y2));

    // Combine the three products to get the final result.
    //return P1 * (1L << (2 * sh)) + (P3 - P1 - P2) * (1L << sh) + P2;
    int[] t1 = new int[sh];
    string w1 = String.Join("", t1);
    string v1 = p1 + w1 + w1;
    string v2 = big_integer_subtract(p3, big_integer_plus(p1, p2)) + w1;
    return big_integer_plus(v1, big_integer_plus(v2, p2));
}
相关推荐
AI+程序员在路上6 分钟前
QT与网页显示数据公式的方法
开发语言·qt
EasyCVR13 分钟前
EasyRTC嵌入式音视频通话SDK:基于纯C语言的跨平台实时通信系统设计与实践
linux·c语言·开发语言·音视频·webrtc·h.265
仟濹27 分钟前
【前缀和与差分 二分搜索 C/C++】洛谷 P1083 借教室
c语言·c++·算法
xinxiangwangzhi_1 小时前
多视图几何--从线性变换到射影变换--2线性变换
人工智能·算法·计算机视觉
AI技术控1 小时前
计算机视觉算法实战——昆虫识别检测(主页有源码)
人工智能·算法·计算机视觉
qq_447663052 小时前
《Spring日志整合与注入技术:从入门到精通》
java·开发语言·后端·spring
蜡笔小新星2 小时前
OpenCV中文路径图片读写终极指南(Python实现)
开发语言·人工智能·python·opencv·计算机视觉
Rverdoser2 小时前
java八股文之消息中间件
c#·linq
七七知享2 小时前
2024 Qiniu 跨平台 Qt 高级开发全解析
开发语言·qt·零基础·操作系统·跨平台·qt5·精通