java.math.BigInteger

BigInteger定义

        public class BigInteger extends Number implementsComparable<BigInteger>

        不可变的任意精度的整数。所有操作中,都以二进制补码形式表示BigInteger(如Java的基本整数类型)。BigInteger提供所有Java的基本整数操作符的对应物,并提供java.lang.Math的所有相关方法。另外,BigInteger还提供以下运算:模算术、GCD计算、质数测试、素数生成、位操作以及一些其他操作。

        算术运算的语义完全模仿Java整数算术运算符的语义,如TheJava Language Specification 中所定义的。例如,以零作为除数的除法抛出ArithmeticException,而负数除以正数的除法则产生一个负(或零)的余数。Spec中关于溢出的细节都被忽略了,因为BigIntegers所设置的实际大小能适应操作结果的需要。

        位移操作的语义扩展了Java的位移操作符的语义以允许产生负位移距离。带有负位移距离的右移操作会导致左移操作,反之亦然。忽略无符号的右位移运算符(>>>),因为该操作与由此类提供的“无穷大的词大小”抽象结合使用时毫无意义。

        逐位逻辑运算的语义完全模仿Java的逐位整数运算符的语义。在执行操作之前,二进制运算符(and、or、xor)对两个操作数中的较短操作数隐式执行符号扩展。

        比较操作执行有符号的整数比较,类似于Java的关系运算符和相等性运算符执行的比较。提供的模算术操作用来计算余数、求幂和乘法可逆元。这些方法始终返回非负结果,范围在 0 和 (modulus - 1)(包括)之间。

        位操作对其操作数的二进制补码表示形式的单个位进行操作。如有必要,操作数会通过扩展符号来包含指定的位。单一位操作不能产生与正在被操作的BigInteger符号不同的BigInteger,因为它们仅仅影响单个位,并且此类提供的“无穷大词大小”抽象可保证在每个BigInteger前存在无穷多的“虚拟符号位”数。

        当为任何输入参数传递 null对象引用时,此类中的所有方法和构造方法都将抛出 NullPointerException


BigInteger常用方法

        BigInteger不是基本数据类型之一,它其实更像String,是Java里的一个类,然而它的初始化方式却没有String那么方便可以直接赋值,而是跟其他自定义的类一样,要调用它的构造器进行初始化。这个类的取值范围原则上是没有上限的,取决于你的计算机的内存,它的构造器有以下几种:

        1、BigInteger(byte[] val):将包含BigInteger的二进制补码表示形式的byte数组转换为 BigInteger。

        2、BigInteger(int signum, byte[] magnitude):将BigInteger的符号-数量表示形式转换为BigInteger。

        3、BigInteger(int bitLength, int certainty, Random rnd):构造一个随机生成的正BigInteger,它可能是一个具有指定bitLength的素数。

        4、BigInteger(int numBits, Random rnd):构造一个随机生成的BigInteger,它是在0到(2^numBits -1)(包括)范围内均匀分布的值。

        5BigInteger(String val):将BigInteger的十进制字符串表示形式转换为BigInteger

        6、BigInteger(String val, int radix):将指定基数的BigInteger的字符串表示形式转换为BigInteger。

        并且在Biginteger类中还预定义了三个常量字段,分别是:staticBigInteger ONE BigInteger的常量 1static BigInteger TENBigInteger的常量10static BigInteger ZEROBigInteger的常量 0

        大数的加减乘除也不能使用+、-、*、/这些运算符号,BigInteger类没有对这些运算符号进行重定义,取而代之的是用一些方法来代替,比如add()、subtract()、mutiply()、divide()这四种方法。下面列举了BigInteger类中的常用方法。


        BigInteger abs() 返回大整数的绝对值

        BigInteger add(BigInteger val)返回两个大整数的和

        BigInteger and(BigInteger val) 返回两个大整数的按位与的结果

        BigInteger andNot(BigInteger val) 返回两个大整数与非的结果

        BigInteger divide(BigInteger val) 返回两个大整数的商

        double doubleValue()   返回大整数的double类型的值

        float floatValue()   返回大整数的float类型的值

        BigInteger gcd(BigInteger val) 返回大整数的最大公约数

        int intValue() 返回大整数的整型值

        long longValue() 返回大整数的long型值

        BigInteger max(BigInteger val)返回两个大整数的最大者

        BigInteger min(BigInteger val)返回两个大整数的最小者

        BigInteger mod(BigInteger val)用当前大整数对val求模

        BigInteger multiply(BigInteger val)返回两个大整数的积

        BigInteger negate() 返回当前大整数的相反数

        BigInteger not() 返回当前大整数的非

        BigInteger or(BigInteger val) 返回两个大整数的按位或

        BigInteger pow(int exponent)返回当前大整数的exponent次方

        BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数

        BigInteger leftShift(int n) 将当前大整数左移n位后返回

        BigInteger rightShift(int n) 将当前大整数右移n位后返回

        BigInteger subtract(BigInteger val)返回两个大整数相减的结果

        byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中

        String toString()将当前大整数转换成十进制的字符串形式

        BigInteger xor(BigInteger val) 返回两个大整数的异或

        int compareTo(BigInteger val)将此 BigInteger与指定的 BigInteger进行比较。小于则返回-1,等于则返回0,大于则返回1

        boolean equals(Object x)比较此 BigInteger与指定的 Object的相等性。

        static BigInteger valueOf(long val)返回其值等于指定 long的值的 BigInteger

        int hashCode() 返回此 BigInteger 的哈希码。


