A

I solved the problem. For starters, it was replaced by a reading method.public static uint[] Substraction(uint[] x, uint[] y, long radix = Base)
{
if (x.Length > y.Length)
y = NormalizeArrays(y, x.Length);
else x = NormalizeArrays(x, y.Length);
uint[] result = new uint[x.Length];
long carry = 0;
int i = 0;
if (radix == Base)
{
for (i = 0; i < x.Length; i++)
{
//carry = carry + x[i] - y[i] + 10;
//result[i] = (uint)carry % 10;
//if (carry < 10) carry = -1;
//else carry = 0;
carry = carry + x[i] - y[i] + radix;
result[i] = (uint)carry;
if (carry < radix) carry = -1;
else carry = 0;
}
}
else
{
for(i = 0; i < x.Length; i++)
{
carry = carry + x[i] - y[i] + radix;
result[i] = (uint)(carry % radix);
if (carry < radix) carry = -1;
else carry = 0;
}
}
result = Normalize(result);
return result;
}
The same method of multiplying changed and sub-codedpublic uint[] KaratsubaMultiplication(uint[] x, uint[] y, long radix = Base)
{
int maxLength = 0;
if (x.Length > y.Length) maxLength = x.Length;
else maxLength = y.Length;
uint[] result = new uint[maxLength * 2];
if (maxLength == 1)
{
ulong mul = (ulong)x[0] * (ulong)y[0];
if (radix == Base)
{
//if(mul > 10)
if (mul > Base)
{
result[0] = (uint)mul;
result[1] = (uint)(mul >> 32);
//result[0] = (uint)mul % 10;
//result[1] = (uint)mul / 10;
}
else result[0] = (uint)mul;
}
else
{
result[0] = (uint)(mul % (ulong)radix);
result[1] = (uint)(mul / (ulong)radix);
}
}
else
{
//a
uint[] BitsX = ExtendArray(x, maxLength);
//b
uint[] BitsY = ExtendArray(y, maxLength);
//а1 первая часть числа a (тк с конца массив у меня)
uint[] a1 = TakeElements(BitsX, BitsX.Length / 2, BitsX.Length / 2);
//a2 вторая часть числа a
uint[] a2 = TakeElements(BitsX, BitsX.Length / 2);
//b1 первая часть числа b
uint[] b1 = TakeElements(BitsY, BitsY.Length / 2, BitsY.Length / 2);
//b2 вторая часть числа b
uint[] b2 = TakeElements(BitsY, BitsY.Length / 2);
//Произведение первых частей чисел
uint[] a1b1 = KaratsubaMultiplication(a1, b1, radix);
//Произведение вторых частей чисел
uint[] a2b2 = KaratsubaMultiplication(a2, b2, radix);
//Сумма произведений первых и вторых частей
uint[] a1b1Suma2b2 = Addition(a1b1, a2b2, radix);
// (a1+a2)(b1+b2) - (a1b1 + a2b2), то что перед - это вот эта переменная хах
uint[] ab = KaratsubaMultiplication(Addition(a1, a2, radix), Addition(b1, b2, radix), radix);
ab = Substraction(ab, a1b1Suma2b2, radix);
result = Addition(Finalize(a1b1, BitsX.Length), Finalize(ab, BitsX.Length / 2), radix);
result = Addition(result, a2b2, radix);
}
result = Normalize(result);
return result;
}
And the worst mistake in the code. Like this. ulong sum = x[i] + y[i] + carry;Tick x[i] and y[i] is uint, as long as I understand and deliver results to uint and then ulong leads. And if there's a refill, the sum will be wrong. So I'm everywhere at these moments (in combination and subtraction) like x[i] adding to ulong.