如何以不那么复杂的方式使用 BigInteger?
How do I use BigInteger in a less convoluted way?
所以在写完这个问题的标题后,我意识到我只需要声明一个 BigInteger 变量,就可以将它用作函数参数和 return 变量(是这样称呼的吗?)
public static void ex8(){
double time_duration = 0;
BigInteger fact; // Originally I also had an "arg" BigInteger
long l = 100000;
fact = BigInteger.valueOf(l); //I had arg = (...) instead of fact
double time_before = System.currentTimeMillis();
//originally it was fact = Matematica.factorial(arg);
fact = Matematica.factorial(fact);
double time_after = System.currentTimeMillis();
time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}
因此,如果我想使用 BigInteger,我似乎必须:
- 声明 BigInteger 变量
- 声明一个长变量
- 将long变量转换(?)为BigInteger并赋值
- 正常使用
问题:有什么办法可以简化这个过程吗?有什么方法可以正常使用 BigInteger 而无需转换?
您可以通过这种方式声明和初始化 BigInteger 变量:
BigInteger bi = new BigInteger(1000);
感谢我提交的评论,由于还没有人在主要 post 上正确回答,我会 post 自己。
BigInteger fact = new BigInteger("10000");
OR
BigInteger fact = BigInteger.valueOf(100000L);
于是就变成了
public static void ex8(){
double time_duration = 0;
BigInteger fact = new BigInteger("10000");
double time_before = System.currentTimeMillis();
fact = Matematica.factorial(fact);
double time_after = System.currentTimeMillis();
time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}
好多了。谢谢
在将它作为参数传递给 BigInteger.valueOf
之前,您不需要在变量中包含 long - 您可以直接将其内联。
我认为您提供的代码的最干净的编辑如下。请注意时间戳已更改为long类型声明,事实变量未被重用。
public static void ex8(){
BigInteger num = BigInteger.valueOf(100000L);
long time_before = System.currentTimeMillis();
BigInteger fact = Matematica.factorial(num);
long time_after = System.currentTimeMillis();
long time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}
所以在写完这个问题的标题后,我意识到我只需要声明一个 BigInteger 变量,就可以将它用作函数参数和 return 变量(是这样称呼的吗?)
public static void ex8(){
double time_duration = 0;
BigInteger fact; // Originally I also had an "arg" BigInteger
long l = 100000;
fact = BigInteger.valueOf(l); //I had arg = (...) instead of fact
double time_before = System.currentTimeMillis();
//originally it was fact = Matematica.factorial(arg);
fact = Matematica.factorial(fact);
double time_after = System.currentTimeMillis();
time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}
因此,如果我想使用 BigInteger,我似乎必须:
- 声明 BigInteger 变量
- 声明一个长变量
- 将long变量转换(?)为BigInteger并赋值
- 正常使用
问题:有什么办法可以简化这个过程吗?有什么方法可以正常使用 BigInteger 而无需转换?
您可以通过这种方式声明和初始化 BigInteger 变量:
BigInteger bi = new BigInteger(1000);
感谢我提交的评论,由于还没有人在主要 post 上正确回答,我会 post 自己。
BigInteger fact = new BigInteger("10000");
OR
BigInteger fact = BigInteger.valueOf(100000L);
于是就变成了
public static void ex8(){
double time_duration = 0;
BigInteger fact = new BigInteger("10000");
double time_before = System.currentTimeMillis();
fact = Matematica.factorial(fact);
double time_after = System.currentTimeMillis();
time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}
好多了。谢谢
在将它作为参数传递给 BigInteger.valueOf
之前,您不需要在变量中包含 long - 您可以直接将其内联。
我认为您提供的代码的最干净的编辑如下。请注意时间戳已更改为long类型声明,事实变量未被重用。
public static void ex8(){
BigInteger num = BigInteger.valueOf(100000L);
long time_before = System.currentTimeMillis();
BigInteger fact = Matematica.factorial(num);
long time_after = System.currentTimeMillis();
long time_duration = time_after - time_before;
System.out.println("Execution time: " + time_duration + " ms " + "AND\n" + "Factorial result: " + fact);
}