public void run() {
   try {
     BigInteger p = BigInteger.ONE;
     while (!cancelled) queue.put(p = p.nextProbablePrime());
   } catch (InterruptedException consumed) {
   }
 }
  public static void nextProbablePrime() throws Exception {
    int failCount = 0;
    BigInteger p1, p2, p3;
    p1 = p2 = p3 = ZERO;

    // First test nextProbablePrime on the low range starting at zero
    for (int i = 0; i < primesTo100.length; i++) {
      p1 = p1.nextProbablePrime();
      if (p1.longValue() != primesTo100[i]) {
        System.err.println("low range primes failed");
        System.err.println("p1 is " + p1);
        System.err.println("expected " + primesTo100[i]);
        failCount++;
      }
    }

    // Test nextProbablePrime on a relatively small, known prime sequence
    p1 = BigInteger.valueOf(aPrimeSequence[0]);
    for (int i = 1; i < aPrimeSequence.length; i++) {
      p1 = p1.nextProbablePrime();
      if (p1.longValue() != aPrimeSequence[i]) {
        System.err.println("prime sequence failed");
        failCount++;
      }
    }

    // Next, pick some large primes, use nextProbablePrime to find the
    // next one, and make sure there are no primes in between
    for (int i = 0; i < 100; i += 10) {
      p1 = BigInteger.probablePrime(50 + i, rnd);
      p2 = p1.add(ONE);
      p3 = p1.nextProbablePrime();
      while (p2.compareTo(p3) < 0) {
        if (p2.isProbablePrime(100)) {
          System.err.println("nextProbablePrime failed");
          System.err.println("along range " + p1.toString(16));
          System.err.println("to " + p3.toString(16));
          failCount++;
          break;
        }
        p2 = p2.add(ONE);
      }
    }

    report("nextProbablePrime", failCount);
  }
 public void run() {
   try {
     BigInteger p = BigInteger.ONE;
     while (!Thread.currentThread().isInterrupted()) queue.put(p = p.nextProbablePrime());
   } catch (InterruptedException consumed) {
     /* Allow thread to exit */
   }
 }
  public static Set<BigInteger> createPrimesSet(int max) {
    Set<BigInteger> primes = new HashSet<BigInteger>();
    BigInteger n = new BigInteger("2");
    BigInteger maxValue = new BigInteger(String.valueOf(max));

    while (n.compareTo(maxValue) < 0) {
      primes.add(n);
      n = n.nextProbablePrime();
    }

    System.out.println("Primes under " + max + " generated.");
    return primes;
  }
 static void largePrimesProduct(BigInteger a, BigInteger b, String c) {
   BigInteger wp = a.multiply(b);
   assertFalse(
       "isProbablePrime failed for product of two large primes" + a + " * " + b + " = " + c,
       wp.isProbablePrime(80));
   BigInteger wpMinusOne = wp.subtract(BigInteger.ONE);
   BigInteger next = wpMinusOne.nextProbablePrime();
   //        System.out.println(c);
   //        System.out.println(next);
   assertTrue(
       "nextProbablePrime returns wrong number: " + next + "instead of expected: " + c,
       next.toString().equals(c));
 }
