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

    Default Count Prime Numbers

    I have to count, how many prime numbers exist, but it give me wrong answer: Exactly 1254 prime numbers exist between 1 and 10,000.

    can you tell me why? Thank you!



    Java Code:
    // Verify Prime Number
    					private static boolean isPrime(int prime) {
    						for(int i = 2; i < Math.sqrt(prime) ; i++) {
    				            if ((prime % i) == 0) {
    				            	return false;
    				             }
    							  
    				           }
    						return true;
    				     }
    				    
    						
    					// Calculate Prime
    					private static int countPrimes(int number){
    						int count =0;
    												
    						for(int i=2; i<=MAX_PRIME; i++)	{
    							if(isPrime(i)){
    							count++;
    							}
    										
    					}
    					System.out.println("Exactly "+  count + " prime numbers exist between "+number + " and 10,000.");
    					return count;	
    									
    					}

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

Similar Threads

  1. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  2. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM
  3. (Help) Quotient summation with prime numbers
    By SapphireSpark in forum New To Java
    Replies: 27
    Last Post: 10-24-2008, 09:28 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 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
  •