java--- BigInteger的用法

 (一)引言

   最近java实验遇到BigInteger类,故因此上网找资料,总结个人笔记,以求加强记忆。

      在java中经常会遇到比较大的数,甚至超过了long型,那么该如何处理这些“大数据”呢?在java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,从原则上是可以表示“天文单位”一样大的数字咯,但有一个缺点就是比较费内存!

在这里,我们详细描述下BigInteger的用法,在使用之前,我们需要导入java.math.*包

import java.math.*;

 (二)介绍BigInteger经常使用到的一些函数 

方法声明
功能介绍
BigInteger(String val)根据参数指定的字符串来构造对象
BigInteger add(BigInteger val)用于实现加法运算
BigInteger subtract(BigInteger val)用于实现减法运算
BigInteger multiply(BigInteger val)用于实现乘法运算
BigInteger divide(BigInteger val)用于实现除法运算
BigInteger remainder(BigInteger val)用于实现取余运算
BigInteger[] divideAndRemainder(BigInteger val)用于实现取商和余数的运算

value.Of(参数); 这个函数的作用是将括号内的参数转换成指定的数据类型,例如以下例子


int A=42;

BigInteger f=BigInteger.valueOf(A);

 System.out.println("f="+f); //输出的f将会等于BigInteger型的42

  // 答案: f=42

//其实还可以转成其他的类型,例如以下,需要注意的是不重写的话,jdk1.8 版本是无法支持这种转换的

   String s="12345";
  BigInteger c= BigInteger.valueOf(s);//c=12345

add()方法; 这个函数的作用是将大整数加起来,例如以下例子:


                      BigInteger c=new BigInteger("666");
                       BigInteger d=new BigInteger("3");
                       System.out.println("c+d="+c.add(d));
                        //答案输出: c+d=669

​

③subtract()方法,这个函数的作用是将大整数相减,例如以下例子,谁调用就是谁减去后者:

               BigInteger c=new BigInteger("55");                  BigInteger d=new 
               BigInteger("33");
               System.out.println("d-c="+d.subtract(c));
                //答案输出: d-c=-22

④multiply()方法,这个函数的作用是将大整数相乘,例如以下例子:

                      BigInteger c=new BigInteger("6");
                      BigInteger d=new BigInteger("3");
                      System.out.println("c*d="+c.multiply(d));
                        //答案输出: c*d=18

⑤divide()方法,这个函数的作用是将大整数做除法,例如以下例子:

​
                      BigInteger c=new BigInteger("6");
                      BigInteger d=new BigInteger("2");
                      System.out.println("c/d="+c.divide(d));
                        // 答案输出;c/d=3

​

⑥remainder()方法,这个函数的作用是将大整数取余:

BigInteger m =new BigInteger("17637"),Count=new BigInteger("0"),One=new BigInteger("1"),Two=new BigInteger("2");
        System.out.println(m.toString()+"的因子有:");
        for (BigInteger i = Two; i.compareTo(m)< 0; i=i.add(One)) {
            //判断m对i是否能模i的结果==0
            if((m.remainder(i).compareTo(BigInteger.ZERO))==0){
                 Count=Count.add(One);
                System.out.println(" "+i.toString());
            }
        }
        System.out.println("");
        System.out.println(m.toString()+"一共有"+Count.toString()+"个因子");

⑦pow(exponent)方法,这个函数的作用是将大整数取exponent的指数,例如:

⑧gcd()方法,这个函数的作用是将两个大整数取最大公约数,例如 

⑨abs()方法,这个函数的作用是取绝对值,例如

⑩negate()方法,这个函数的作用是取数的相反数,例如

mod()方法; 这个函数的作用是对数进行取余 ,例如

⑫max()方法,min()方法,分别是比较两个数的大小,例如 

⑬compareTo()方法这个方法是用来比较两个大整数大小的

⑭equals()方法,判断两个大整数是否相等,例如 :

(三) 介绍BigInteger一些基本类型的转换

public void testToAnother() {
    BigInteger bigNum = new BigInteger("36");
    int radix = 2;
  //1.转换为bigNum的二进制补码形式
    byte[] num1 = bigNum.toByteArray();
    //2.转换为bigNum的十进制字符串形式
    String num2 = bigNum.toString();        //36
    //3.转换为bigNum的radix进制字符串形式
    String num3 = bigNum.toString(radix);   //100100
    //4.将bigNum转换为int
    int num4 = bigNum.intValue();
    //5.将bigNum转换为long
    long num5 = bigNum.longValue();
    //6.将bigNum转换为float
    float num6 = bigNum.floatValue();
    //7.将bigNum转换为double
    double num7 = bigNum.doubleValue();
}

(四)如何遍历大整数---求阶乘,前n项和

  //阶乘
        BigInteger m11 =new BigInteger("4"),Count11=new BigInteger("1"),One11=new BigInteger("1"),Two11=new BigInteger("1");
        for (BigInteger i =Two11; i.compareTo(m11)<=0; i=i.add(Two11)) {
            Count11=Count11.multiply(i);
        }
        System.out.println("");
        System.out.println(m11.toString()+"前阶乘为: "+Count11.toString() );
        System.out.println("----------------------->");
    //前n项和
        BigInteger mm =new BigInteger("99999999"),Count1=new BigInteger("0"),One1=new BigInteger("1"),Two1=new BigInteger("1");
        for (BigInteger i =Two1; i.compareTo(mm)<=0; i=i.add(Two1)) {
                Count1=Count1.add(i);
        }
        System.out.println("");
        System.out.println(mm.toString()+"前n项和为: "+Count1.toString() );

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘 关

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值