is inumber是否为主要数字,使用for循环。n/2。混淆

huangapple go评论75阅读模式
英文:

is inumber Primary Number or not with forloop . n/2 . confusion

问题

在下面的示例代码中,代码告诉你一个数字是否是质数,使用了一个for循环。我理解大部分代码,但有一件事情让我困惑。首先,在检查2是否是质数时,程序告诉你它是一个质数,这是正确的,但我不明白为什么当它与i-2进行比较时,在第一次迭代时,程序告诉你它是质数。代码中是什么决定它是质数的呢?我们知道2%2不余数,但代码仍然告诉你它是质数。

更令人困惑的是,当我检查9是否是质数时,代码说它不是质数,这是正确的,但实际上,代码应该立即告诉你它是质数,而不需要在检查i-2后再进行检查。9%2有余数,而且"break"应该停止循环,不再继续检查下一个迭代i=3,而在那时才正确决定它不是质数。9%3没有余数,所以它不是质数。在这个程序中,是什么决定它应该与i-3进行比较呢?感谢。

#include <iostream>
using namespace std;

int main() {
    
    int i, n;
    bool is_prime = true;
    
    cout << "Enter a positive integer: ";
    cin >> n;
    
  
    if (n == 0 || n == 1) {
        is_prime = false;
    }
    
    
    for (i = 2; i <= n/2; ++i) {
        if (n % i == 0) {
            is_prime = false;
            break;
        }
    }
    
    if (is_prime){
        cout << n << " is a prime number" << " checked against : " << i << endl;
        cout << endl;
    }
    else
        cout << n << " is not a prime number" << " checked against : " << i << endl;
    cout << endl;
    
    return 0;
}
英文:

in example below code tells you if number is Primary or not in a for loop . i understand most of it but one thing confuses me . why , first: when i check if 2 is Prmary number or not progrmamme tells you that it is Prmary number which is correct but i don't understand why when it is checked against i -2 , first iteration it tells you that it is . what in a code decides that it is Primary number ? we know that 2%2 leaves no remainder. code still tell you it is a Primary number.

2nd and even more confusing , when i checking if 9 is Prmary number , code says it is not a Primary number which is correct but in essence code should tell you that it is Primary number immediately after 9 is checked against i-2 . 9%2 leaves a remainder and " break " should stop the loop without checking it with next iteration i=3 which is when it is correctly decided that it is not aPrimary number . 9%3 leaves no remainder and it is not a Primary number . what in this programme decides that it shold be checked against i-3 ?

Thanks.

using namespace std;

int main() {
    
    int i, n;
    bool is_prime = true;
    
    cout &lt;&lt; &quot;Enter a positive integer: &quot;;
    cin &gt;&gt; n;
    
  
    if (n == 0 || n == 1) {
        is_prime = false;
    }
    
    
    for (i = 2; i &lt;= n/2; ++i) {
        if (n % i == 0) {
            is_prime = false;
            break;
        }
    }
    
    if (is_prime){
        cout &lt;&lt; n &lt;&lt; &quot; is a prime number&quot; &lt;&lt; &quot; checked against : &quot; &lt;&lt; i &lt;&lt; endl;
        cout &lt;&lt; endl;
    }
    else
        cout &lt;&lt; n &lt;&lt; &quot; is not a prime number&quot;&lt;&lt; &quot; checked against : &quot; &lt;&lt; i &lt;&lt; endl;
    cout &lt;&lt; endl;
    
    return 0;
}

答案1

得分: 1

我认为问题在于你没有理解循环。
for (i = 2; i &lt;= n/2; ++i) 表示从2开始,只要 i 小于 n 的一半,就执行循环。

n=2 时,i 的值永远不会小于 n/2(1),所以根本不执行循环。

n=9 时,i 的值可以在2到4之间变化(整数除法向下取整)。所以它会检查这些值,在达到3时跳出循环。

循环中的 break 语句只有在没有余数时才会触发,所以 (9 % 2) 不会执行那段代码。

英文:

I think the issue is that you are not understanding the loop.
for (i = 2; i &lt;= n/2; ++i) says that you start with a value of 2, and as long as i is less than half of n, execute the loop.

When n=2, the value of i is never less than n/2 (1) so it does not execute the loop at all.

When n=9, the value if i can range from 2 to 4 (integer division rounds down). So it will check those values, dropping out when it hits 3.

The break statement in the loop is only triggered when there is no remainder, so (9 % 2) will not execute that code.

答案2

得分: 0

我花了差不多一个月的时间来理解它。现在有意义了。使用2,我错过了2是唯一具有2个因子的偶数。这是我主要困惑的原因,不是因为给定代码中的for循环的工作方式。关于为什么9和一些其他数字在第一次迭代i=2时没有被报告为质数的困惑,是因为该数字(比如说9)必须经过所有由n/2定义的迭代,直到它被能够整除而不产生余数的i(数字),然后循环才会停止,即"break",对于9的情况是i=3。

谢谢。我对编程非常陌生,之前没有经验,所以需要一点时间来理解编程的基础知识。

英文:

I spent almost a month figuring it out . it makes sence now . with 2 i missed the fact that 2 is the only even namber that has 2 only factors. that's what i was confused mainly not because the way for loop in given code works . confusion about why 9 and some other numbers are not reported as the Prime number when it is checked against first iteration i= 2 was that number (lets say 9) has to go though all the iterations defined by n/2 untill it hits i(numer)that divides it without remainder and only then loop is stoped " by "brak;" i=3 in case of 9 .

Thanks . i'm very new to coding had no prior experience so it takes little time untill i get my head around basics of coding

huangapple
  • 本文由 发表于 2023年2月24日 04:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定