Results 1 to 8 of 8
  1. #1
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Question 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. #2
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: countPrime & isPrime Help

    So what is the problem?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: countPrime & isPrime Help

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: countPrime & isPrime Help

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

  5. #5
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: countPrime & isPrime Help

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

    kind regards,

    Jos

    can you give me a hint?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: countPrime & isPrime Help

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: countPrime & isPrime Help

    Quote Originally Posted by JosAH View Post
    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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Palindromic Prime - Combining isPrime and isPalindrome
    By javanewbie123 in forum New To Java
    Replies: 4
    Last Post: 03-14-2012, 01:19 AM

Tags for this Thread

Posting Permissions

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