程序实例

package com.zxt.test2;
 
import java.math.BigInteger;
 
/**
 * 水仙花数(Narcissistic number):也被称为超完全数字不变数(pluperfect digital invariant,
 * PPDI)、自恋数、自幂数等
 *
 * 水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。
 *
 * 21位水仙花数
 */
 
// 小数据规模时,可以直接根据定义求解,当数据变大时,则使用BigInteger来求解
public classNarcissistic {
 
    // 指定需要求解的位数
    private static int size = 7;
    // 用一个BigInteger数组来存储0-9的size次方的值
    private static BigInteger[] base = new BigInteger[10];
 
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();// 程序开始时间
 
        for (int i = 0; i < base.length; i++) {
            base[i] = calcu(i, size);
        }
 
        // 每个数字出现几次(那就是0-9出现的次数与0-9的size次方的乘积之和与原数比较,若相等那就是水仙花数)
        int[] usedTimes = new int[10];
        BigIntegerk = BigInteger.TEN.pow(size - 1);
        // compareTo方法来比较,小于则返回-1,等于则返回0,大于则返回1
        while (k.compareTo(BigInteger.TEN.pow(size)) == -1) {
            getUsedTimes(usedTimes, k);
            test(usedTimes, base, k);
 
            k = k.add(BigInteger.ONE);
        }
 
        long endTime = System.currentTimeMillis();// 程序结束时间
        System.out.println((endTime - startTime) / 1000f + "秒"); // 运行总时
    }
 
    // 统计BigInteger中0-9数字出现的次数
    private static void getUsedTimes(int[] usedTimes, BigInteger num) {
        Stringstr = num.toString();
 
        for (int i = 0; i < usedTimes.length; i++) {
            usedTimes[i] = 0;
        }
 
        for (int i = 0; i < str.length(); i++) {
            usedTimes[str.charAt(i) - '0']++;
        }
    }
 
    // 根据统计得到的次数,判断该BigInteger是否是一个水仙花数
    private static void test(int[] usedTimes, BigInteger[] base, BigInteger num) {
        BigIntegerbn = BigInteger.ZERO;
 
        for (int i = 0; i < usedTimes.length; i++) {
            bn = bn.add(base[i].multiply(BigInteger.valueOf(usedTimes[i])));
        }
 
        // BigInteger判断相等可以使用compareTo或者equals方法(equals方法返回布尔值)
        if (bn.equals(num)) {
            System.out.println(num);
        }
    }
 
    // 计算某数的size次方(用BigInteger存储)
    private static BigInteger calcu(int num, int size) {
 
        // BigInteger res = BigInteger.ONE;
        // for (int i = 0; i < size; i++) {
        // BigInteger.valueOf(long val):返回其值等于指定 long 的值的 BigInteger。
        // res = res.multiply(BigInteger.valueOf(num));
        // }
 
        // return res;
 
        // BigInteger pow(int exponent):返回其值为 (this^exponent) 的 BigInteger。
        return BigInteger.valueOf(num).pow(size);
    }
}

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值