Ejemplo n.º 1
0
  /**
   * Main method.
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    SimpleReader in = new SimpleReader1L();
    SimpleWriter out = new SimpleWriter1L();

    // Sanity check of randomNumber method -- just so everyone can see how
    // it might be "tested"

    final int testValue = 17;
    final int testSamples = 100000;
    NaturalNumber test = new NaturalNumber2(testValue);
    int[] count = new int[testValue + 1];
    for (int i = 0; i < count.length; i++) {
      count[i] = 0;
    }
    for (int i = 0; i < testSamples; i++) {
      NaturalNumber rn = randomNumber(test);
      assert rn.compareTo(test) <= 0 : "Help!";
      count[rn.toInt()]++;
    }
    for (int i = 0; i < count.length; i++) {
      out.println("count[" + i + "] = " + count[i]);
    }
    out.println("  expected value = " + (double) testSamples / (double) (testValue + 1));

    // Check user-supplied numbers for primality, and if a number is not
    // prime, find the next likely prime after it

    while (true) {
      out.print("n = ");
      NaturalNumber n = new NaturalNumber2(in.nextLine());
      if (n.compareTo(new NaturalNumber2(2)) < 0) {
        out.println("Bye!");
        break;
      } else {
        if (isPrime1(n)) {
          out.println(n + " is probably a prime number" + " according to isPrime1.");
        } else {
          out.println(n + " is a composite number" + " according to isPrime1.");
        }
        if (isPrime2(n)) {
          out.println(n + " is probably a prime number" + " according to isPrime2.");
        } else {
          out.println(n + " is a composite number" + " according to isPrime2.");
          generateNextLikelyPrime(n);
          out.println("  next likely prime is " + n);
        }
      }
    }

    // Close input and output streams

    in.close();
    out.close();
  }
Ejemplo n.º 2
0
  /**
   * Main method.
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    SimpleReader in = new SimpleReader1L();
    SimpleWriter out = new SimpleWriter1L();

    // Asks the user if they would like to find the square root of a number
    out.println(
        "Would you like to find the square root of a number? "
            + "(Enter 'y' for yes and anything else to terminate)");
    String response = in.nextLine();

    // Asks for number to find square root of. Will continue to ask user if
    // they would like to find the square root after computation.
    while (response.equals("y")) {
      out.println("Enter number: ");
      double x = in.nextDouble();
      out.println("Enter error percentage margin: ");
      double e = in.nextDouble();
      double sqroot = sqrt(x, e);
      out.println("Square Root estimate: " + sqroot);

      // Asks the user if they would like to find the square root
      out.println(
          "Would you like to find the square root of a number? "
              + "(Enter 'y' for yes and anything else to terminate)");
      response = in.nextLine();
    }
    if (response != "y") {
      out.println("Terminating Program");
    }

    /*
     * Close input and output streams
     */
    in.close();
    out.close();
  }