Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Repeating infinite loop in prime number finder

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Repeating infinite loop in prime number finder

    Hi!,

    I'm trying to write a short code for my own curiosity that will "sieve out" prime numbers from a range of positive integers (with an upper bound defined by the user) by dividing each integer within the set by all the previous integers before it (i.e. 5 by 1, 2, 3, and 4) and evaluating the remainders for 0 values (except when dividing by 1 and the number itself), and then adding the prime numbers to an array list which will display itself at the end.

    For example:

    User inputs the value 10 as an upper bound.
    We assume 1 is a prime number and add it to our array list to start.
    The program will then divide 2 by 1 and 2, then 3 by 1, 2 and 3, then 4 by 1, 2, 3 and 4; and so on and so forth.
    If the remainder of any of these operations (except in the case of division by 1 and the number we are currently evaluating) is equal to zero, the program moves on to the next number in the list until it has evaluated every integer up to/including 10.
    If none of the operations result in a remainder equal to 0 (except, again, in those special cases noted above), the number will be added to the array list before moving on to the next number in the sequence.
    The array list will display [1, 2, 3, 5, 7] at the end.


    So far the code I have written is as follows (it was a relatively short code, so I'm including the entire thing here so you can follow the operations if you'd like):

     
    import java.util.Scanner;
    import java.util.*;
     
    public class EratosthenesSieve
    {
    	public static void main(String args[])
    	{
    		Scanner keyboard = new Scanner(System.in);
    		ArrayList list = new ArrayList();
     
    		list.add(1);
     
    		int boundary = 0;
     
    		System.out.println("Please enter the positive integer you would like evaluated to:");
    		boundary = keyboard.nextInt();
     
    		while (boundary < 1)
    		{
    			System.out.println("I'm sorry, but that's not a valid number. Please enter a positive integer " +
    										"to be evaluated to:");
    			boundary = keyboard.nextInt();
    		}
     
     
    		int counter = 2;
    		int currentNumber = 2;
    		int remainder = 0;
     
     
    		do
    		{			
    			while(counter <= currentNumber)
    			{
    				remainder = currentNumber % counter;
    				System.out.println("Your remainder for this operation is: " + remainder);
     
    				if (remainder == 0 && counter != currentNumber)
    				{
    					currentNumber++;
    					counter = 2;
    				}
    				else if (remainder == 0 && counter == currentNumber)
    				{
    					list.add(currentNumber);
    					currentNumber++;
    					counter = 2;
    				}
    				else if (remainder > 0)
    				{
    					counter++;
    				}			
    			}
    		} while (currentNumber < boundary);
     
    		System.out.println("Your prime numbers are: " + list);
    	}
    }


    Currently when it runs, it gets stuck in an infinite loop inside of the first do-while, and I'm not sure why... I'm hoping it's something blindingly obvious that I'm just not seeing (I haven't programmed in well over a year, so I'm rusty at best) but I'm not too sure about the logic of those loops, either. Anyway, I've been staring at this for hours now and I can't figure out what's going on, so I would very much appreciate any advice/insight about this that anyone has to offer! (Thanks either way!)

    ~dp
    Last edited by dontpanic; August 6th, 2012 at 01:19 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Repeating infinite loop in prime number finder

    it gets stuck in an infinite loop inside of the first do-while, and I'm not sure why..
    Try debugging the code to see shy it doesn't exit the loop. Add some println statements that print out the values of the variables used and changed in the loop so you can see what the computer sees.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Repeating infinite loop in prime number finder

    Ahh, thanks! I tried that and it turns out it wasn't stopping at the boundary number entered by the user, so I added an extra condition to the inner while loop and took away the outside one and it seems to be working now! Yay! Thank you.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Repeating infinite loop in prime number finder

    Glad you figured it out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Infinite loop problem
    By jacobjc3 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 30th, 2012, 09:41 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 03:40 AM
  4. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 07:45 AM
  5. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM