# 大整数运算

用分治算法思想实现的大整数乘法

http://blogger.org.cn/blog/more.asp?name=njucs&id=10146

VC++6.0下测试通过.

#include <iostream.h>
#include <math.h>

long mult(long x,long y,int n);
int num(long x);

void main() //主函数
{
long x,y;
cout<<"input x and y:"<<endl;
cin>>x>>y;
cout<<mult(x,y,num(x))<<endl;
}

long mult(long x,long y,int n)
{
long a,b,c,d,s;
if (n=1)
return x*y;
else
{
a=long(x/pow(10,(n/2))); //取x的左半部分
b=long(x%long(pow(10,(n/2)))); //取x的右半部分
c=long(y/pow(10,(n/2))); //取y的左半部分
d=long(y%long(pow(10,(n/2)))); //取y的右半部分
s=mult(a,c,n)*pow(2,n)+(mult((a-b),(d-c),n)+mult(a,c,n)+mult(b,d,n))*pow(2,n/2)+mult(b,d,n); //书上的公式
return (s);
}
}

int num(long x) //判断输入的数字的位数
{
int i=0;
if(x-9<=0)
return 1;
else
{
while (x!=0)
{
i++;
x=x/10;
}
return i;
}
}

# Algorithm Gossip: 超长整数运算（大数运算）

## 实作

C
void add(int *a, int *b, int *c) {     int i, carry = 0;     for(i = N - 1; i >= 0; i--) {         c[i] = a[i] + b[i] + carry;         if(c[i] < 10000)             carry = 0;         else { // 进位             c[i] = c[i] - 10000;             carry = 1;         }     } } void sub(int *a, int *b, int *c) {     int i, borrow = 0;     for(i = N - 1; i >= 0; i--) {         c[i] = a[i] - b[i] - borrow;         if(c[i] >= 0)             borrow = 0;         else { // 借位             c[i] = c[i] + 10000;             borrow = 1;         }     } } void mul(int *a, int b, int *c) { // b 为乘数     int i, tmp, carry = 0;     for(i = N - 1; i >=0; i--) {         tmp = a[i] * b + carry;         c[i] = tmp % 10000;            carry = tmp / 10000;     } } void div(int *a, int b, int *c) {  // b 为除数     int i, tmp, remain = 0;     for(i = 0; i < N; i++) {         tmp = a[i] + remain;         c[i] = tmp / b;         remain = (tmp % b) * 10000;     } }

Java
public class BigNumber {    public static int[] add(int[] a, int[] b) {         int carry = 0;        int[] c = new int[a.length];        for(int i = a.length - 1; i >= 0; i--) {             c[i] = a[i] + b[i] + carry;             if(c[i] < 10000)                 carry = 0;             else { // 进位                 c[i] = c[i] - 10000;                 carry = 1;             }         }        return c;    }     public static int[] sub(int[] a, int[] b) {         int borrow = 0;         int[] c = new int[a.length];        for(int i = a.length - 1; i >= 0; i--) {             c[i] = a[i] - b[i] - borrow;             if(c[i] >= 0)                 borrow = 0;             else { // 借位                 c[i] = c[i] + 10000;                 borrow = 1;             }         }        return c;    }     public static int[] mul(int[] a, int b) { // b 为乘数         int carry = 0;         int[] c = new int[a.length];        for(int i = a.length - 1; i >=0; i--) {             int tmp = a[i] * b + carry;             c[i] = tmp % 10000;                carry = tmp / 10000;         }         return c;    }     public static int[] div(int[] a, int b) {  // b 为除数         int remain = 0;         int[] c = new int[a.length];        for(int i = 0; i < a.length; i++) {             int tmp = a[i] + remain;             c[i] = tmp / b;             remain = (tmp % b) * 10000;         }         return c;    }    public static void main(String[] args) {        int[] a = {1234, 5678, 9910, 1923, 1124};        int[] b = {1234, 5678, 9910, 1923, 1124};        int[] c = BigNumber.add(a, b);        for(int i = 0; i < c.length; i++) {            System.out.print(c[i]);        }        System.out.println();    }}