当我的编译器告诉我时,我在F#中使用了BigInteger类的Pow函数:
不推荐使用此构造.此成员已被删除,以确保此类型与.NET 4.0类型System.Numerics.BigInteger二进制兼容
我觉得很公平,但我没有立即找到替代品.
有吗?我们应该只使用自己的Pow功能吗?并且(如何)它将在NET4.0中被替换?
我正在阅读一个文本文件,其中包含[1,10 ^ 100]范围内的数字.然后,我对每个数字执行一系列算术运算.我想只在数字超出int/long范围时才使用BigInteger.一种方法是计算字符串中有多少位数,如果有太多则切换到BigInteger.否则我只是使用原始算法,因为它更快.有没有更好的办法?
有没有什么理由为什么Java不能自动执行此操作,如果int太小,切换到BigInteger?这样我们就不用担心溢出了.
我对pow(指数)方法做了一些测试.不幸的是,我的数学技能不足以解决以下问题.
我正在使用此代码:
BigInteger.valueOf(2).pow(var);
Run Code Online (Sandbox Code Playgroud)
结果:
看到?2,500,000指数的计算速度几乎与2,000,000一样快.4,500,000的计算速度比4,000,000快得多.
这是为什么?
为了给你一些帮助,这里是BigInteger.pow(exponent)的原始实现:
public BigInteger pow(int exponent) {
if (exponent < 0)
throw new ArithmeticException("Negative exponent");
if (signum==0)
return (exponent==0 ? ONE : this);
// Perform exponentiation using repeated squaring trick
int newSign = (signum<0 && (exponent&1)==1 ? -1 : 1);
int[] baseToPow2 = this.mag;
int[] result = {1};
while (exponent != …Run Code Online (Sandbox Code Playgroud) 我在讨论是使用BigDecimal和BigInteger还是只使用BigDecimal来让我的生活更轻松,更少来回转换.在资源方面仅使用BigDecimal是否存在缺点?
如何仅使用原始数据类型和BigInteger或仅使用BigInteger来使我的生活更轻松,更少来回转换?
这是我面临的一个奇怪问题.当我将一些大数字传递给JS函数(自定义或内置)时,该值会自动增加1.例如,请参阅:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert(10466511635124471);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在我看到的警报中10466511635124472,而不是10466511635124471.有人可以解释这种行为吗?
我在FF 17.0.10和Chrome 12.0.742.122以及IE 8中检查了这个.
对于Erlang或的解决方案C / C++,请转到下面的试验4.
isqrt(N) when erlang:is_integer(N), N >= 0 ->
erlang:trunc(math:sqrt(N)).
Run Code Online (Sandbox Code Playgroud)
此实现使用sqrt()C库中的函数,因此它不适用于任意大整数(请注意,返回的结果与输入不匹配.正确的答案应该是12345678901234567890):
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1> erlang:trunc(math:sqrt(12345678901234567890 * 12345678901234567890)).
12345678901234567168
2>
Run Code Online (Sandbox Code Playgroud)
+仅使用Bigint ]isqrt2(N) when erlang:is_integer(N), N >= 0 ->
isqrt2(N, 0, 3, 0).
isqrt2(N, I, _, Result) when I >= N ->
Result;
isqrt2(N, …Run Code Online (Sandbox Code Playgroud) 是否有可能在BIGINT没有硬编码的情况下获得类型的最大值?
我知道限制是众所周知的,但我不想硬编码.
我正在使用以下代码创建一个BigInteger十六进制字符串并打印到输出.
package javaapplication2;
import java.math.BigInteger;
import javax.xml.bind.DatatypeConverter;
public class JavaApplication2 {
public static void main(String[] args) {
// Number in hexadecimal form
String HexString = "e04fd020ea3a6910a2d808002b30309d";
// Convertation from string to byte array
byte[] ByteArray = toByteArray(HexString);
// Creation of BigInteger from byte array
BigInteger BigNumber = new BigInteger(ByteArray);
// Print result
System.out.print(BigNumber + "\n");
}
public static String toHexString(byte[] array) {
return DatatypeConverter.printHexBinary(array);
}
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
}
Run Code Online (Sandbox Code Playgroud)
执行此代码后,我得到以下结果:
-42120883064304190395265794005525319523
但我希望看到这个结果: …
我有一个神秘的错误,用于减去各种长度的无符号整数的算法.它几乎适用于每对数字,但如果n不小于单元格中的位数(2^n +1)-(2^n - 1) <> 2.我无法理解为什么算法不起作用.
数字存储在"cellimal"系统中的数组中(base = 2 ^ bits),最低有效单元格为lowmem.ad1处的数组将从ad2处的数组中减去,两者都是相同的维度len,结果应存储在ad2:
false borrow ! len 0
do i ad2 + @ borrow @ +
i ad1 + @ 2dup u< dup borrow !
if 1 swap 0 d- drop \ subtraction with borrow
else - \ subtraction without borrow
then i ad2 + ! cell
+loop
Run Code Online (Sandbox Code Playgroud)
注意:我认为错误来自于从包含零值的单元格借用...
也许有人可以纠正算法?
我正在导入一个1700万位长的大型文本文件,我正在使用此代码:
BufferedReader reader = new BufferedReader(new FileReader("test2.txt"));
String line = reader.readLine();
System.out.println("Done");
BigInteger num = new BigInteger(line);
System.out.println("Done Again");
Run Code Online (Sandbox Code Playgroud)
它几乎立即加载文件并打印出来'Done'但是String要转换成a 需要很长时间(大约一个小时)BigInteger,有什么办法可以加快速度并快速加载数字吗?
biginteger ×10
java ×5
algorithm ×2
pow ×2
arrays ×1
bigdecimal ×1
bigint ×1
erlang ×1
exponent ×1
f# ×1
forth ×1
gforth ×1
javascript ×1
optimization ×1
performance ×1
postgresql ×1
square-root ×1
types ×1