编写一个Java 应用程序,计算两个大整数的和、差、积和商,并计算一个大整数的因 子个数(因子中不包括1 和大整数本身)。

时间:2021-03-10 06:43:59
 1 package ex6_2;
 2 import java.math.BigInteger;
 3 
 4 public class BigintegerExample {
 5     public static void main(String[] args) {
 6                 BigInteger n1=new BigInteger("987654321987654321");
 7                 BigInteger n2=new BigInteger("123456789123456789");
 8                 BigInteger result=null;
 9                 result=n1.add(n2);
10                 System.out.println("和:"+result.toString());
11                 result=n1.subtract(n2);
12                 System.out.println("差:"+result.toString());
13                 result=n1.multiply(n2);
14                 System.out.println("积:"+result.toString());
15                 result=n1.divide(n2);
16                 System.out.println("商:"+result.toString());
17                 BigInteger m=new BigInteger("123"),
18                            COUNT=new BigInteger("0"),
19                                 ONE=new BigInteger("1"),
20                                TWO=new BigInteger("2");
21                 System.out.print(m.toString()+"的因子有:");
22                 for(BigInteger i=TWO; i.compareTo(m)<0;i=i.add(ONE)){
23                     if((m.remainder(i).compareTo(BigInteger.ZERO))==0){
24                         COUNT=COUNT.add(ONE);
25                         System.out.print("  "+i.toString());
26                     }
27                 }
28                 System.out.println(" ");
29                 System.out.println(m.toString()+"一共有"+COUNT.toString()+"个因子");
30             }
31     }