3.BigInteger模拟了Integer的数学操作:
如add()等同于"+",subtract等同于"-",multiply等同于“*”,divide等同于“/”;运算时必须使用内部方法,操作数必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。 4.用一个程序熟悉其用法: import java.math.BigInteger; public class Test { public static void main(String[] args) { BigInteger total=new BigInteger("0"); BigInteger base=new BigInteger("2"); for (int i = 0; i < 64; i++) { total=total.add(base.pow(i)); } System.out.println(total); }}
5 java.math.BigInteger.pow(int exponent)方法
返回一个BigInteger,其值是 (thisexponent),该指数是一个整数,而不是一个BigInteger。
import java.math.*; public class BigIntegerDemo { public static void main(String[] args) { // create 2 BigInteger objects BigInteger bi1, bi2; // create and assign value to exponent int exponent = 2; // assign value to bi1 bi1 = new BigInteger("6"); // perform pow operation on bi1 using exponent bi2 = bi1.pow(exponent); String str = "Result is " + bi1 + "^" +exponent+ " = " +bi2; // print bi2 value System.out.println( str ); } }让我们编译和运行上面的程序,这将产生以下结果:
Result is 6^2 = 366求很大的素数
素数就是不能再进行等分的整数。比如:7,11。而9不是素数, 因为它可以平分为3等份。一般认为最小的素数是2,接着是3,5
public class Application { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("1"); for (int s = 1; s <= 1002; s++) { bigInteger = bigInteger.nextProbablePrime(); //求下一个素数 System.out.println(s + " : " + bigInteger); } } }
