//BigInteger 不能直接计算出10000的阶乘,下面这个程序仅需1524ms
//100000的阶乘耗时163091 ms
class Jc{
private int digit;
private int[] data;
public Jc(){
digit = 1;
data = new int[4000000];
data[0] = data[1] = 1;
}
public static void main(String[] args){
long start = System.currentTimeMillis();
Jc jc2 = new Jc();
jc2.run(10000);
long end = System.currentTimeMillis();
System.out.println("Spent "+(end-start)+" ms.");
}
public void run(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=digit;j++){
data[j] *= i;
}
for(int k=1;k<=digit;k++){
if(data[k]>=10){
for(int r=1;r<=digit;r++){
if(data[digit]>=10){
digit++;
}
data[r+1]+=data[r]/10;
data[r]=data[r]%10;
}
}
}
}
for(int k=digit;k>0;k--){
System.out.print(data[k]);
}
System.out.println();
System.out.println(digit);
}
}
No comments:
Post a Comment