コード例 #1
0
ファイル: Program.cs プロジェクト: qiao-snail/TheCode
        static BigInteger Karatsuba(BigInteger x, BigInteger y)
        {
            int n = (int)Math.Max(x.BitLength(), y.BitLength());
            if (n <= 1) return x * y;

            n = ((n + 1) / 2);

            BigInteger b = x >> n;
            BigInteger a = x - (b << n);
            BigInteger d = y >> n;
            BigInteger c = y - (d << n);

            BigInteger ac = Karatsuba(a, c);
            BigInteger bd = Karatsuba(b, d);
            BigInteger abcd = Karatsuba(a + b, c + d);

            return ac + ((abcd - ac - bd) << n) + (bd << (2 * n));
        }