In an assignment for class we have to make a loop to test for prime with a big integer WITHOUT using isProbablePrime. This is what we have.
在类的赋值中,我们必须创建一个循环来测试素数,并使用一个大整数而不使用isProbablePrime。这就是我们所拥有的。
package src.bigInteger;
import java.math.BigInteger;
public class BigIntegerTester {
BigInteger bi = new BigInteger();
public boolean isPrime(BigInteger number) {
boolean result = true;
if (number == 2) result = true;
for (int i = 3; i <= number/2; i += 2) {
if (number % i == 0) {result = false;}
}
return result;
}
}
How can we get this to work?
我们怎样才能让它发挥作用?
1 个解决方案
#1
1
- You cannot compare
BigInteger
using==
. You should be comparing it against anotherBigInteger
Object usingBigInteger.compareTo(BigInteger val)
. If you want to compare as anint
, for example, you can useBigInteger.intValue()
. - 您无法使用==来比较BigInteger。您应该使用BigInteger.compareTo(BigInteger val)将其与另一个BigInteger对象进行比较。例如,如果要比较为int,则可以使用BigInteger.intValue()。
- You cannot perform arithmetic operations on the
BigInteger
Object (number/2
).BigInteger
has methods that will perform these operations,BigInteger.divide(BigInteger val)
- 您不能对BigInteger对象(数字/ 2)执行算术运算。 BigInteger有一些方法可以执行这些操作,BigInteger.divide(BigInteger val)
- Once you have established the number is a Prime, you should either
break
orreturn
from the for loop. - 一旦你确定了数字是一个Prime,你应该从for循环中断或返回。
- There isn't much point using
BigInteger
as this algorithm will not scale well. If you were to use a number too large to be held in anint
, the computation time would be incredibly long. - 使用BigInteger并没有多大意义,因为这种算法不能很好地扩展。如果您使用的数字太大而无法保存在int中,则计算时间将非常长。
- The second line of the
isPrime(...)
method, (if (number == 2) result = true;
), is redundant. - isPrime(...)方法的第二行(if(number == 2)result = true;)是多余的。
- Your algorithm incorrectly classifies any power of 2, where the power is greater than 1, as prime.
- 您的算法错误地将功率大于1的2的幂幂归类为素数。
- 1 is not prime.
- 1不是素数。
There are many resources available that provide more efficient techniques to calculating whether an integer is a prime; you may want to give some of these a read.
有许多资源可用于提供更有效的技术来计算整数是否为素数;你可能想要给它们一些阅读。
#1
1
- You cannot compare
BigInteger
using==
. You should be comparing it against anotherBigInteger
Object usingBigInteger.compareTo(BigInteger val)
. If you want to compare as anint
, for example, you can useBigInteger.intValue()
. - 您无法使用==来比较BigInteger。您应该使用BigInteger.compareTo(BigInteger val)将其与另一个BigInteger对象进行比较。例如,如果要比较为int,则可以使用BigInteger.intValue()。
- You cannot perform arithmetic operations on the
BigInteger
Object (number/2
).BigInteger
has methods that will perform these operations,BigInteger.divide(BigInteger val)
- 您不能对BigInteger对象(数字/ 2)执行算术运算。 BigInteger有一些方法可以执行这些操作,BigInteger.divide(BigInteger val)
- Once you have established the number is a Prime, you should either
break
orreturn
from the for loop. - 一旦你确定了数字是一个Prime,你应该从for循环中断或返回。
- There isn't much point using
BigInteger
as this algorithm will not scale well. If you were to use a number too large to be held in anint
, the computation time would be incredibly long. - 使用BigInteger并没有多大意义,因为这种算法不能很好地扩展。如果您使用的数字太大而无法保存在int中,则计算时间将非常长。
- The second line of the
isPrime(...)
method, (if (number == 2) result = true;
), is redundant. - isPrime(...)方法的第二行(if(number == 2)result = true;)是多余的。
- Your algorithm incorrectly classifies any power of 2, where the power is greater than 1, as prime.
- 您的算法错误地将功率大于1的2的幂幂归类为素数。
- 1 is not prime.
- 1不是素数。
There are many resources available that provide more efficient techniques to calculating whether an integer is a prime; you may want to give some of these a read.
有许多资源可用于提供更有效的技术来计算整数是否为素数;你可能想要给它们一些阅读。