• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

prime numbers -please help

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//My aim is to find prime numbers from 1 to 100. Here i have done what i could think

//of. //But i could not get the answer. Please let me know where i am wrong.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your reasoning behind this?

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A
{
[deleted by FBR]
}
This is the correct code.
[ February 12, 2007: Message edited by: Fred Rosenberger ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code of Raj Kumar Bindal looks like it probably works, but it might not be the best code, depending what you are trying to achieve. It hard-codes the fact that the upper limit is 100. If you wanted a different limit, you'd need to rewrite the code and recompile. Lastly, if you're allowed to assume the primes between 1 and 10 (and I'm not sure all teachers/examiners would allow this), then why not just assume the values between 1 and 100, and just write a load of println() statements!

Perhaps a good exercise would be to work out what Raj is doing, then to re-code it more flexibly so that it would work for any upper limit. Then you have written a real, useful program.

(The "continue" is unnecessary, too. Just use != and && in the condition instead.)
[ February 12, 2007: Message edited by: Peter Chase ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another minor mathematical complaint about the program... one is not a prime number.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have deleted the solution provided by Raj Kumar Bindal. We'd much rather help people figure out how to code than just hand them a solution.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simplest (and possibly least efficient) way to test a number for being prime is to divide it by every number less than itself. if any return a 0 remainder, the number is not prime.

I'd suggest starting off writing that code. pick a number, say 91. write a loop that tests the remainder for every possible number from 2 to 90. set a boolean before you start, something like isPrime = true;. then, if you ever get a 0 remainder, set it to false.

after you check all the possible numbers, and exit the loop, use your boolean for printing prime/not prime.

I'd put all that code into a method.

Then, change the method so that instead of being hard-coded to testing 91, you can pass it a value to test. Write your main to pass it 91, then call it again with 17 then again with 48.

Then, write a loop that would call that method, passing in the values from 2 to 100 (or whatever).

Note that once you have it working, you can start tweaking the method that checks for primality without effecting anything else. Keep the original copy of the method, and make a primeCheck2() method. tweak that one - maybe you only need to check to up to 1/2 the original number. or maybe only up to the sqaure root.

maybe you could keep a list of the primes you've found, and only divide by those...

the point is that you want to separate out the different parts of your code. the prime-checking part should be completly separated from the "which number to check" part. which should be separate from the "get input from user and validate it" part...

These are some ideas to consider when writing your program.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A classic algorithm to solve this problem is aSieve of Eratosthenes. Maybe you could try your hand at implementing this in Java.
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help...Atlast i manage to come out with a solution



If i need to find the number of prime numbers between 1-100 then i would need a outer loop.

Thanks ya.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic