标签: biginteger

随机化BigInteger

我想随机化一个BigInteger.目的是从1到8180385048中选择一个数字.虽然,从我注意到,BigInteger(BitLen,Random)从n到X 2 -1,我想要一些不可预测的数字.我试图制作一个可以做到这一点的方法,但是我一直遇到错误并最终放弃在这里询问.:P有没有人对如何做这个有任何建议?

java random biginteger

4
推荐指数
1
解决办法
4034
查看次数

BigDecimal在小数点后有2个分数?

我希望在小数点后面有一个格式为2位小数的BigDecimal.总是.

根据一些布尔检查,我有时会返回一个BigDecimal.ZERO; 在这种情况下,虽然我设置了JSF页面,但它只显示"0" <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>.

我不明白这一点,但是我怎么能强制执行2位数呢?

java jsf biginteger

4
推荐指数
1
解决办法
2万
查看次数

java.math.BigInteger的用法是错误的吗?

我玩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.为什么?

java biginteger

4
推荐指数
1
解决办法
206
查看次数

Java随机数生成器

是否可以让用户选择随机数的位数,特别是随机的Big Integer?例如,如果用户希望它长15位,则随机数生成器将仅生成15位长的大整数.

java random biginteger

4
推荐指数
1
解决办法
2万
查看次数

我需要在C#中使用非常大的数组长度(大小)

public double[] result = new double[ ??? ];
Run Code Online (Sandbox Code Playgroud)

我存储的结果和结果的总数大于2,147,483,647,即max int32.

我尝试过biginteger,ulong等,但他们都给了我错误.

如何扩展可以存储> 50,147,483,647结果(双)的数组的大小?

谢谢...

c# arrays size biginteger

4
推荐指数
2
解决办法
1万
查看次数

使用Java 8的整数列表的总和

我正在玩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)

java biginteger java-8

4
推荐指数
2
解决办法
9569
查看次数

我可以在C#中找到BigInteger的位数吗?

我正在解决这个问题,他们要求第一个斐波纳契数为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).我得到以下运行时间:

  • 原始方法:00:00:00.0961957
  • StringMethod:00:00:00.1535350
  • BigIntegerLogMethod:00:00:00.0387479
  • MaxLimitMethod:00:00:00.0019509

程序

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)

c# arrays biginteger

4
推荐指数
2
解决办法
1448
查看次数

BigInteger.Parse()大量读取麻烦

目前我正在尝试做这个挑战(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的合适方法是什么?

c# parsing biginteger

4
推荐指数
2
解决办法
486
查看次数

Julia 中 2 个正数相乘得到负积

在 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
我确实遇到了这个问题,对(大)孪生素数进行了一些数学计算。

integer biginteger multiplication int128 julia

4
推荐指数
1
解决办法
201
查看次数

有没有一种有效的方法可以在不调用 ToString 的情况下获取 BigInteger 的长度?

考虑以下代码:

BigInteger value = BigInteger.Parse("9876543210987654321098765432109876543210");
Console.WriteLine(value.ToString().Length); // 40
Run Code Online (Sandbox Code Playgroud)

有没有更有效的方法来获取号码的长度而不需要打电话ToString?

c# biginteger

4
推荐指数
1
解决办法
289
查看次数

标签 统计

biginteger ×10

java ×5

c# ×4

arrays ×2

random ×2

int128 ×1

integer ×1

java-8 ×1

jsf ×1

julia ×1

multiplication ×1

parsing ×1

size ×1