Multiplication of large numbers and multiplication of large numbers
Address: http://blog.csdn.net/wangyuling1234567890/article/details/39278097
# Include <stdlib. h> # include <stdio. h> # include <string. h> void multiply (char * a, char * B, char * c) {int sa = 0; int sb = 0; int I, j; int * result = NULL; if (NULL = a) | (NULL = B) | (NULL = c) {return;} sa = strlen (); sb = strlen (B); result = (int *) malloc (sizeof (int) * (sa + sb )); // clears the array storing results // memset (result, 0, sizeof (result); for (I = 0; I <sa + sb; I ++) {result [I] = 0 ;}for (I = 0; I <sa; I ++) {for (j = 0; j <sb; j ++) {result [I + j] + = (* (a + sa-i-1)-'0') * (B + sb-j-1)-'0 ');}} // carry for (I = 0; I <sa + sb; I ++) {result [I + 1] + = result [I]/10; result [I] % = 10;} // value of the output parameter I = sa + SB-1; if (result [I] = 0) {I --;} (; i> = 0; I --) {* c ++ = result [I] + '0';} * c = '\ 0'; free (result); return ;} void main () {char * a = "52364"; char * B = "68746"; char c [100] = {0}; multiply (a, B, c ); return ;}
Fast Algorithm for multiplication of large numbers
The fastest Algorithm for multiplication of large numbers is the fast Fourier transformation method, which is one, but not written by myself.
# Include <iostream>
# Include <cmath>
# Include <complex>
# Include <cstring>
Using namespace std;
Const double PI = acos (-1 );
Typedef complex <double> cp;
Typedef long int64;
Const int N = 1 <16;
Int64 a [N], B [N], c [N <1];
Void bit_reverse_copy (cp a [], int n, cp B [])
{
Int I, j, k, u, m;
For (u = 1, m = 0; u <n; u <= 1, ++ m );
For (I = 0; I <n; ++ I)
{
J = I; k = 0;
For (u = 0; u <m; ++ u, j> = 1)
K = (k <1) | (j & 1 );
B [k] = a [I];
}
}
Void FFT (cp _ x [], int n, bool flag)
{
Static cp x [N <1];
Bit_reverse_copy (_ x, n, x );
Int I, j, k, kk, p, m;
For (I = 1, m = 0; I <n; I <= 1, ++ m );
Double alpha = 2 * PI;
If (flag) alpha =-alpha;
For (I = 0, k = 2; I <m; ++ I, k <= 1)
{
Cp wn = cp (cos (alpha/k), sin (alpha/k ));
For (j = 0; j <n; j + = k)
{
Cp w = 1, u, t;
Kk = k> 1;
For (p = 0; p <kk; ++ p)
{
T = w * x [j + p + kk];
U = x [j + p];
X [j + p] = u + t;
X [j + p + kk] = u-t;
W * = wn;
}
}
}
Memcpy (_ x, x, sizeof (cp) * n );
}
Void polynomial_multiply (int ...... remaining full text>
C language, big number multiplication problem, urgent,
# Include <stdio. h>
# Include <string. h>
# Define N 80
Void mul (int * x, int * y, int * z)
{
Int I, j;
For (I = 0; I <2 * N; I ++)
Z [I] = 0;
For (I = 0; I <N; I ++)
For (j = 0; j <N; j ++)
Z [I + j] + = x [I] * y [j];
For (I = 0; I <2 * N-1; I ++)
{
Z [I + 1] + = z [I]/10;
Z [I] % = 10;
}
}
Void rev (char * s, int * n)
{
Int I = 0, j = strlen (s)-1;
While (j> = 0)
N [I ++] = s [j --]-'0 ';
}
Void iniXY (int * x, int * y)
{
Int I;
For (I = 0; I <N; I ++)
X [I] = y [I] = 0;
}
Int main ()
{
Char a [N], B [N];
Int x [N], y [N], z [2 * N];
Int I = 2 * N-1;
Gets ();
Gets (B );
IniXY (x, y );
Rev (a, x );
Rev (B, y );
Mul (x, y, z );
While (! Z [I --])
;
I ++;
While (I> = 0)
Printf ("% d", z [I --]);
Printf ("\ n ");
Return 0;
}