简体   繁体   English

我不了解此质数检查器(Java)背后的逻辑

[英]I do not understand the logic behind this prime number checker (Java)

I do not understand the logic behind this number checker and I'm wondering if somebody could help me understand it a little bit better. 我不明白这个数字检查器背后的逻辑,我想知道是否有人可以帮助我更好地理解它。

Here's the code: 这是代码:

I will do my best to comment on what's happening but I do not fully understand it. 我将尽力评论正在发生的事情,但我不完全了解。

//find prime numbers between 2 and 100

class PrimeNumberFinder {
    public static void main(String args[]) {

        int i, j; // declare the integer variables "i" and "j"
        boolean isPrime; // declare the Boolean variable is prime but do not assign value

        // create a for loop that starts at two and stops at 99.
        for (i=2; i < 100 ; i++) {
            isPrime = true; // I do not know why isPrime is set to true here.
            // This is where I get confused badly.. we give the "j" variable a value of two and check to see if it's less than whatever "i" divided by "j" is.             
            // If "i=2" then how would j (which is = 2) be less than or equal to i/j (2/2)? 

            for (j = 2; j <= i/j; j++)
                if ((i%j) == 0) isPrime = false; // If a certain number goes in evenly that isn't 1, or "i" itself, it isn't prime so we set the boolean to false

            if (isPrime) // if true print i
                System.out.println(i + " Is a prime number");


        }
    }
}

As you can see the second for loop and almost everything going on within it confuses me, especially the "j <= i/j" because to me j is always going to be bigger.. and why is "j" even increasing? 如您所见,第二个for循环及其中发生的几乎所有事情都使我感到困惑,尤其是“ j <= i / j”,因为对我而言j总是会变得更大。为什么“ j”甚至会增加? Can't you just divide it by two and determine whether or not it's prime that way? 您不能仅将其除以2并确定它是否是素数?

Any help is greatly appreciated, thank you for reading. 非常感谢您的帮助,感谢您的阅读。

Let's go through it line by line. 让我们逐行处理它。

int i, j;
boolean isPrime;

We start with declaring our variables. 我们首先声明变量。 Nothing too fancy. 没什么好看的。

for (i=2; i < 100; i++) {
    isPrime = true;

Here we enter our loop that basically contains all the number we are going to check (here: 2 - 99). 在这里,我们进入循环,该循环基本上包含我们要检查的所有数字(此处为2-99)。 We also state that the current number is a prime number (unless proven otherwise). 我们还声明,当前数字是质数(除非另有证明)。

    for (j = 2; j <= i/j; j++)
        if ((i%j) == 0) isPrime = false;

Now here is where the magic happens. 现在,这里就是魔术发生的地方。 We are going to check if we can divide the current number i evenly by any integer ranging from j == 2 to i/j ( i/j ultimately is just a fancy way of writing Math.sqrt(i) ). 我们将检查是否可以将当前数i平均除以从j == 2i/j的任何整数( i/j最终只是编写Math.sqrt(i) )。 So why up until there? 那么为什么要一直到那儿呢?

Well, say we have two divisors a and b such that a * b = i . 好吧,假设我们有两个因数ab ,使得a * b = i Now, if divisor a is bigger than the square root of i , then the other divisor b will be smaller than the square root of i . 现在,如果除数a大于i平方根,则另一个除数b将小于i平方根。 If not then a * b > i and this isn't possible. 如果不是,则a * b > i ,这是不可能的。

So, if we can find a case in which we can divide evenly, this explicitly means that the current number is not prime and we set the isPrime variable to false . 因此,如果我们找到了可以平均除法的情况,则这显然意味着当前数不是素数,并且将isPrime变量设置为false

    if (isPrime) // if true print i
        System.out.println(i + " Is a prime number");

}

So, if we still have isPrime == true , it means that the current number withstood our test and we can print it. 因此,如果我们仍然具有isPrime == true ,则意味着当前数字经受住了我们的测试,我们可以将其打印出来。

Two further improvements; 两个进一步的改进;

  1. Once we know the number is not prime, there is no need to check any additional divisors, so we want to exit the loop and hence a break; 一旦知道数字不是素数,就无需检查任何其他除数,因此我们想退出循环并因此break; statement could be addded. 语句可以添加。
  2. 2 is the only even prime number, so alternatively you could start the second loop at j == 3 and increase by 2 after every execution. 2唯一的偶数质数,因此也可以在j == 3处开始第二个循环,并在每次执行后加2 You'll then have to consider the case when i == 2 separately. 然后,您将不得不分别考虑i == 2的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM