Quote:
How can I improve my counting prime numbers function?
there is nothing to improve, because what you have done is not working.
the whole logic of your code is to be reviewed.
Your logic actually say that
i
is a prime if not a divisor of
n
, it is plain wrong.
You need to create a function that tells you if a number is a prime and change your code to:
public int countPrimes(int n) {
if(n == 0){
return 0;
}
int count = 0;
for(int i = 2; i <= n; i++){
if(IsPrime( i )){
count++;
}
}
return count;
}
public int IsPrime(int n) {
}