我想随机化一个BigInteger.目的是从1到8180385048中选择一个数字.虽然,从我注意到,BigInteger(BitLen,Random)从n到X 2 -1,我想要一些不可预测的数字.我试图制作一个可以做到这一点的方法,但是我一直遇到错误并最终放弃在这里询问.:P有没有人对如何做这个有任何建议?
我希望在小数点后面有一个格式为2位小数的BigDecimal.总是.
根据一些布尔检查,我有时会返回一个BigDecimal.ZERO; 在这种情况下,虽然我设置了JSF页面,但它只显示"0" <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>.
我不明白这一点,但是我怎么能强制执行2位数呢?
我玩java.math.BigInteger.这是我的java类,
public class BigIntegerTest{
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为结果是555000.但实际是0.为什么?
是否可以让用户选择随机数的位数,特别是随机的Big Integer?例如,如果用户希望它长15位,则随机数生成器将仅生成15位长的大整数.
public double[] result = new double[ ??? ];
Run Code Online (Sandbox Code Playgroud)
我存储的结果和结果的总数大于2,147,483,647,即max int32.
我尝试过biginteger,ulong等,但他们都给了我错误.
如何扩展可以存储> 50,147,483,647结果(双)的数组的大小?
谢谢...
我正在玩Java 8,并使用程序比较Java 6中的一些东西来计算大型列表中偶数的总和
Java 8
public class ListOperationsJava8 {
static List<BigInteger> list = new LinkedList<>();
public static void main(String[] args) {
createList();
long start = System.currentTimeMillis();
/*System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToInt(BigInteger::intValue).sum()); --> gives result -1795017296 */
System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToLong(BigInteger::longValue).sum());
long end = System.currentTimeMillis();
System.out.println("Time taken using Java 8: " + (end - start) + " ms");
}
private static void createList() {
for (int i = 0; i < 100000; i++) {
list.add(new BigInteger(String.valueOf(i)));
}
} …Run Code Online (Sandbox Code Playgroud) 我正在解决这个问题,他们要求第一个斐波纳契数为1000位的索引,我的第一个想法是类似于:
BigInteger x = 1;
BigInteger y = 1;
BigInteger tmp = 0;
int currentIndex = 2;
while (x.NoOfDigits < 1000)
{
tmp = x + y;
y = x;
x = tmp;
currentIndex++;
}
return currentIndex;
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,没有方法可以计算BigInteger的位数.这是真的?绕过它的一种方法是使用BigInteger的.ToString().Length方法,但我被告知字符串处理很慢.
BigInteger也有一个.ToByteArray(),我想把BigInteger转换成一个字节数组并检查该数组的长度 - 但我不认为这唯一地决定了BigInteger中的位数.
为了它的价值,我实现了另一种解决方法,即手动将Fibonacci数存储在数组中,并在数组填满后立即停止,并将其与基于.ToString的方法进行比较,大约为2.5时间慢,但第一种方法需要0.1秒,这也似乎很长一段时间.
编辑:我已经在下面的答案中测试了这两个建议(一个是BigInteger.Log,一个是MaxLimitMethod).我得到以下运行时间:
程序
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Stopwatch clock = new Stopwatch();
clock.Start();
int index1 = Algorithms.IndexOfNDigits(1000);
clock.Stop();
var elapsedTime1 = …Run Code Online (Sandbox Code Playgroud) 目前我正在尝试做这个挑战(http://cryptopals.com/sets/1/challenges/1),我在使用C#完成任务时遇到了一些麻烦.我似乎无法将数字解析成一个大整数.
所以代码如下所示:
string output = "";
BigInteger hexValue = BigInteger.Parse("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6");
output = Convert.ToBase64String(hexValue.ToByteArray());
Console.WriteLine(hexValue);
Console.WriteLine(output);
Console.ReadKey();
return "";
Run Code Online (Sandbox Code Playgroud)
目前我遇到的问题是当我运行程序时它失败并出现错误
System.FormatException:'无法解析该值.' 而且我不完全确定为什么.
那么,从字符串中获取大整数到BigInt的合适方法是什么?
在 Julia 1.7.2 中,3037000691和3037000693的乘法返回负积-9223370870501072753。我期望的产品是9223373203208478863。
function m(a::BigInt, b::BigInt)::BigInt
a * b
end
Run Code Online (Sandbox Code Playgroud)
该函数给出相同的负结果。
关于如何在 Julia 1.7.2 中获得正确答案有什么想法吗3037000691 * 3037000693?
PS
我确实遇到了这个问题,对(大)孪生素数进行了一些数学计算。
考虑以下代码:
BigInteger value = BigInteger.Parse("9876543210987654321098765432109876543210");
Console.WriteLine(value.ToString().Length); // 40
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来获取号码的长度而不需要打电话ToString?