• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Prime number Program

 
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 ?

 
Greenhorn
Posts: 1
MyEclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 65407
248
  • 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: 65407
248
  • 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.
 
lowercase baba
Posts: 12760
51
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?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
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.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!