Exemple #1
0
  /**
   * Runs the algorithm of the "Sieve of Eratosthenes" and count every found prime factor. If the
   * 10001. is found, it is returned.
   *
   * @return the 100001. prime factor
   * @see Sieve#next()
   * @see <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes @
   *     Wikipedia</a>
   */
  @Override
  public String solve() {

    while (sieve.hasNext() && cnt < 9999) {
      cnt++;
      IO.infoln(cnt + ". prime = " + sieve.next());
    }

    return IO.i2s(sieve.next());
  }
 public static void main(String[] args) {
   System.out.println("This program computes all prime numbers up to a");
   System.out.println("maximum using the Sieve of Eratosthenes.");
   System.out.println();
   Scanner console = new Scanner(System.in);
   Sieve s = new Sieve();
   for (; ; ) {
     System.out.print("Maximum n to compute (0 to quit)? ");
     int max = console.nextInt();
     if (max == 0) break;
     System.out.println();
     s.computeTo(max);
     s.reportResults();
     int percent = s.getCount() * 100 / s.getMax();
     System.out.println("% of primes = " + percent);
     System.out.println();
   }
 }