Thursday 16 August 2012

Example for usage of Big Integer: Use of Big Integer to do factorial.


import java.math.BigInteger;
import java.util.ArrayList;


public class TextFactorial {

public static synchronized BigInteger bigNumber(int num){

ArrayList list = new ArrayList();
list.add(BigInteger.valueOf(1));

for (int i=list.size();i<(num+1);i++){
BigInteger lastfact = (BigInteger)list.get(i-1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(i));
list.add(nextfact);
}
return (BigInteger)list.get(num);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int num = 23;
System.out.println(TextFactorial.bigNumber(num));
}

}


Big Integer: 任意无限大。 As long as computer support, it could support. 

1 comment: