Results 1 to 8 of 8
Thread: countPrime & isPrime Help
 10112012, 08:32 AM #1Member
 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; }
 10112012, 08:50 AM #2Member
 Join Date
 Jul 2012
 Location
 Earth
 Posts
 75
 Rep Power
 0
Re: countPrime & isPrime Help
So what is the problem?
 10112012, 09:09 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,410
 Blog Entries
 7
 Rep Power
 25
Re: countPrime & isPrime Help
I don't see a loop of any sort in your countPrime( ... ) method ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10112012, 04:58 PM #4Member
 Join Date
 Sep 2012
 Posts
 26
 Rep Power
 0
 10112012, 04:59 PM #5Member
 Join Date
 Sep 2012
 Posts
 26
 Rep Power
 0
 10112012, 05:07 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,410
 Blog Entries
 7
 Rep Power
 25
 10112012, 05:33 PM #7Member
 Join Date
 Sep 2012
 Posts
 26
 Rep Power
 0
Re: countPrime & isPrime Help
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;
 10112012, 06:42 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,410
 Blog Entries
 7
 Rep Power
 25
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;
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Palindromic Prime  Combining isPrime and isPalindrome
By javanewbie123 in forum New To JavaReplies: 4Last Post: 03142012, 02:19 AM
Bookmarks