# Thread: countPrime & isPrime Help

1. Member
Join Date
Sep 2012
Posts
26
Rep Power
0

## 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:

Java 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;
}```

2. Member
Join Date
Jul 2012
Location
Earth
Posts
75
Rep Power
0

## Re: countPrime & isPrime Help

So what is the problem?

3. ## Re: countPrime & isPrime Help

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

kind regards,

Jos

4. Member
Join Date
Sep 2012
Posts
26
Rep Power
0

## Re: countPrime & isPrime Help

Originally Posted by sabre150
So what is the problem?
I don't know if this works at all??

5. Member
Join Date
Sep 2012
Posts
26
Rep Power
0

## Re: countPrime & isPrime Help

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?

6. ## Re: countPrime & isPrime Help

Originally Posted by niloufar
can you give me a hint?
erm ... add a loop?

kind regards,

Jos

7. Member
Join Date
Sep 2012
Posts
26
Rep Power
0

## Re: countPrime & isPrime Help

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

kind regards,

Jos
does this one make sense?
Java 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;```

8. ## 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:

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•