而循环素数在1 - 100之间

时间:2018-04-05 13:42:09

标签: java while-loop prime-factoring

我对编程很新,而且我正试图绕过循环。我设法让一段代码工作,但我仍然没有完全理解它是如何工作的。我在网上找到了类似程序的代码,这是用for循环编写的,我设法让它作为一个while循环工作(花了我几天!!!)。我试图理解内循环在做什么。

我知道外循环正在检查循环的每次迭代 x 是否小于100。为什么需要在循环中嵌套 y 变量,为什么它设置为2,为什么每次需要增加1?另外,有没有办法摆脱循环而不使用中断;

我在这里看到了这类程序的其他几个例子,但我希望有人可以说明这个程序是如何具体运作的。

提前致谢!!!

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++;
    }
  }
}

6 个答案:

答案 0 :(得分:2)

评论是你的朋友:

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++;
        }
    }
}

答案 1 :(得分:0)

变量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)。 if y==x返回true。这意味着你迭代了y的所有值。所以x是素数。所以你打印x。退出内循环。你增加x(x=6)。等等...

答案 2 :(得分:0)

为什么需要在循环中嵌套y变量,为什么它设置为2,为什么每次需要增加一个?

我们需要在循环中重置变量,因为需要对x的每个值应用测试。 x的值是素数的潜在候选者。为什么设置为2是因为每个数字都可以被1整除。

另外,有没有办法摆脱循环而不使用中断;吗

您可以向第二个while循环添加另一个条件。一旦满足退出条件,就可以设置此标志。

答案 3 :(得分:0)

正如你所看到的,你的第一个循环是检查x是否小于100.然后你的下一个同时划分所有小于x的数字,如果有一些数字小于x并且有0则修改它不是素数。例如我们在第一次迭代中x = 5 y = 2,5%2 = 1因为你不能分割因为mod不是零,那么y增加1,5%3 = 2 mod再次2不可分割,等等,你将y递增,直到5这意味着没有更小的整数,你可以从我们的x = 5是素数除以x。如果您不了解发生的事情,请尝试打印所有内容。

答案 4 :(得分:0)

要知道一个数字是否为素数,当数字(x)在他自己或1之间分配时,其余数字只会为零。

因此,在此代码中,您将每个数字(x)除以1和数字本身之间的所有数字(y)。

这就是

的原因

if (x % y == 0) { break; }

这意味着,如果将y除以1和X之间的rest = 0,则会停止循环,因为它不会成为素数。

您可以使用变量标志删除中断,但循环将执行更多迭代。

答案 5 :(得分:0)

print('素数在1到100之间是:')

对于范围(2,101)中的num: 如果num> 1: 对于范围(2,num)中的i: if(num%i)== 0: 打破 其他: 打印(数字)