Niloo Rad wrote:something is wrong with my code and i can't figure it out! will you help me to find it?

Well, you haven't given us all of it. Where is MAX_PRIME defined? Without that and your isPrime() method, it's a bit tough to work out exactly what's wrong.

However, there is one simple optimization: you DON'T have to check every number.
In fact, (tip) ALL primes greater than 3 are equal to 6k ± 1, where k is a positive integer.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Don’t That makes Winston’s reply look nonsense. It means nobody can work out the changes. Please repost the original code and a new post with the changes. And read this.

I'm sure your motives were great, but we don't like people to post ready-made solutions here - particularly in the 'Beginners' forum.
It's much better to guide someone to their own solution. Please read the NotACodeMill (←click) page.

I've kept your original post, and if my colleagues ask me to, I'll restore it.

so you pass in 3 numbers assign them to variables with very bad names a,b,c so we have no idea what you intended to do with them, you then do nothing with them.

the controlling loop in countPrimes always goes from 2 to MAX_PRIME.

this is just a nice layout thing...
you do not need to indent the validation try catch blocks within each other

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481

23

posted

0

This sounds like a project Euler question. Is it?
I would suggest you create an array and use a sieve of Eratosthenes to work out whether each number is prime. Then count the primes. 0 and 1 don’t count. Much more effecient than an isPrime method.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.

subject: How many prime Exist between two numbers?