aspose file tools*
The moose likes Beginning Java and the fly likes Prime numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prime numbers" Watch "Prime numbers" New topic
Author

Prime numbers

Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
Hello, I am trying to code this program that finds and prints out all the prime numbers between two numbers , it compiles fine but in the execution nothing happens, I am suspecting an infinite loop but cannot find the problem. Thank you for your time.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I wouldn't expect that program to produce any output. Perhaps that's what is confusing you, the fact that you were expecting some output. I suggest you put in some debugging code to show you which array entries are being set to false.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
I did it found the error for the infinite loop thanks to you, : ) the code brings these number out for an input of 50: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 4 8 12 16 20 24 28 32 36 40 44 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 7 14 21 28 35 42 8 16 24 32 40 9 18 27 36 45 10 20 30 40 11 22 33 44 12 24 36 13 26 39 14 28 42 15 30 45 16 32 17 34 18 36 19 38 20 40 21 42 22 44 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 so it appears to work, but I don't want it to take the first numbers for example 2,3,7. Do you have any idea of how to do that ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So you are marking all of these numbers as non-primes:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44

And also these:

3 6 9 12 15 18 21 24 27 30 33 36 39 42 45

But you already noticed that flagging the first one of each list as non-prime was incorrect, didn't you? Now look at the loop which does that. Notice where it starts. It shouldn't start there, should it?


Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
Ok, I get what you mean, Should it be ?
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
ok not thinking right should it be more like this: ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No, that isn't quite right.

You didn't get it right because your variable names "i" and "j" don't mean anything so it's easy to get them confused. Which you did.

I always find it easier to think of an example while I am working on code. In your case:

2 4 6 8 10 ...

you don't want to start at 2, you want to start at 4. Likewise with

3 6 9 12 15 ...

you don't want to start at 3, you want to start at 6. Does a pattern start to appear?
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
Point taken, but then what would you call them ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you had used the name "nonPrime" instead of "j" I think you would have been less likely to start by assigning "i" to it.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
I now am officially brain-dead. I get what you mean by naming it nonPrime though but I don't see how that troubles my logic.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
Paul Clapham wrote:No, that isn't quite right.

You didn't get it right because your variable names "i" and "j" don't mean anything so it's easy to get them confused. Which you did.

I always find it easier to think of an example while I am working on code. In your case:

2 4 6 8 10 ...

you don't want to start at 2, you want to start at 4. Likewise with

3 6 9 12 15 ...

you don't want to start at 3, you want to start at 6. Does a pattern start to appear?

Ok yeah I didn't see that part of the comment before weird.
And yeah I agree and that was what I tried to do by saying "2*i".
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
The thing is, I know it is wrong the right thing to do would be to directly start at the time you go into the loop. so I tried :

but obviously the error "variable may not have been initialized appeared. Aaand I'm stuck.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Sebastien Zerbato wrote:Ok yeah I didn't see that part of the comment before weird.

That's because I edited the post after you had looked at it.
And yeah I agree and that was what I tried to do by saying "2*i".

Except that when I looked at your post it said "i + j" and not "2 * i". That's because you edited the post after I had looked at it.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
Oh ok,
so... Am I right ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Sebastien Zerbato wrote:so... Am I right ?


Try it and see.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
it prints this out "2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 11 11 11 12 12 13 13 14 14 15 15 16 17 18 19 20 21 22 23 " so it doesn't work..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Then fix it.

Have a look at this FAQ article: ItDoesntWorkIsUseless -- and remember you are talking about code which nobody but you can see.
Sebastien Zerbato
Greenhorn

Joined: Nov 14, 2011
Posts: 24
hahaha. Point taken. I'll try and find out what the bug is..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prime numbers