1

I am working on a project that take in a starting number and a number of primes then outputs a table with each prime number after your starting number until it reaches your number of primes. I wrote a program to find the prime numbers but im having trouble getting the output to format in a table. I know i need to use nested for loops to build the table but im not sure how to go about starting that. Here is my code so far:

class Tester2{

   public static void main(String[] args){
      int number = 750000; //starting number
      int nofn = 12;       //number of primes to find 
      int primecount = 0;

      while(primecount < nofn){  
         if(isPrime(number)){
            primecount ++;
            System.out.println(primecount + " " +number);
         }   
         number++;
      }
   }


   //determins if num is prime
   private static boolean isPrime(int num){
      int divisor = 2;
      boolean itIsPrime;   
      while(num % divisor != 0){
         divisor++;
         }        
      if(divisor == num)
         itIsPrime = true;
      else
         itIsPrime = false;
      return itIsPrime;        
   }
}

Currently my code returns

1 750019
2 750037
3 750059
4 750077
5 750083
6 750097
7 750119
8 750121
9 750131
10 750133
11 750137
12 750151

and I am looking for it to return

Row #: 1--> 750019 750037 750059 750077 750083 750097 750119 750121 750131 750133
Row #: 2--> 750137 750151
Spartan172
  • 13
  • 4
  • 2
    I don't see any table here. Do you just want to have a new line after every 10th result? – NeplatnyUdaj Mar 22 '19 at 14:39
  • Hello @Spartan172 and welcome to SO. First I would recommend few naming strategies for you to use -> for variables name simply use mixedCase with leading lower case letter. So that `primecount` should become `primeCount`. Then for the boolean variable name it's more appropriate to construct the name in the manner of a question so that `itIsPrime` should become `isItPrime`. `nofn` doesn't bring any information about the value contained and should be renamed. Now, for the grid itself -> who defined the grid row length? I can see from the expected output that it should be like 10 or so... – dbl Mar 22 '19 at 14:45
  • @NeplatnyUdaj exactly, I need it to print 10 results per row then end when it gets to the last prime ( the 12th in this case). Im just not sure how to build out the table – Spartan172 Mar 22 '19 at 14:47

5 Answers5

1

Here you go. I also improved your isPrime method to loop until sqrt(num) instead of num, so it should be much faster now:

public class Test {
    public static void main(String[] args) {
        int number = 750000; // starting number
        int nofn = 100; // number of primes to find
        int primecount = 0;
        int row = 0, col = 0;

        while (primecount < nofn) {
            if (isPrime(number)) {
                primecount++;
                if (col++ == 0)
                    System.out.print(String.format("%3d-->", ++row));
                System.out.print(String.format(" %6d", number));
                if (col == 10) {
                    System.out.println();
                    col = 0;
                }
            }
            number++;
        }
    }

    private static boolean isPrime(int num) {
        final int last = (int) Math.sqrt(num);
        for (int divisor = 2; divisor <= last; ++divisor) {
            if (num % divisor == 0)
                return false;
        }
        return true;
    }

}

Output:

  1--> 750019 750037 750059 750077 750083 750097 750119 750121 750131 750133
  2--> 750137 750151 750157 750161 750163 750173 750179 750203 750209 750223
  3--> 750229 750287 750311 750313 750353 750383 750401 750413 750419 750437
  4--> 750457 750473 750487 750509 750517 750521 750553 750571 750599 750613
  5--> 750641 750653 750661 750667 750679 750691 750707 750713 750719 750721
  6--> 750749 750769 750787 750791 750797 750803 750809 750817 750829 750853
  7--> 750857 750863 750917 750929 750943 750961 750977 750983 751001 751007
  8--> 751021 751027 751057 751061 751087 751103 751123 751133 751139 751141
  9--> 751147 751151 751181 751183 751189 751193 751199 751207 751217 751237
 10--> 751259 751273 751277 751291 751297 751301 751307 751319 751321 751327
rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Wow than you so much, this is exactly what i needed to see. – Spartan172 Mar 22 '19 at 14:56
  • Since there is already a primecount, wouldnt it be better practice to use modulo instead of int i? – Benyom Mar 22 '19 at 15:01
  • @Zeta no, division can be a very slow operation. – rustyx Mar 22 '19 at 15:04
  • @rustyx well if you really want performance that badly, you should consider using power of 2. reference: http://www.gregbugaj.com/?p=204 – Benyom Mar 22 '19 at 15:09
  • how could i format the row header to count up by one instead of showing the prime count? – Spartan172 Mar 22 '19 at 15:12
  • Make a rowcounter and everytime a new line is added, add 1 to it. Then change primecount in this row to your new counter: System.out.print(String.format("%5d:", primecount)); – Benyom Mar 22 '19 at 15:14
  • After reviewing this answer I've remembered that back in the days we used to put `divisor * divisor <= number` instead of `divisor <= sqrtNumber` and was wondering which is better in the end... After a quick check from [another stackoverflow thread](https://stackoverflow.com/questions/825221/where-can-i-find-the-source-code-for-javas-square-root-function) I think that sqrt should be significantly efficient. – dbl Mar 22 '19 at 15:38
  • @dbl well no doubt its the best performance using that, but also the worst readability in my opinion^^ – Benyom Mar 22 '19 at 16:00
  • @Zeta what are you talking about? What exactly is performing better than what? What you find as not readable I don't get it :/ – dbl Mar 22 '19 at 16:06
0

You are using System.out.println every time you need to print. This prints a new line every time a prime is found. What you need to do is use the function System.out.print to print your information. Then, after a 10 primes have been found, implement the function System.out.println.

if(isPrime(number)){
        primecount ++;
        if(primecount%10==0)
            System.out.println(number+" ");
        else
            System.out.print(number+" ");
     }
alt440
  • 51
  • 1
  • 6
  • Usually this one will do the work, just have in mind that the white space inside the `println` statement is obsolete. – dbl Mar 22 '19 at 14:47
  • In this scenario, it is not: when you move on to the next line, you still need a space after your first number. Otherwise, there would be no space after the first prime of the second row. The space that is obsolete is the one that is after the last prime of the row. – alt440 Mar 22 '19 at 14:55
  • I think you should start your IDE and run the example so you can see what I mean. I will not explain how your logic fails as it will be much easier for you to *try and see* :) – dbl Mar 22 '19 at 15:05
0

Not clear what you mean by table. If you just want to print 10 prime numbers on 1 row you can do:

  while(primecount < nofn){  
     if(isPrime(number)){
        primecount ++;
        System.out.print(primecount + " " +number);
        if (primecount%10==0)
           System.out.println();
     }   
     number++;
  }
Conffusion
  • 4,335
  • 2
  • 16
  • 28
0

To print the row number this should be the code.

public static void main(String[] args) {
    int number = 750000; //starting number
    int nofn = 12;       //number of primes to find
    int primecount = 0;
    int rowCount = 1;

    System.out.print(rowCount + "--> ");
    while (primecount < nofn) {
        if (isPrime(number)) {
            primecount++;

            System.out.print(number + " ");

            if (primecount % 10 == 0) {
                System.out.println();
                rowCount++;
                System.out.print(rowCount + "--> ");
            }


        }
        number++;
    }
}
LeoN
  • 1,590
  • 1
  • 11
  • 19
0

I mean if you just want to skip it after like 8 entries, you have to do a nested loop how you said.
Not a very good solution, since it could get stuck in the while loop and its not beautiful code at all, but I guess it could work somehow like this:

Edited

int rowCount = 0;
while(primecount < nofn){
  if(isPrime(number)){
    if(primeCount% 10 == 0) {
      rowCount++;
      System.out.println("Row #: " + rowCount + "-->");
    }
    primecount ++;
    System.out.print(" " +number); // print not printl
  }
  number++;
}
Benyom
  • 75
  • 7