• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prime number Program  RSS feed

 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to find prime number from an given array. I could see 3 is a prime number in my array but it is printing as NOT prime number. But if I remove other numbers and keep only 3 in this array, 3 prints as prime number only. Could anyone suggest what is the problem going on here ?

 
Niclas Wilson
Greenhorn
Posts: 1
Chrome Java MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check where you are setting the boolean value back to true.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please look at the contents page for this forum, where you will read this:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
By posting a complete solution, you are depriving the OP of the chance to learn from the problem. It is even worse if there is no explanation of the problem. Finding the problem for onesefl will teach far more.
Please don't be annoyed, but I have pulled rank and removed the complete solution.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sometimes the problem isn't what a number is...but WHERE it is...or HOW MANY numbers there are. I'd suggest you try this array:

int number[] = { 3, 5, 7, 9, 11 };

What would you expect this to print? what does it print?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmistha Sarkar wrote:I am trying to find prime number from an given array....

And maybe that's where your problem is. Why not just try to find out if A (single) number is prime or not?

Then you can concentrate on the problem and forget about all that boilerplate code, eg:Do you see the difference? Now your isPrime() method only has ONE thing to do: work out whether n is prime or not.
And if IT works properly, then all the other code must work.

BTW, very good to see you've separated out the Executor from the Prime class itself. Well done.
However, in general you'll want to make Prime public as well, so I'd suggest making them separate classes in the same package.

You can see a couple of methodologies here.

And thank you for a nicely produced question. Have a cow.

HIH

Winston
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for loop doesn't have to go as far as half the number.


It only has to go as far as the square root of the number.

This will make the code more efficiently handle large numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!