Array or List index starts from 0. Hence the last object is always at the index size-1.
When you try primes.get(primes.size()) - index primes.size() is out of reach in other words out of bounds for primes arraylist.
Hope this helps.

Mike Simmons wrote:What does your error message say?

ArrayList Index out of bounds.... here's the cut and paste:

Also, what is "indexMark ^ 2" intended to do?

I need to square the indexMark..... or indexMark to the second power. The loop needs to end when (the value in the indexMark) squared is greater than the value in the last index of the ArrayList.

Change this line while ((primes.get(indexMark) ^ 2) <= primes.get(primes.size())) to while ((primes.get(indexMark) ^ 2) <= primes.get(primes.size()-1))

I did try that, then I realized that I increment the indexMark before the loop ends. I changed it to:

But now it just appears to freeze instead of continuing. Not sure why.

Here's the whole method as it stands now:

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018

10

posted

0

Janeice DelVecchio wrote:

Mike Simmons wrote:Also, what is "indexMark ^ 2" intended to do?

I need to square the indexMark..... or indexMark to the second power. The loop needs to end when (the value in the indexMark) squared is greater than the value in the last index of the ArrayList.

I don't think that symbol means what you think it does.

Suchitra K Bhat
Greenhorn

Joined: Nov 04, 2009
Posts: 8

posted

0

Is this part correct?

for (int x = indexMark; x < primes.size(); x++) {
if (primes.get(x) % primes.get(indexMark) == 0) {
primes.remove(x);
}
indexMark++;

}

If I trace this, first iteration X is Zero and indexMark is Zero
primes.get(0) % primes.get(0) == 0 -- this is true and removes the object at index Zero

Next iteration that when x=1, at this point indexMark is also 1 , this way all objects inside primes arrayList gets removed by the time "(primes.get(indexMark - 2) ^ 2) <= primes.get(primes.size() -1)" is reached.

public static void main(String args[]) {
Test testObj = new Test();
System.out.print("countPrimes returned == "+testObj.countPrimes());
}

public int countPrimes() {
int choice = 12; //You can remove this hard coding
List<Integer> primes = new ArrayList<Integer>(choice);

for (int x = 2; x <= choice; x++) {
primes.add(x);
}
int indexMark = 2;
do {
for (int x = 0; x < primes.size(); x++) {
if ( (primes.get(x)>indexMark) && (primes.get(x) % indexMark == 0)) {
primes.remove(x);
}
}
indexMark++;
} while ((indexMark ^ 2) <= primes.get(primes.size() -1));

for (int x = 0; x < primes.size(); x++) {
System.out.print(primes.get(x) + " ");
}
return primes.size();
}
}

It's still not getting rid of the multiples of 3 or 5 or 7 or higher.

Something is wrong with my algorithm I think... and I'm not seeing it.

For example: Input of 30
It's supposed to go through the first time and take out the multiples of 2.
That leaves: 2, 3, 5, 7, 9, 11, 13, 15, 19, 21, 23, 25, 27, 29.
Index^2 = 4 which is less than 30 so keep going.

Then it goes to the next index, 3, and takes out all those multiples.
That leaves: 2, 3, 5, 7, 11, 13, 19, 23, 25, 29
Index^2 = 9.... again less than 30 so keep going.

Then it goes to the next index, 5, and takes out all those multiples.
That leaves: 2, 3, 5, 7, 11, 13, 19, 23, 29
Index^2 = 25... keep going.

Then it goes to the next index, 7, and should remove nothing.
Index^2 = 49 which is greater than 30 so STOP

Here's what I have now:

Suchitra K Bhat
Greenhorn

Joined: Nov 04, 2009
Posts: 8

posted

0

your countPrimes() is not correct.
Changes you need are

1) indexMark should be incremented out side for loop but inside the do-while loop
2) first for loop should start with 0 not with indexMark

countPrimes() given below works fine. I have tried the below countPrimes() with 30 which gives the out put of 2 3 5 7 11 13 17 19 23 29

public int countPrimes() {
List<Integer> primes = new ArrayList<Integer>(choice);

for (int x = 2; x <= choice; x++) {
primes.add(x);
}
int indexMark = 2;
do {
for (int x = 0; x < primes.size(); x++) { // --> Here x is Zero not indexMark
if ( (primes.get(x)>indexMark) && (primes.get(x) % indexMark == 0)) {
primes.remove(x);
}
} // --> for loop is closed here note that indexMark is incremented after closing the for loop
indexMark++;
} while ((indexMark ^ 2) <= primes.get(primes.size() -1));

for (int x = 0; x < primes.size(); x++) {
System.out.print(primes.get(x) + " ");
}
return primes.size();
}

Suchitra K Bhat
Greenhorn

Joined: Nov 04, 2009
Posts: 8

posted

0

and "^" this symbol in java is to indicate power of

For eg
2 ^ 2 will be 2 power 2 i.e 2 *2
2 ^ 3 is 2 power 3 i.e 2 * 2 * 2

Similarly

indexMark ^ 2 == indexMark * indexMark . You can use ^ and do not need to use indexMarkSqd.

Apparently I was too indirect in my previous posts. Let me fix that:

In Java, the symbol does NOT indicate "power" or "to the power of". EVER. You guys are thinking of other languages.

It's easy to test this:

So, what does the ^ really do in Java? Well, it's not really relevant here, but if you're interested, I'd start with the Java Tutorial's section on Operators.

If you want to square a number in Java, just multiply it by itself. So for example

could be replaced with

I make no comment on whether this algorithm looks correct or not; I haven't analyzed it at all. I'm just commenting on the basics of what ^ means in Java.