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):
Code Java:
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
Re: Repeating infinite loop in prime number finder
Quote:
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.
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.
Re: Repeating infinite loop in prime number finder