# Creating Prime List, need Progress Report

Page 1 of 1

## 10 Replies - 1244 Views - Last Post: 08 October 2009 - 09:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130615&amp;s=0e0d7f367a8cfaf22ca2e51a53e925b9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 noctolater

Reputation: 3
• 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>();

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)
{
}
}
}

for (int j = 0; j < primeList.size(); j++)
{
answer += primeList.get(j) + "\n";
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Creating Prime List, need Progress Report

### #2 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #3 xclite

• I wrote you an code

Reputation: 1281
• Posts: 4,089
• 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?

### #4 Martyr2

• Programming Theoretician

Reputation: 5226
• Posts: 14,006
• 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.

### #5 noctolater

Reputation: 3
• 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!

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12316
• Posts: 45,416
• 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).

### #7 noctolater

Reputation: 3
• 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 ). 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

### #8 Martyr2

• Programming Theoretician

Reputation: 5226
• Posts: 14,006
• 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.

### #9 syfran

• D.I.C Lover

Reputation: 83
• 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.

### #10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12316
• Posts: 45,416
• 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!

### #11 pbl

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

Reputation: 8378
• Posts: 31,956
• 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