Exemple #6
0
  public static void main(String[] args) {
    // Generics, varargs & boxing working together:
    List<Integer> li = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    Integer result = reduce(li, new IntegerAdder());
    print(result);

    result = reduce(li, new IntegerSubtracter());
    print(result);

    print(filter(li, new GreaterThan<Integer>(4)));

    print(forEach(li, new MultiplyingIntegerCollector()).result());

    print(
        forEach(filter(li, new GreaterThan<Integer>(4)), new MultiplyingIntegerCollector())
            .result());

    MathContext mc = new MathContext(7);
    List<BigDecimal> lbd =
        Arrays.asList(
            new BigDecimal(1.1, mc),
            new BigDecimal(2.2, mc),
            new BigDecimal(3.3, mc),
            new BigDecimal(4.4, mc));
    BigDecimal rbd = reduce(lbd, new BigDecimalAdder());
    print(rbd);

    print(filter(lbd, new GreaterThan<BigDecimal>(new BigDecimal(3))));

    // Use the prime-generation facility of BigInteger:
    List<BigInteger> lbi = new ArrayList<BigInteger>();
    BigInteger bi = BigInteger.valueOf(11);
    for (int i = 0; i < 11; i++) {
      lbi.add(bi);
      bi = bi.nextProbablePrime();
    }
    print(lbi);

    BigInteger rbi = reduce(lbi, new BigIntegerAdder());
    print(rbi);
    // The sum of this list of primes is also prime:
    print(rbi.isProbablePrime(5));

    List<AtomicLong> lal =
        Arrays.asList(
            new AtomicLong(11), new AtomicLong(47), new AtomicLong(74), new AtomicLong(133));
    AtomicLong ral = reduce(lal, new AtomicLongAdder());
    print(ral);

    print(transform(lbd, new BigDecimalUlp()));
  }
  /**
   * Get the value of a JE property that is mapped to a configuration attribute.
   *
   * @param cfg The configuration containing the property values.
   * @param attrName The configuration attribute type name.
   * @return The string value of the JE property.
   */
  private static String getPropertyValue(BackendCfg cfg, String attrName, ByteString backendId) {
    try {
      PropertyDefinition<?> propDefn = jebDefnMap.get(attrName);
      Method method = jebMethodMap.get(attrName);

      if (propDefn instanceof DurationPropertyDefinition) {
        Long value = (Long) method.invoke(cfg);

        // JE durations are in microseconds so we must convert.
        DurationPropertyDefinition durationPropDefn = (DurationPropertyDefinition) propDefn;
        value = 1000 * durationPropDefn.getBaseUnit().toMilliSeconds(value);

        return String.valueOf(value);
      } else {
        Object value = method.invoke(cfg);

        if (value != null) {
          return String.valueOf(value);
        }

        if (attrName.equals(ATTR_NUM_CLEANER_THREADS)) {
          // Automatically choose based on the number of processors. We will use
          // similar heuristics to those used to define the default number of
          // worker threads.
          value = Platform.computeNumberOfThreads(8, 1.0f);

          logger.debug(INFO_ERGONOMIC_SIZING_OF_JE_CLEANER_THREADS, backendId, (Number) value);
        } else if (attrName.equals(ATTR_NUM_LOCK_TABLES)) {
          // Automatically choose based on the number of processors. We'll assume that the user has
          // also allowed
          // automatic configuration of cleaners and workers.
          BigInteger tmp = BigInteger.valueOf(Platform.computeNumberOfThreads(1, 2));
          value = tmp.nextProbablePrime();

          logger.debug(INFO_ERGONOMIC_SIZING_OF_JE_LOCK_TABLES, backendId, (Number) value);
        }

        return String.valueOf(value);
      }
    } catch (Exception e) {
      logger.traceException(e);
      return "";
    }
  }
  public static void prime() {
    BigInteger p1, p2, c1;
    int failCount = 0;

    // Test consistency
    for (int i = 0; i < 10; i++) {
      p1 = BigInteger.probablePrime(100, rnd);
      if (!p1.isProbablePrime(100)) {
        System.err.println("Consistency " + p1.toString(16));
        failCount++;
      }
    }

    // Test some known Mersenne primes (2^n)-1
    // The array holds the exponents, not the numbers being tested
    for (int i = 0; i < NUM_MERSENNES_TO_TEST; i++) {
      p1 = new BigInteger("2");
      p1 = p1.pow(mersenne_powers[i]);
      p1 = p1.subtract(BigInteger.ONE);
      if (!p1.isProbablePrime(100)) {
        System.err.println("Mersenne prime " + i + " failed.");
        failCount++;
      }
    }

    // Test some primes reported by customers as failing in the past
    for (int i = 0; i < customer_primes.length; i++) {
      p1 = new BigInteger(customer_primes[i]);
      if (!p1.isProbablePrime(100)) {
        System.err.println("Customer prime " + i + " failed.");
        failCount++;
      }
    }

    // Test some known Carmichael numbers.
    for (int i = 0; i < carmichaels.length; i++) {
      c1 = BigInteger.valueOf(carmichaels[i]);
      if (c1.isProbablePrime(100)) {
        System.err.println("Carmichael " + i + " reported as prime.");
        failCount++;
      }
    }

    // Test some computed Carmichael numbers.
    // Numbers of the form (6k+1)(12k+1)(18k+1) are Carmichael numbers if
    // each of the factors is prime
    int found = 0;
    BigInteger f1 = new BigInteger(40, 100, rnd);
    while (found < NUM_CARMICHAELS_TO_TEST) {
      BigInteger k = null;
      BigInteger f2, f3;
      f1 = f1.nextProbablePrime();
      BigInteger[] result = f1.subtract(ONE).divideAndRemainder(SIX);
      if (result[1].equals(ZERO)) {
        k = result[0];
        f2 = k.multiply(TWELVE).add(ONE);
        if (f2.isProbablePrime(100)) {
          f3 = k.multiply(EIGHTEEN).add(ONE);
          if (f3.isProbablePrime(100)) {
            c1 = f1.multiply(f2).multiply(f3);
            if (c1.isProbablePrime(100)) {
              System.err.println("Computed Carmichael " + c1.toString(16));
              failCount++;
            }
            found++;
          }
        }
      }
      f1 = f1.add(TWO);
    }

    // Test some composites that are products of 2 primes
    for (int i = 0; i < 50; i++) {
      p1 = BigInteger.probablePrime(100, rnd);
      p2 = BigInteger.probablePrime(100, rnd);
      c1 = p1.multiply(p2);
      if (c1.isProbablePrime(100)) {
        System.err.println("Composite failed " + c1.toString(16));
        failCount++;
      }
    }

    for (int i = 0; i < 4; i++) {
      p1 = BigInteger.probablePrime(600, rnd);
      p2 = BigInteger.probablePrime(600, rnd);
      c1 = p1.multiply(p2);
      if (c1.isProbablePrime(100)) {
        System.err.println("Composite failed " + c1.toString(16));
        failCount++;
      }
    }

    report("Prime", failCount);
  }