Creating Prime List, need Progress Report

GUI window displaying what number Java is currently checking for prime

Page 1 of 1

10 Replies - 791 Views - Last Post: 08 October 2009 - 09:40 PM Rate Topic: -----

#1 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Creating Prime List, need Progress Report

Posted 07 October 2009 - 08:44 PM

So, because I have no life, I've built a program that checks numbers for primeness and adds them to an arrayList of all primes found up to that point. I still have much to do to make it more efficient, but I was wondering how to display what number that Java is currently checking. I am not even sure exactly what I am asking for, other than a GUI window that continuously updates with whatever number the computer is checking for primeness currently. If someone could point me in the right direction, or show me a Java tutorial that explains something like this, I would be forever grateful!

Here's the basics of my code. The number I want to display would be i in the first for loop.


	public static void main (String args[])
	{
		String answer = "";
		List<Integer> primeList = new ArrayList<Integer>();
		primeList.add(2);
		primeList.add(3);
		primeList.add(5);
		primeList.add(7);
		
		for (int i = 8; i <= 1000000; i++)
		{
			if ((Primes.multipleOfThreeCheck(Primes.multipleOfThree(i)) == false) && 
				(Primes.multipleOfFive(i) == false))
			{
				if (Primes.primeFinder (i, (ArrayList)primeList) == true)
				{
					primeList.add(i);
				}
			}
		}
		
		for (int j = 0; j < primeList.size(); j++)
		{
			answer += primeList.get(j) + "\n";
		}
		System.out.println(answer);
	}




Is This A Good Question/Topic? 0
  • +

Replies To: Creating Prime List, need Progress Report

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating Prime List, need Progress Report

Posted 07 October 2009 - 09:09 PM

Start by making that loop

for (int i = 8; i <= 1000000; i++)

for(int i = 9; i <= 100000; i += 2)

no need to check the even numbers :)
Was This Post Helpful? 1
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: Creating Prime List, need Progress Report

Posted 07 October 2009 - 09:13 PM

Do you have any previous GUI experience? Like Swing?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Creating Prime List, need Progress Report

Posted 07 October 2009 - 09:14 PM

Well it depends on the control you want to add the number to which will determine how you add it. For instance if you have a JList setup with a DefaultListModel powering it called "model" then it is just a matter of putting in model.add(position, i); where position would be the position in the list and "i" would be the number you are testing. If it is a textfield, it will just be a matter of appending on the value to the textfield value.

But the point is that this adding will simply put right before the if statement where you check if it is a multipleOfThree etc. That is where "i" is being tested and before you check it for primeness and add it to your arraylist.

Hopefully that answers your question. :)
Was This Post Helpful? 1
  • +
  • -

#5 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: Creating Prime List, need Progress Report

Posted 07 October 2009 - 09:36 PM

I guess my concern was I have only used JOptionPane so far, and I was worried I would have to have some sort of user interaction to have what number I am checking to change. I was hoping for a way where the window changes on its own. At first, it would be ridiculously quickly as it moved through the lower numbers fast, but it would slow down as it got higher. Thanks for the help, and I will look into what I have heard so far!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 10:19 AM

At the rate that your computer runs calculations, it may be unwise to have it say "currently checking " + number. Simply, the number will change so rapidly making it very blury and a headache for the user (I'm getting one just thinking about it).
Was This Post Helpful? 0
  • +
  • -

#7 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 12:15 PM

I understand what you are saying, but eventually I want this code to work for BIG_INT as well, and be able to go upwards towards infinity (as long as my computer has memory :P). By the time it gets that high, it will be going much much slower. I also want to eventually put in a quit button that basically saves the current state of the program so that it can resume it's search later on. These are all much further down the line for me, since I have so little experience, but it's a fun project for me to work on :P
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 06:49 PM

Well if that is your plan, then I would definitely make sure you take pbl's advice and check only the odd numbers to cut your work load in half. That right there will cut out billions of numbers you never even had to check.

;)
Was This Post Helpful? 0
  • +
  • -

#9 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 08:15 PM

Even better skip all the arbitrary checks for 2 3 and 5. Store all your primes in an array and only check against other primes. 2, 3, and 5 will be checked nearly first and break. Gaining the same (or more) efficiency but simpler.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 08:30 PM

Have you heard of the Sieve of Eratosthenes? Basically, it populates a list with numbers 2 to x. It checks for primes by getting the first number in the list (2), and removing all the numbers that are divisible by 2. It then goes to the next number in the list (3), and repeats this process. It continues along until the first loop reaches the end of the list. I have written a code snippet on it if you want to check it out.

Just a note, it is almost always better to use automation to do something when possible rather than hardcoding in initial values. Good luck!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating Prime List, need Progress Report

Posted 08 October 2009 - 09:40 PM

Both Syfran and Macosxnerd have good ideas

You can check for multiples of 3 by adding all numbers (each digits) and see if it os a multiple of 3

the Sieve of Eratosthenes use a BitSet and is used by all CPU manufacturers to perform benchmarks it is a de facto criteria for CPU performance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1