Reputation: 23
I'm pretty new to programming, and I'm trying to get my head around loops. I managed to get a piece of code working, but I'm still not fully understanding how it works. I found code for a similar program online which was written using a for loop and I managed to get it to work as a while loop (took me a few days!!!). I'm trying to understand what the inner loop is doing.
I know the outer loop is checking that x is less than 100 for each iteration of the loop. Why the need to nest the y variable in the loop, why is it set to 2 and why does it need to increment by one each time? Also, is there a way to get out of the loop without using the break; ?
I've seen a few other examples of this type of program here, but I am hoping someone can shed some light on how this one is working specifically.
Thanks in advance!!!
class PrimeNumbers {
public static void main(String args[]) {
int x = 2;
while (x <= 100) {
int y = 2;
while (y <= x) {
if (x == y) {
System.out.println(x);
}
if (x % y == 0) {
break;
}
y++;
}
x++;
}
}
}
Upvotes: 1
Views: 11115
Reputation: 1
class Example{
public static void main(String[] args) {
int i=2;
int j;
int count;
while(i<100){
j=1;
count=0;
while(j<=i){
if (i%j==0){
count++;
}
j++;
}
if (count==2){
System.out.println(i);
}
i++;
}
}
}
Upvotes: 0
Reputation: 1
print('Prime numbers between 1 and 100 are:')
for num in range(2,101): if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num)
Upvotes: 0
Reputation: 1130
To know if a number is prime, the rest will only be zero when the number(x) is divided between himself or 1.
So in this code you divide each number(x) by all the numbers(y) between 1 and the number itself.
That's the reason when
if (x % y == 0) {
break;
}
This means,if dividing y
there is a rest = 0
between 1 and X you stop the loop cause it is not gonna be a prime.
You can remove the break using a variable flag but the loop would do many more iterations.
Upvotes: 0
Reputation: 65811
Comments are your friend:
class PrimeNumbers {
public static void main(String args[]) {
// No point checking below 2 since the definition of a prime is "A prime number (or a prime) is a natural number greater than 1 ..."
int x = 2;
// Looking for primes up-to and including 100
while (x <= 100) {
// Same argument as above - start checking at 2 and work upwards.
int y = 2;
// stop when y > x as obviously y will not divide x
while (y <= x) {
// if y reaches x then we have not found any divisors
if (x == y) {
// success! This x is prime.
System.out.println(x);
}
// if y divides x leaving no remainder then not prime - give up this y loop and select our next x
if (x % y == 0) {
break;
}
// Try next y divisor.
y++;
}
// Try next x candidate.
x++;
}
}
}
and naming your variables more helpfully it gets even easier
class PrimeNumbers {
public static void main(String args[]) {
// No point checking below 2 since the definition of a prime is "A prime number (or a prime) is a natural number greater than 1 ..."
int candidate = 2;
// Looking for primes up-to and including 100
while (candidate <= 100) {
// Same argument as above - start checking at 2 and work upwards.
int divisor = 2;
// stop when divisor > candidate as obviously divisor will not divide candidate
while (divisor <= candidate) {
// if divisor reaches candidate then we have not found any divisors (because of the `break` below).
if (candidate == divisor) {
// success! This candidate is prime.
System.out.println(candidate);
}
// if divisor divides candidate leaving no remainder then not prime - give up this divisor loop and select our next candidate
if (candidate % divisor == 0) {
break;
}
// Try next divisor.
divisor++;
}
// Try next candidate.
candidate++;
}
}
}
Upvotes: 2
Reputation: 625
As you can see your first loop is checking whether or not x is smaller than 100. Then your next while dividing all numbers that is smaller than x, and if there is some number that is smaller than x and have 0 mod it is not prime. For example our x = 5 in first iteration y = 2, 5 % 2 = 1 as you can it is not possible to divide because mod is not zero, then y increments by one, 5 % 3 = 2 mod is 2 again not dividable, and so on in the you would that y increments till the 5 this means there is no smaller integer that you could divide to x from that our x=5 is prime number. When you don't understand what is going on, try to print all things.
Upvotes: 0
Reputation: 152
Why the need to nest the y variable in the loop, why is it set to 2 and why does it need to increment by one each time?
We need to reset the variable within the loop because the test needs to be applied for each value of x. The values of x are the potential candidates for prime numbers. Why is it set to 2 is because every number is divisible by 1.
Also, is there a way to get out of the loop without using the break; ?
You can add another condition to the second while loop. This flag could then be set once you exit condition has been satisfied.
Upvotes: 0
Reputation: 2208
The variable x
iterate all the number from 2 to 100. Inside this loop you will process some stuff to determine if x is a prime number or not. This stuff your code do is to iterate through all the number from 2 to x and try for each one if it divides x. The variable y
is this second number.
For example when you're at the iteration where x = 4
. y will first be equal to 2. Then you check if x%y==0
, this means you check if x is divisible by y. In this case it's true. so x is not a prime number so you exit the inner loop (break;
statement). Then x=5
, you have y=2
. this does not divide x. you increment y (y=3
). This does not divide x either. You increment y (y=4
). This does not divide x. you increment y (y=5
). The if y==x
return true. It means you've iterate through all value of y. So x is a prime number. So you print x. You exit your inner loop. you increment x (x=6
). And so on...
Upvotes: 0