Displaying 1000 Prime Numbers

How to display 1000 Prime numbers in one window

Page 1 of 1

7 Replies - 4116 Views - Last Post: 15 May 2009 - 01:42 PM Rate Topic: -----

#1 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Displaying 1000 Prime Numbers

Posted 14 May 2009 - 10:26 AM

I am supposed to display the first 1000 Prime Numbers in a JOptionPane window in groups of 10.

I have everything working, but it only displays a single number at a time, so I would have to click ok 1,000. Someone please help me display all numbers at once in a group of 10:


X=prime number
x x x x x x x x x x
x x x x x x x x x x

This is what I have:

  /*Jeremy D Duenas
	Student#074193
	Assignment5.1 
	CS212
  */
  
  
  
   import javax.swing.JOptionPane;
	public class FindPrime
   {
	   public static void main (String[] args)
	  {
	  
		 final int PRIMES = 1000;//Number of primes to display
		 final int NUMBER_OF_PRIMES= 10; //Display 10 per line
		 int count = 0;//Count number of prime numbers
		 int number = 2;
		 JOptionPane.showMessageDialog(null,"The first 1000 prime numbers are \n");
		 while (count < PRIMES)
		 {
			boolean isPrime = true;
			int temp = number/2; //declare new variable
			for (int divisor = 2;divisor<=temp;divisor++) 
			
			{
			   if (number % divisor == 0)
			   {
				  isPrime = false;
				  break;
			   }
			}
			if (isPrime)
			{
			   count++;
			   if (count % PRIMES != 0) 
																							 
			   {
				  JOptionPane.showMessageDialog(null,number + " ");
			   }
			   else
				  JOptionPane.showMessageDialog(null, number + ".");
			}
			number++;
		 }
	  }
   }





Our hour is marked, and no one can claim a moment of life beyond what fate has predestined."
----Napoleon Bonaparte

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying 1000 Prime Numbers

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 11:23 AM

Do something like:

String batch = ""; // (before loop)
StringBuilder builder = new StringBuilder(); // (before loop)

// (in loop)
if(count > 0 && count % 10 == 0) {
	batch = batch.substring(0, batch.length() - 1); // remove delimiter
	builder.append(batch).append("\n");
	batch = "";
}
else {
	batch += "" + number + delimiter;
}


Was This Post Helpful? 0
  • +
  • -

#3 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 11:23 AM

Are you sure this works? It doesn't look like your For loop is ever entered.

As for displaying them all in a JOptionPane, I would create a list of all the primes under 1000 and then when that is filled up print out the list instead of each individual one. (you can format it in rows of 10 afterwards)

This post has been edited by Jubb: 14 May 2009 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 12:11 PM

g00se:
I tried what you said and still doesn't work maybe I am putting it in the wrong place?!?!?!
This is what it looks like now:


  /*Jeremy D Duenas
	Student#074193
	Assignment5.1 
	CS212
  */
   
   import javax.swing.JOptionPane;
	public class FindPrime
   {
	   public static void main (String[] args)
	  {
	  
		 final int PRIMES = 1000;//Number of primes to display
		 final int NUMBER_OF_PRIMES= 10; //Display 10 per line
		 int count = 0;//Count number of prime numbers
		 int number = 2;
		 JOptionPane.showMessageDialog(null,"The first 1000 prime numbers are \n");
		 
		  
		 String batch = ""; 
		 StringBuilder builder = new StringBuilder(); 
				
		  
		 while (count < PRIMES)
		 {
			boolean isPrime = true;
			int temp = number/2; //declare new variable
			for (int divisor = 2;divisor<=temp;divisor++) 
			{
			   if(count > 0 && count % 10 == 0)
			   {		
				  batch = batch.substring(0, batch.length() - 1);	   
				  builder.append(batch).append("\n");		
				  batch = "";}
			   else 
			   {		
				  batch += "" + number;
			   }
				
			   if (number % divisor == 0)
			   {
				  isPrime = false;
				  
				 
			   }
			}
			if (isPrime)
			{
			   count++;
			   if (count % PRIMES != 0) 
																							 
			   {
				  JOptionPane.showMessageDialog(null, number + " ");
			   }
			   else
				  JOptionPane.showMessageDialog(null, number + ".");
			}
			number++;
		 }
	  }
   }













[/quote]
Was This Post Helpful? 0
  • +
  • -

#5 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 12:22 PM

Your right I just double checked it and it goes over 1000.....I cant figure out how to make it stop.




View PostJubb, on 14 May, 2009 - 10:23 AM, said:

Are you sure this works? It doesn't look like your For loop is ever entered.

As for displaying them all in a JOptionPane, I would create a list of all the primes under 1000 and then when that is filled up print out the list instead of each individual one. (you can format it in rows of 10 afterwards)

Was This Post Helpful? 0
  • +
  • -

#6 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 01:13 PM

Sorry to say but if your assignment is just to print out all the primes under 1000 I would go about it a different way.

Look into the Sieve of Eratosthenes on wikipedia, very useful for finding all the primes up to a certain number. And pretty easy to implement. Your code looks kinda buggy and will probably take a very long time to run.
Was This Post Helpful? 1
  • +
  • -

#7 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Re: Displaying 1000 Prime Numbers

Posted 14 May 2009 - 02:57 PM

Thanks Jubb, I took your advice and was able to get a simple code written, that finds the first prime numbers, now all I need to do is get them into a what I believe is a 7row x 10column box. There are 69 total prime numbers to display the code below is what I have so far, I just cant seem to get it to display.....maybe I am completly wrong as I am new to this. Can anyone help?!?!?!?


 import java.util.Scanner;
	
	public class Prime
   {
	   static boolean isPrime(int n)
	  {
		 if (n%2==0) 
			return n==2;
		 int lim=(int)Math.sqrt(n);
		 for (int i=3; i<=lim; i+=2)
			if (n%i==0) 
			   return false;
		 return true;
	  }
	
	   public static void main(String args[])
	  {
		 System.out.println("The First 1,000 Prime Numbers are:\n"); 
		  
		 for (int n=2; n<=1000; ++n)
//this is where I am lost I think this is what I need to do start the rows and columns but I am not sure. I know if you take this code out and run it it will display the first 1000. I need to get it into a 10 column box. 
		 {
			int r[][]=new int[8][];
			r[0]=new int[10];
			r[1]=new int[10];
			r[2]=new int[10];
			r[3]=new int[10];
			r[4]=new int[10];
			r[5]=new int[10];
			r[6]=new int[10];
			r[7]=new int[10];
		 	
		 
			if (isPrime(n)) 
			   System.out.println(n);
			
			
		 }
	  }
   }






View PostJubb, on 14 May, 2009 - 12:13 PM, said:

Sorry to say but if your assignment is just to print out all the primes under 1000 I would go about it a different way.

Look into the Sieve of Eratosthenes on wikipedia, very useful for finding all the primes up to a certain number. And pretty easy to implement. Your code looks kinda buggy and will probably take a very long time to run.

Was This Post Helpful? 0
  • +
  • -

#8 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Re: Displaying 1000 Prime Numbers

Posted 15 May 2009 - 01:42 PM

any help out there for this?!?!?!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1