我正在尝试用Java生成大的素数.我使用BigIntegers.这是我在数组中生成和存储10个素数的代码.
public static void primeGenerator() {
BigInteger[] primeList = new BigInteger[10];
BigInteger startLine = new BigInteger("10");
int startPower = 6;
BigInteger endLine = new BigInteger("10");
int endPower = 9;
int j = 0;
for (BigInteger i = fastExp(startLine,startPower);
i.compareTo(fastExp(endLine,endPower)) <= 0;
i = i.add(BigInteger.ONE)) {
if (checkPrimeFermat(i) == true && j < 10) {
primeList[j] = i;
j++;
}
}
System.out.println(primeList[0]);
System.out.println(primeList[1]);
System.out.println(primeList[2]);
System.out.println(primeList[3]);
System.out.println(primeList[4]);
System.out.println(primeList[5]);
System.out.println(primeList[6]);
System.out.println(primeList[7]);
System.out.println(primeList[8]);
System.out.println(primeList[9]);
}
Run Code Online (Sandbox Code Playgroud)
我编写了自己的fastExp函数来更快地生成数字.这是我的其他功能.
public static BigInteger getRandomFermatBase(BigInteger n)
{
Random rand …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GMP库在C++中生成一个巨大的随机数,但是在查明语法时遇到了问题.这与我发现的其他例子略有不同,因为我需要为随机数设置一个楼层和天花板.这是我需要做的事情:
mpz_class high, low;
low = pow(2,199);
high = pow(2,210);
// code to use the high and low numbers to generate the random number
Run Code Online (Sandbox Code Playgroud)
我知道这不是很多,但是,我不知道在这一点上语法甚至是什么,我已经尝试了几件事,但我发现没有什么能让我告诉GMP使用高和数字生成的低范围.
思考?
我正在尝试将BigInteger数转换为二进制数.我使用while循环来减少BigInteger,直到它等于1,在循环运行时取余数.
循环的条件是:(decimalNum.intValue()> 1).
但程序只进行一次循环然后认为BigInteger小于/等于1,而实际上它大约是55193474935748.为什么会发生这种情况?
("inBinary"是一个ArrayList,用于保存循环中的余数.)
这是while循环:
while (decimalNum.intValue()>1){
inBinary.add(0, decimalNum.mod(new BigInteger("2")).intValue()); //Get remainder (0 or 1)
decimalNum = decimalNum.divide(new BigInteger("2")); //Reduce decimalNum
}
Run Code Online (Sandbox Code Playgroud) 我被困在代码的这一部分上.我有一个存储在输入中的字符串.
String input = ("831346848 2162638190 2014846560 1070589609 326439737");
Run Code Online (Sandbox Code Playgroud)
该字符串包含长整数.我试图通过将每个长整数转换为BigInteger来实现它们.例如,从字符串中,我需要这样做:
BigInteger bi1= new BigInteger("831346848");
Run Code Online (Sandbox Code Playgroud)
等等.输入字符串非常长,所以我需要把它放在某种循环中.在我暂时将它存储到bi1后,我需要执行b1.modPow(exp,mod).然后对字符串中的每个长整数重复这些步骤.那部分我理解,但我困惑的部分是如何将输入字符串放在循环中,以便将它存储在bi1中.
长整数由空格分隔,字符串中的每个长整数长度不同.
实现这个的最佳方法是什么?
我正在解决这个问题,即我们给出了数字N,它可以非常大,最多可以有 100000 位数字。
现在我想知道找到这些数字的最有效方法是什么,我认为在大数字中,我最多需要删除 3 位数字才能使数字被 3 整除。
我知道如果数字之和可以被三整除,这个数字可以被三整除,但我想不出我们如何使用它。
我的想法是对字符串进行蛮力并检查我们是否删除了该数字是否可以被 3 整除,但是我的解决方案在复杂的例子中失败了。请给我一些提示。
提前致谢。
朱莉娅如何计算大数字?
例如,这可以按预期工作:
julia> 10^18
1000000000000000000
Run Code Online (Sandbox Code Playgroud)
但是对于更大的数字,整数存在问题:
julia> 10^19
-8446744073709551616
julia> 10^20
7766279631452241920
Run Code Online (Sandbox Code Playgroud)
但是如果使用十进制数则它可以工作:
julia> 10.0^20
1.0e20
Run Code Online (Sandbox Code Playgroud)
你知道为什么吗?
import java.lang.Math;
import java.math.BigInteger;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
int e1 = 20, d = 13;
BigInteger C = BigDecimal.valueOf(e1).toBigInteger();
BigInteger po = C.pow(d);
System.out.println("pow is:" + po);
int num = 11;
BigInteger x = po;
BigInteger n = BigDecimal.valueOf(num).toBigInteger();
BigInteger p, q, m;
System.out.println("x: " + x);
q=(x / n);
p=(q * n);
m=(x - p);
System.out.println("mod is:" + m);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过寻找一些与之相关的答案,但无法解决。请有人告诉我这有什么问题。我将数据类型更改为整数,但幂函数不起作用。
这是我得到的错误:
error: bad operand types for binary operator '/' …Run Code Online (Sandbox Code Playgroud) 我试图总结 2 个给定数字之间的所有数字,不包括边界。例如,addNumbers("5", "8")由于 6+7=13,应该返回 13。这是我目前拥有的功能。
public static BigInteger addNumbers(String from, String to) {
BigInteger total = new BigInteger("0");
BigInteger startingBoundary = new BigInteger(from);
BigInteger finishingBoundary = new BigInteger(to);
if (startingBoundary.compareTo(finishingBoundary) < 0) {
startingBoundary = new BigInteger(from);
finishingBoundary = new BigInteger(to);
} else {
finishingBoundary = new BigInteger(from);
startingBoundary = new BigInteger(to);
}
while (startingBoundary.compareTo(finishingBoundary) != 0 ) {
System.out.println("Starting boundary:" + startingBoundary.intValue());
System.out.println("Finishing boundary: " + finishingBoundary.intValue());
total.add(startingBoundary);
System.out.println("total: "+total.intValue());
startingBoundary.add(new BigInteger("1"));
}
return total;
Run Code Online (Sandbox Code Playgroud)
} …
我想创建一个BinaryOperator<BigInteger> biOp以将BigInteger值相加。例如,我将有一个巨大的列表或不同BigInteger值的数组,我想使用循环和biOp.
例如,两个值的结果应该是这样的:
System.out.println(biOp.apply(BigInteger.ONE, BigInteger.ONE));
// outputs 2
Run Code Online (Sandbox Code Playgroud)
如何biOp正确创建或初始化?
biginteger ×10
java ×7
string ×2
add ×1
algorithm ×1
c++ ×1
conditional ×1
gmp ×1
julia ×1
math ×1
operators ×1
precision ×1
random ×1
while-loop ×1