Results 1 to 3 of 3
  1. #1
    dextr is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Finding nth prime number

    I am trying to write a code for a program that will find the n th prime number.As for example 7 is the 4th prime number,11 is the 5th.
    The method will take argument as n,and it will find the nth prime number.But I am facing some difficulty with the code.The code doesnt terminate by itself and doesnt return the desired value.Someone please help me out.
    Java Code:
      
    public class Problem{
    
    	/**
    	 * @param args
    	 */
    	public int findAns(int n){
    int count=0;
    int ret=0;
    while(count<n){
    	for(int i=0;i<Integer.MAX_VALUE;i++){
    		if(ifPrime(i)){
    			count++;
    		}
    		ret=i;
    	}
    }
    return ret;
    	}
    		public boolean ifPrime(int num){
    			boolean b=false;
    			int i;
    		    for (i=2; i < num ;i++ ){
    		      int n = num%i;
    		      if (n==0){
    		        b=false;
    		        break;
    		      }
    		    }
    		    if(i == num){
    		      b=true;
    		    }
    		    return b;
    		}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    System.out.println(new Problem().findAns(10));
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you sure that you want both a for loop and a while loop here:
    Java Code:
        while(count<n){
    	for(int i=0;i<Integer.MAX_VALUE;i++){
    		if(ifPrime(i)){
    			count++;
    		}
    		ret=i;
    	}
        }
    Also, if you correct your indentation, it would make your code a lot easier for others to read, and would help you get more helpful answers. Best of luck.

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    your

    for(int i=0;i<Integer.MAX_VALUE;i++){

    will never terminate (well not until you went through all integers). instead, do

    for(int i=0;count < n && i<Integer.MAX_VALUE;i++){

    and you should be good to go. That's not to say the algorithm could not be improved...

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 09:25 PM
  2. Replies: 10
    Last Post: 02-20-2010, 09:25 PM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. 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
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 09:14 AM

Posting Permissions

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