码迷,mamicode.com
首页 > 编程语言 > 详细

两个大的整数的运算(java)

时间:2016-07-21 21:49:00      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

 1 import java.math.BigInteger;    
 2    
 3 public class BigInt {    
 4     BigInteger m1;    
 5     BigInteger m2;    
 6     BigInteger m3;    
 7     
 8     BigInt(String s1, String s2) {    
 9         m1 = new BigInteger(s1);    
10         m2 = new BigInteger(s2);    
11     }    
12     
13     public void add() {    
14         m3 = m1.add(m2);    
15         System.out.println("两个数的和为:" + m3);    
16     }    
17     
18     public void cut() {    
19         m3 = m1.subtract(m2);    
20         System.out.println("两个数的差为:" + m3);    
21     }    
22     
23     public void multiply() {    
24         m3 = m1.multiply(m2);    
25         System.out.println("两个数的积为:" + m3);    
26     }    
27     
28     public void divide() {    
29         m3 = m1.divide(m2);    
30         System.out.println("两个数的商为:" + m3);    
31     }    
32     
33     public void factorCount() {    
34         int count = 0;    
35         for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m1) < 0; i = i    
36                 .add(BigInteger.ONE)) {    
37             if (m1.remainder(i).equals(BigInteger.ZERO)) {    
38                 ++count;    
39             }    
40         }    
41         System.out.println(m1 + "的因子个数为:" + count);    
42     }    
43     
44     
45     public static void main(String[] args)
46     {
47         String num1="123456789123456789123456789";
48         String num2="987654321987654321987654321";        
49         BigInt bigInt=new BigInt(num1,num2);
50         
51         bigInt.add();
52         bigInt.cut();
53         bigInt.multiply();
54         bigInt.divide();
55         bigInt.factorCount();       
56     }   
57 }    

技术分享

 

两个大的整数的运算(java)

标签:

原文地址:http://www.cnblogs.com/xh0102/p/5693045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!