# countPrime & isPrime Help

• 10-11-2012, 08:32 AM
niloufar
countPrime & isPrime Help
Hi guys,
I'm pretty new with java, and i suppose to do my project to find if my number is prime or not, below is my requirement:

1st integer: count the prime numbers between 0 and the integer, inclusively
i. Include the integer in the count if it is prime
ii. Neither 0 nor 1 is considered to be a prime number
iii. See the List of Prime Numbers file for a list of prime number less than 10,000
iv. See the Counting Prime Numbers Algorithm below the Additional Notes section for tips on counting prime numbers

Counting Prime Numbers Algorithm [C(n)]
Note 1: Neither 0 nor 1 is prime. If n is equal to 0 or 1, the prime number count must be 0.
-For every positive integer (i) greater than 1 and less than or equal to n
- i is prime if and only if it cannot be evenly divided by any positive number between 2 and the square root of i (√i), inclusively
-Since all even numbers can be evenly divided by 2, there is no need to test even numbers greater than 2
-If i is prime, add 1 to the prime number count

and here is my code:

Code:

```// Verify Prime Number private static boolean isPrime(int prime) {                 boolean isPrime=true;                                 for(int i = 2; i <= Math.sqrt(prime); i++) {             if ((prime % i) == 0) {                     isPrime = false;             }           }         return isPrime;     }                             // Calculate Prime         private static int countPrime(int num1){                         int i = 2 ;                                 if (num1 == 0 || num1 == 1 && i==0 ){                 if ((i >1 ) && (i<= num1)){                                                                                 if (isPrime(num1)){                         System.out.println(i + " is prime");                         ++num1;                 }                         else {                                 System.out.println(i + " is Not prime");                           }                         }             }                 return num1; }```
• 10-11-2012, 08:50 AM
sabre150
Re: countPrime & isPrime Help
So what is the problem?
• 10-11-2012, 09:09 AM
JosAH
Re: countPrime & isPrime Help
I don't see a loop of any sort in your countPrime( ... ) method ...

kind regards,

Jos
• 10-11-2012, 04:58 PM
niloufar
Re: countPrime & isPrime Help
Quote:

Originally Posted by sabre150
So what is the problem?

I don't know if this works at all??
• 10-11-2012, 04:59 PM
niloufar
Re: countPrime & isPrime Help
Quote:

Originally Posted by JosAH
I don't see a loop of any sort in your countPrime( ... ) method ...

kind regards,

Jos

can you give me a hint?
• 10-11-2012, 05:07 PM
JosAH
Re: countPrime & isPrime Help
Quote:

Originally Posted by niloufar
can you give me a hint?

erm ... add a loop?

kind regards,

Jos
• 10-11-2012, 05:33 PM
niloufar
Re: countPrime & isPrime Help
Quote:

Originally Posted by JosAH
erm ... add a loop?

kind regards,

Jos

does this one make sense? :(sweat):
Code:

```// Calculate Prime         private static int countPrime(int num1){                         int i = 2 ;                                 while (num1 == 0 || num1 == 1 && i==0 ){                 if ((i >1 ) && (i<= num1)){                                                                                 if (isPrime(num1)){                         System.out.println(i + " is prime");                         ++num1;                 }                         else {                                 System.out.println(i + " is Not prime");                           }                         }             }                 return num1;```
• 10-11-2012, 06:42 PM
JosAH
Re: countPrime & isPrime Help
Check it manually: suppose num1 == 5 (you want to check all numbers less than 5). Is the condition in the while loop true? Does that loop enter the code in its body? Your program logic is completely convoluted and hardly makes any sense. I'd expected something like this as the body of that method:

Code:

```int nofPrimes=0; // the number of primes for (int i= 2; i*i <= num1; i++) // numbers in the range 2 ...sqrt(num1)   // you do this part return nofPrimes;```
kind regards,

Jos