• 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

Generating Prime Number

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I Just want to ask what is wrong about
my codes in generating the prime nos.


any reply would be appreciated. thanks!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leigh, welcome to the Ranch!

Why don't you start by telling us why you think there is something wrong with your code. Does it not compile? Throw an exception when you run it? Read our FAQ entry TellTheDetails to find out about things which would be useful to anybody wanting to help out with the learning process.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your tests (i%j==0 and i==j) are true for every i, since your inner loop runs till j is equal to i.

It's not a good test either. Prime number is a number which is divisible only by one and itself. You should be looking for numbers (n) that do not have any divisor between 2 and n-1. Try to rewrite your condition that way.

(However, this is still quite inefficient way to find primes. There are many possible optimizations. For example, no prime except 2 is even, so you might put in 2 in advance and then only test odd numbers starting at three. And there are others. If you're really interested, I'd suggest doing a small research on prime numbers first.)
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are lots of errors. It's good if you can study more about prime numbers..
 
Leigh Castillo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys to the replies..

I already find out the code
I just need to put break when the no. didn't passed 2nd condition..

Thanks again ^^
 
reply
    Bookmark Topic Watch Topic
  • New Topic