Home > Mobile >  Unable to print Large # of Primes to Console
Unable to print Large # of Primes to Console

Time:10-24

I've been having an issue with the following program:

public class PrimeFinder implements Runnable {
    Thread go;
    StringBuffer primes = new StringBuffer();
    int time = 0;
    
    public PrimeFinder() {
        start();
        while (primes != null) {
            System.out.println(time);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException exc) {
                // do nothing
            }
            time  ;
        }
    }
    
    public void start() {
        if (go == null) {
            go = new Thread(this);
            go.start();
        }
    }
    
    public void run() {
        int quantity = 1_000_000;
        int numPrimes = 0;
        // candidate: the number than might be prime
        int candidate = 2;
        primes.append("\nFirst ").append(quantity).append(" primes:\n\n");
        while (numPrimes < quantity) {
            if (isPrime(candidate)) {
                primes.append(candidate).append(" ");
                numPrimes  ;
            }
            candidate  ;
        }
        System.out.println(primes);
        primes = null;
        System.out.println("\nTime elapsed: "   time   " seconds");
    }
    
    public static boolean isPrime(int checkNumber) {
        double root = Math.sqrt(checkNumber);
        for (int i = 2; i <= root; i  ) {
            if (checkNumber % i == 0) {
                return false;
            }
        }
        return true;
    }
    
    public static void main (String[] arguments) {
        new PrimeFinder();
    }
}

The program will count the time it takes to Print all Primes to the console. beginning at 0 seconds to x (when the program completes)

Then will print x number of prime numbers (line 29: quantity = 1_000_000).

Then will print "Time Elapsed: x seconds

when I run the program with a smaller quantity (ex:10) it will print up to '29' (the 10th prime).

I'm assuming there is some limitation in eclipse that is preventing a large quantity of numbers from being printed to the console.

Edit: at exactly 5572 the output to the console will be cleared this is the output:

how many primes would you like to see? 5572

0 //this is time the program has ran

First 5572 primes:

Time elapsed: 0 seconds.

when copy and pasting here the numbers carried over, so its just disappeared from the console.

CodePudding user response:

Your console output is probably limited. In Eclipse, got to...

Window > Preferences > Run/Debug > Console

Uncheck the Limit console output check box.

eclipse preferences

CodePudding user response:

console output was being printed on one line, added "\n" for the if statement on lines: (41-46)

if (isPrime(candidate)) {
                primes.append(candidate  "\n").append(" "); //if candidate is prime, print # then space " "
                numPrimes  ;
            }
            candidate  ;
        }

Now prints vertically.

  • Related