我需要在不使用 的情况下将两个非常大的数字相加BigInteger。我采用两个字符串参数,但下面的代码仅适用于长度相等的字符串,否则会抛出IndexOutOfBoundsException. 如何通过添加大数字(无论其长度如何)来解决这个问题?
public static String add(String a, String b) {
int carry = 0;
String result = "";
for (int i = a.length() - 1; i >= 0; i--) {
int digitA = a.charAt(i) - 48;
int digitB = b.charAt(i) - 48;
int resultingNumber = digitA + digitB + carry;
if (resultingNumber >= 10) {
result = (resultingNumber % 10) + result;
carry = 1;
} else {
result = resultingNumber + result;
carry = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Java 代码转换为 C#,但在处理 BigInteger 操作时遇到了问题。我找到了一些关于 C# 中 BigInteger 实现和 intValue 本身的资源。但是没有关于 C# 中 BigInteger.intValue 等价物的线索。Java中的定义是:
将此 BigInteger 转换为 int。这种转换类似于 Java™ 语言规范第 5.1.3 节中定义的从 long 到 int 的缩小原始转换:如果此 BigInteger 太大而无法放入 int,则仅返回低 32 位。注意此转换可能会丢失有关 BigInteger 值的整体大小的信息,并会返回具有相反符号的结果。
但是在 C# 中使用 (int) 获得类似的结果会导致错误:
对于 Int32,值要么太大要么太小。
我也尝试只使用低字节但没有成功如果有人帮忙,我们将不胜感激
我想向 BigInt 添加前导零,如果 BigInt 的位数少于 10 位,则应该添加填充零,并且我必须稍后用该数字进行计算。
到目前为止,这就是我的解决方案:
BigInteger bankAccountNumber = new BigInteger("999999999");
BigInteger zero = new BigInteger("10000000");
public void checksum(){
if(bankAccountNumber.toString().length()<10){
while(bankAccountNumber.toString().length()<10){
}
System.out.println(bankAccountNumber);
}
System.out.println(bankAccountNumber.toString().length());
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法想出一种方法,其中包括bankAccountNumber是一个带有前导0的实数,因为我必须稍后将数字与另一个BigInt结合起来,这应该包括前导零,我得到的只是一个带有前导的输出零,但我无法计算,所以这就是我的问题,谢谢你的帮助。
我正在尝试在 C 中添加大约 25 位数字。我得到的结果与预期的、可能的原因数据类型有点不同。
/* Online C Compiler and Editor */
#include <stdio.h>
int main()
{
long double result;
long double a;
long double b;
a = 51680708854858333333;
b = 83621143489848333333,
result = a + b;
printf("Hello, World!\n");
printf("can be written %.0Lf\n", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 您好我正在研究gcd和逆模运算的算法.
我必须使用BigInteger类,但我有一些问题.
那你能帮我吗?
问题是java programm不想用新的输入覆盖旧的BigInteger输入.
import java.math.BigInteger;
public class Gcd
{
public static void gcd(BigInteger a, BigInteger b){
BigInteger modulo = b;
BigInteger wert = a;
BigInteger zwischenwert1 = BigInteger.valueOf(1);
BigInteger zwischenwert2 = BigInteger.valueOf(0);
BigInteger zwischenwert3 = BigInteger.valueOf(0);
BigInteger zwischenwert4 = BigInteger.valueOf(1);
BigInteger negativ = BigInteger.valueOf(-1);
BigInteger q;
do{
q = modulo.divide(wert);
wert = modulo.subtract(wert.multiply(q));
zwischenwert3 = zwischenwert1.subtract(zwischenwert3.multiply(q));
zwischenwert4 = zwischenwert2.subtract(zwischenwert4.multiply(q));
modulo = negativ.multiply((wert.subtract(modulo)).divide(q));
zwischenwert1 = negativ.multiply((zwischenwert3.subtract(zwischenwert1)).divide(q));
zwischenwert2 = negativ.multiply((zwischenwert4.subtract(zwischenwert2)).divide(q));
}while((modulo.signum()>1)&&(wert.signum()>1));
System.out.println("gcd("+a+","+b+") = "+zwischenwert3+" * "+b+ " + "+ zwischenwert4+" …Run Code Online (Sandbox Code Playgroud) 是否可以采用大于C/C++提供的范围输入?是否可以接受大于无符号长整数的输入范围,甚至更大的范围为10 ^ 1000?如果可以用C/C++,请回答如何完成,谢谢.
我正在做一个会使用非常大的数字的程序,即使是最轻微的也会有太多的工作.我的问题是:big_integer比普通的int慢吗?
long number = 12;
BigInteger number2 = 12;
public void add()
{
number += 1;
}
public void add2()
{
number2 += 1;
}
Run Code Online (Sandbox Code Playgroud)
哪一个现在更快?
我需要计算一下:2894135 ^ 3787313 mod 4028033
正如你在下面看到的,我试图使用BigInteger因为我有非常大的数字.
import java.lang.Math;
import java.util.Scanner;
public class BigInteger extends Number implements Comparable<BigInteger>
{
public static void main(String[] args)
{
BigInteger result=new BigInteger(Math.pow(2894135,3787313) % 4028033);
System.out.println(result);
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
/tmp/java_Wcf144/BigInteger.java:19:错误:BigInteger不是抽象的,并且不会覆盖抽象方法doubleValue()中的数字公共类BigInteger extends Number实现Comparable ^ /tmp/java_Wcf144/BigInteger.java:24:错误:构造函数BigInteger类中的BigInteger不能应用于给定的类型;
BigInteger结果= new BigInteger(Math.pow(2894135,3787313)%4028033); ^ required:找不到参数:double reason:实际和形式参数列表长度不同2错误
所以我是Go的新手,对编程总体上缺乏经验,所以我希望我不会因为提出愚蠢的问题而再次投票.我正在通过项目euler问题和问题25"1000位Fibonacci数字"工作我遇到了似乎奇怪的行为.以下是我编写的导致此行为的代码.
package main
import (
"fmt"
"math/big"
)
func main() {
index := 2
l := new(big.Int)
pl := big.NewInt(1)
i := big.NewInt(1)
for {
l = i
i.Add(i, pl)
pl = l
index++
if len(i.String()) == 1000 {
break
}
}
fmt.Println(i, "\nindex: ", index)
}
Run Code Online (Sandbox Code Playgroud)
当然,这并没有产生正确的答案所以在确定为什么我发现我无意中发现了一种产生2的幂的简洁方法的过程中.我做了以下更改,这确实产生了正确的结果.
package main
import (
"fmt"
"math/big"
)
func main() {
index := 2
l := new(big.Int)
pl := big.NewInt(1)
i := big.NewInt(1)
for {
l.Set(i)
i.Add(i, pl)
pl.Set(l)
index++
if len(i.String()) == …Run Code Online (Sandbox Code Playgroud) 我使用Java的Big Integers创建了一个Fermat素性测试.但是,尽管没有出现错误并且一切看起来都很好,但对于任何输入(BigInteger.valueOf(3)除外)都不会返回true或false.
public static boolean isPrime (BigInteger n){
BigInteger counter=BigInteger.ZERO;
boolean isPrime=false;
if(n.equals(BigInteger.valueOf(2)))isPrime=true;
if(n.compareTo(BigInteger.valueOf(2))>0 && n.compareTo(BigInteger.valueOf(40))<0) {
for (BigInteger a=BigInteger.valueOf(2);a.compareTo(n.subtract(BigInteger.ONE))<0;a.add(BigInteger.ONE)) {
if (a.modPow(n.subtract(BigInteger.ONE),n).equals(BigInteger.ONE)) counter.add(BigInteger.ONE);
}
if (counter.equals(n.subtract(BigInteger.valueOf(3)))) isPrime = true;
}
else {
for (BigInteger a=BigInteger.valueOf(2);a.compareTo(BigInteger.valueOf(40))<=0;a.add(BigInteger.ONE)) {
if (a.modPow(n.subtract(BigInteger.ONE),n).equals(BigInteger.ONE)) counter.add(BigInteger.ONE);
}
if (counter.equals(BigInteger.valueOf(39))) isPrime = true;
}
return isPrime;
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是由Big Biggers引起的吗?