java.math.BigInteger.isProbablePrime()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(291)

本文整理了Java中java.math.BigInteger.isProbablePrime()方法的一些代码示例,展示了BigInteger.isProbablePrime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.isProbablePrime()方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:isProbablePrime

BigInteger.isProbablePrime介绍

[英]Tests whether this BigInteger is probably prime. If trueis returned, then this is prime with a probability greater than 1 - 1/2certainty). If false is returned, then this is definitely composite. If the argument certainty
[中]测试这个大整数是否可能是素数。如果返回true,则这是素数,概率大于1-1/2确定性)。如果返回false,那么这肯定是复合的。如果论点是肯定的

代码示例

代码示例来源:origin: google/guava

@GwtIncompatible // isPrime is GWT-incompatible
public void testIsPrimeOnUniformRandom() {
 Random rand = new Random(1);
 for (int bits = 10; bits < 63; bits++) {
  for (int i = 0; i < 2000; i++) {
   // A random long between 0 and Long.MAX_VALUE, inclusive.
   long l = rand.nextLong() & ((1L << bits) - 1);
   assertEquals(BigInteger.valueOf(l).isProbablePrime(100), LongMath.isPrime(l));
  }
 }
}

代码示例来源:origin: stackoverflow.com

boolean bigIntSays = bigInt.isProbablePrime(certainty);
if(isPrime(i) != bigIntSays) {
  System.out.println("ERROR: isProbablePrime(" + certainty + ") returns "

代码示例来源:origin: google/guava

@GwtIncompatible // isPrime is GWT-incompatible
public void testIsPrimeSmall() {
 // Check the first 1000 integers
 for (int i = 2; i < 1000; i++) {
  assertEquals(BigInteger.valueOf(i).isProbablePrime(100), LongMath.isPrime(i));
 }
}

代码示例来源:origin: google/guava

@GwtIncompatible // isPrime is GWT-incompatible
public void testIsPrimeManyConstants() {
 // Test the thorough test inputs, which also includes special constants in the Miller-Rabin
 // tests.
 for (long l : POSITIVE_LONG_CANDIDATES) {
  assertEquals(BigInteger.valueOf(l).isProbablePrime(100), LongMath.isPrime(l));
 }
}

代码示例来源:origin: internetarchive/heritrix3

while(!BigInteger.valueOf(nLockTables).isProbablePrime(Integer.MAX_VALUE)) {
  nLockTables--;

代码示例来源:origin: linkedin/indextank-engine

/**
 * Returns a prime number, at least twice as large as needed. This should
 * minimize hash collisions. Since all the hash keys are known up front,
 * the capacity could be tweaked until there are no collisions, but this
 * technique is easier and deterministic.
 */
private static int hashCapacity(int min) {
  BigInteger capacity = BigInteger.valueOf(min * 2 + 1);
  while (!capacity.isProbablePrime(10)) {
    capacity = capacity.add(BigInteger.valueOf(2));
  }
  return capacity.intValue();
}

代码示例来源:origin: spring-projects/spring-integration

public static ResultSet getPrimes(int beginRange, int endRange) throws SQLException {
  SimpleResultSet rs = new SimpleResultSet();
  rs.addColumn("PRIME", Types.INTEGER, 10, 0);
  for (int i = beginRange; i <= endRange; i++) {
    if (new BigInteger(String.valueOf(i)).isProbablePrime(100)) {
      rs.addRow(i);
    }
  }
  return rs;
}

代码示例来源:origin: pholser/junit-quickcheck

@Property(trials = 10) public void factorsPassPrimalityTest(
  BigInteger n) {
  assumeThat(n, greaterThan(ZERO));
  for (BigInteger each : PrimeFactors.of(n))
    assertTrue(each.isProbablePrime(100));
}

代码示例来源:origin: axkr/symja_android_library

/**
 * Prime checking routine
 * 
 * @param N
 * @return return codes: 0 = Number is prime. 1 = Number is composite.
 */
private static int ProbabilisticPrimeTest(BigInteger N) {
  return N.isProbablePrime(32) ? 0 : 1;
}

代码示例来源:origin: net.openhft/koloboke-impl-jdk8

public static boolean isPrime(long n) {
  if (!BigInteger.valueOf(n).isProbablePrime(10))
    return false;
  if (n > 2L && (n & 1L) == 0)
    return false;
  for (long i = 3L; i * i <= n; i += 2L) {
    if (n % i == 0)
      return false;
  }
  return true;
}

代码示例来源:origin: net.openhft/koloboke-impl-jdk6-7

public static boolean isPrime(long n) {
  if (!BigInteger.valueOf(n).isProbablePrime(10))
    return false;
  if (n > 2L && (n & 1L) == 0)
    return false;
  for (long i = 3L; i * i <= n; i += 2L) {
    if (n % i == 0)
      return false;
  }
  return true;
}

代码示例来源:origin: com.koloboke/koloboke-impl-common-jdk8

public static boolean isPrime(long n) {
  if (!BigInteger.valueOf(n).isProbablePrime(10))
    return false;
  if (n > 2L && (n & 1L) == 0)
    return false;
  for (long i = 3L; i * i <= n; i += 2L) {
    if (n % i == 0)
      return false;
  }
  return true;
}

代码示例来源:origin: williamfiset/data-structures

@Override
protected void adjustCapacity() {
 while(!(new BigInteger(String.valueOf(capacity)).isProbablePrime(20))) {
  capacity++;
 }
}

代码示例来源:origin: stackoverflow.com

for (int i=2; i <=100; i++) {
     BigInteger bi = new BigInteger(""+i);
     if (bi.isProbablePrime(9999)) {
       System.out.println("Prime: " + i);
     }
   }

代码示例来源:origin: stackoverflow.com

public class Prime { 
  public static void main(String[] args){
    long start = System.nanoTime();
    System.out.println("enter a number:");
    Scanner scanner = new Scanner(System.in);
    BigInteger bigInt = scanner.nextBigInteger();
    boolean prime = bigInt.isProbablePrime(10);
    System.out.println(prime);
  }
}

代码示例来源:origin: org.appdapter/ext.bundle.math.symja_jas

public boolean apply(final IExpr obj) {
  try {
    final BigInteger value = ((IInteger) obj).getBigNumerator();
    return value.isProbablePrime(32);
  } catch (final Exception e) {
    if (Config.DEBUG) {
      e.printStackTrace();
    }
  }
  return false;
}

代码示例来源:origin: com.madgag.spongycastle/core

protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key) throws IOException
  {
    // TODO What is the minimum bit length required?
    // key.getModulus().bitLength();

    if (!key.getExponent().isProbablePrime(2))
    {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }

    return key;
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key)
    throws IOException
  {
    // TODO What is the minimum bit length required?
    // key.getModulus().bitLength();

    if (!key.getExponent().isProbablePrime(2))
    {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }

    return key;
  }
}

代码示例来源:origin: com.madgag.spongycastle/core

protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key)
    throws IOException
  {
    // TODO What is the minimum bit length required?
    // key.getModulus().bitLength();

    if (!key.getExponent().isProbablePrime(2))
    {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }

    return key;
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

protected RSAKeyParameters validateRSAPublicKey(RSAKeyParameters key) throws IOException
  {
    // TODO What is the minimum bit length required?
    // key.getModulus().bitLength();

    if (!key.getExponent().isProbablePrime(2))
    {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }

    return key;
  }
}

相关文章