While 循环 1 - 100 之间的质数
While Loop Prime Numbers between 1 - 100
我对编程还很陌生,我正在努力了解循环。我设法让一段代码工作,但我仍然不完全理解它是如何工作的。我在网上找到了一个类似程序的代码,它是使用 for 循环编写的,我设法让它像 while 循环一样工作(花了我几天时间!!!)。我试图了解内部循环在做什么。
我知道外循环正在检查循环的每次迭代 x 是否小于 100。为什么要在循环中嵌套y变量,为什么设置为2,为什么每次都需要自增1?另外,有没有办法在不使用 break; 的情况下跳出循环?
我在这里看到了此类程序的其他几个示例,但我希望有人可以阐明这个程序的具体工作原理。
提前致谢!!!
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++;
}
}
}
变量 x
迭代从 2 到 100 的所有数字。在这个循环中,您将处理一些东西以确定 x 是否是素数。您的代码所做的这件事是遍历从 2 到 x 的所有数字,并尝试每一个数字是否能除以 x。变量 y
就是第二个数字。
例如,当您处于 x = 4
的迭代时。 y 将首先等于 2。然后检查是否 x%y==0
,这意味着您检查 x 是否可以被 y 整除。在这种情况下,这是真的。所以 x 不是质数,所以你退出内部循环(break;
语句)。那么x=5
,你有y=2
。这不会除以 x。您增加 y (y=3
)。这也不除 x 。您增加 y (y=4
)。这不会除以 x。您增加 y (y=5
)。如果y==x
return 为真。这意味着您已经遍历了 y 的所有值。所以x是质数。所以你打印 x。你退出你的内循环。你递增 x (x=6
)。等等...
为什么要在循环中嵌套y变量,为什么设置为2,为什么每次都要加1?
我们需要在循环中重置变量,因为需要对 x 的每个值应用测试。 x 的值是素数的潜在候选者。为什么设置为2是因为每个数都能被1整除。
另外,有没有不用break就跳出循环的方法; ?
您可以向第二个 while 循环添加另一个条件。一旦满足退出条件,就可以设置此标志。
正如您所看到的,您的第一个循环正在检查 x 是否小于 100。然后您的下一个循环除以所有小于 x 的数字,如果有一些数字小于 x 并且有 0 mod 它不是素数。例如我们的 x = 5 在第一次迭代中 y = 2, 5 % 2 = 1 因为 mod 不为零,所以不可能进行除法,然后 y 递增 1, 5 % 3 = 2 mod 又是 2 不可整除,依此类推,y 递增直到 5,这意味着没有更小的整数可以除以 x,因为我们的 x=5 是质数。当你不明白发生了什么事时,试着打印所有的东西。
评论是你的朋友:
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++;
}
}
}
并且更有帮助地命名您的变量,它变得更加容易
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++;
}
}
}
要知道一个数是不是质数,只有当这个数(x)除以自己或1时,余数才为零
所以在这段代码中,你将每个数字 (x) 除以 1 和数字本身之间的所有数字 (y)。
这就是
的原因
if (x % y == 0) {
break;
}
这意味着,如果除以 y
在 1 和 X 之间有一个 rest = 0
,您将停止循环,因为它不会是素数。
您可以使用变量标志删除中断,但循环会进行更多次迭代。
打印('Prime numbers between 1 and 100 are:')
对于范围内的数字 (2,101):
如果数字 > 1:
对于范围内的我(2,数字):
如果 (num % i) == 0:
休息
别的:
打印(数字)
我对编程还很陌生,我正在努力了解循环。我设法让一段代码工作,但我仍然不完全理解它是如何工作的。我在网上找到了一个类似程序的代码,它是使用 for 循环编写的,我设法让它像 while 循环一样工作(花了我几天时间!!!)。我试图了解内部循环在做什么。
我知道外循环正在检查循环的每次迭代 x 是否小于 100。为什么要在循环中嵌套y变量,为什么设置为2,为什么每次都需要自增1?另外,有没有办法在不使用 break; 的情况下跳出循环?
我在这里看到了此类程序的其他几个示例,但我希望有人可以阐明这个程序的具体工作原理。
提前致谢!!!
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++;
}
}
}
变量 x
迭代从 2 到 100 的所有数字。在这个循环中,您将处理一些东西以确定 x 是否是素数。您的代码所做的这件事是遍历从 2 到 x 的所有数字,并尝试每一个数字是否能除以 x。变量 y
就是第二个数字。
例如,当您处于 x = 4
的迭代时。 y 将首先等于 2。然后检查是否 x%y==0
,这意味着您检查 x 是否可以被 y 整除。在这种情况下,这是真的。所以 x 不是质数,所以你退出内部循环(break;
语句)。那么x=5
,你有y=2
。这不会除以 x。您增加 y (y=3
)。这也不除 x 。您增加 y (y=4
)。这不会除以 x。您增加 y (y=5
)。如果y==x
return 为真。这意味着您已经遍历了 y 的所有值。所以x是质数。所以你打印 x。你退出你的内循环。你递增 x (x=6
)。等等...
为什么要在循环中嵌套y变量,为什么设置为2,为什么每次都要加1?
我们需要在循环中重置变量,因为需要对 x 的每个值应用测试。 x 的值是素数的潜在候选者。为什么设置为2是因为每个数都能被1整除。
另外,有没有不用break就跳出循环的方法; ?
您可以向第二个 while 循环添加另一个条件。一旦满足退出条件,就可以设置此标志。
正如您所看到的,您的第一个循环正在检查 x 是否小于 100。然后您的下一个循环除以所有小于 x 的数字,如果有一些数字小于 x 并且有 0 mod 它不是素数。例如我们的 x = 5 在第一次迭代中 y = 2, 5 % 2 = 1 因为 mod 不为零,所以不可能进行除法,然后 y 递增 1, 5 % 3 = 2 mod 又是 2 不可整除,依此类推,y 递增直到 5,这意味着没有更小的整数可以除以 x,因为我们的 x=5 是质数。当你不明白发生了什么事时,试着打印所有的东西。
评论是你的朋友:
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++;
}
}
}
并且更有帮助地命名您的变量,它变得更加容易
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++;
}
}
}
要知道一个数是不是质数,只有当这个数(x)除以自己或1时,余数才为零
所以在这段代码中,你将每个数字 (x) 除以 1 和数字本身之间的所有数字 (y)。
这就是
的原因if (x % y == 0) {
break;
}
这意味着,如果除以 y
在 1 和 X 之间有一个 rest = 0
,您将停止循环,因为它不会是素数。
您可以使用变量标志删除中断,但循环会进行更多次迭代。
打印('Prime numbers between 1 and 100 are:')
对于范围内的数字 (2,101): 如果数字 > 1: 对于范围内的我(2,数字): 如果 (num % i) == 0: 休息 别的: 打印(数字)