Tag: biginteger

BigInteger.toString方法正在删除前导0

我正在尝试使用MessageDigest生成MD5总和。 我有以下代码。 byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); output = bigInt.toString(16); 这不返回32个字符串,而是返回31个字符的字符串8611c0b0832bce5a19ceee626a403a7 预期字符串是08611c0b0832bce5a19ceee626a403a7 输出中缺少前导0。 我尝试了另一种方法 byte[] md5sum = digest.digest(); output = new String(Hex.encodeHex(md5sum)); 输出正如预期的那样。 我检查了doc,Integer.toString根据它进行了转换 使用Character.forDigit提供的数字到字符映射,如果合适,前缀为减号。 并在Character.forDigit方法 如果0 <= digit <radix,则数字参数有效。 有人可以告诉我两种方法有何不同以及为什么前导0被删除?

StackOverflowError计算BigInteger的阶乘?

我正在尝试编写一个Java程序来计算大数的阶乘。 似乎BigInteger无法容纳这么大的数字。 以下是我写的(直截了当的)代码。 public static BigInteger getFactorial(BigInteger num) { if (num.intValue() == 0) return BigInteger.valueOf(1); if (num.intValue() == 1) return BigInteger.valueOf(1); return num.multiply(getFactorial(num.subtract(BigInteger.valueOf(1)))); } 上述程序在5022中处理的最大数量,之后程序抛出StackOverflowError 。 有没有其他方法来处理它?

BigInteger.pow(BigInteger的)?

我正在玩Java中的数字,想看看我能做多少。 我的理解是BigInteger可以容纳一些无限大小,只要我的计算机有足够的内存来容纳这样的数字,对吗? 我的问题是BigInteger.pow只接受一个int,而不是另一个BigInteger,这意味着我只能使用一个最多2,147,483,647的数字作为指数。 是否可以使用BigInteger类? BigInteger.pow(BigInteger) 谢谢。

BigInteger:计算可伸缩方法中的小数位数

我需要计算BigInteger的小数位数。 例如: 99返回2 1234返回4 9999返回4 12345678901234567890返回20 我需要为具有184948十进制数字的BigInteger执行此操作。 我怎样才能快速,可扩展? convert-to-String方法很慢: public String getWritableNumber(BigInteger number) { // Takes over 30 seconds for 184948 decimal digits return “10^” + (number.toString().length() – 1); } 这种循环 – 十分之一的方法甚至更慢: public String getWritableNumber(BigInteger number) { int digitSize = 0; while (!number.equals(BigInteger.ZERO)) { number = number.divide(BigInteger.TEN); digitSize++; } return “10^” + (digitSize – […]

BigInteger的对数

我有一个BigInteger号码,例如超过2 64 。 现在我想计算该BigInteger数的对数,但BigInteger.log()方法不存在。 如何计算我的大BigInteger值的(自然)对数?

如何使用BigInteger?

我有这段代码,但是没有用: BigInteger sum = BigInteger.valueOf(0); for(int i = 2; i < 5000; i++) { if (isPrim(i)) { sum.add(BigInteger.valueOf(i)); } } sum变量总是0.我做错了什么?