简体   繁体   English

素数C ++

[英]Prime numbers c++

I have this code: 我有以下代码:

#include <iostream>
#include <cmath>

using namespace std;

int n, liczba;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> liczba;
        if (liczba < 2) {
            cout << "NIE" << endl;
        } else if (liczba == 2) {
            cout << "TAK" << endl;
        }

        for (int i = 2; i < liczba; i++) {
            if (liczba % i == 0) {
                cout << "NIE" << endl;
                break;
            } else if (liczba % i != 0) {
                cout << "TAK" << endl;
                break;
            }
        }
    }
    return 0;
}

This program is supposed to write yes "TAK" or no "NIE" whether the number you input is prime or isn't. 无论您输入的数字是素数还是否,该程序都应该写"TAK""NIE" Variable n is the number of numbers you want to input into program, and liczba is the number you want to check if it's prime or not. 变量n是要输入到程序中的数字数,而liczba是要检查其是否为质数的数字。 It seems to work fine expect one significant thing. 期待一件重要的事情似乎很好。 If I input number 9 it says yes "TAK" instead of no "NIE" .. I discovered that this happens to numbers: 9,27,45,63,81 and so on.. if I add 18 starting from 9 it will happen every time. 如果我输入数字9,它说是"TAK"而不是"NIE" ..我发现这恰好发生在数字上: 9,27,45,63,81 ,依此类推..如果我从9开始加18 ,它将每次都会发生。

What's wrong with my code? 我的代码有什么问题?

You're break ing on BOTH sides of your if() test. break荷兰国际集团在您的两面if()测试。 Effectly you'll only ever test one divisor: 实际上,您只会测试一个除数:

eg liczba = 9 例如liczba = 9

1. if (liczba % 2 == 0) -> if (9 % 2 == 0) -> if (1 == 0) -> false
2. ...jump to else
3. if (liczba % 2 != 0) -> if (9 % 2 != 0) -> if (1 != 0) -> TRUE
4. spit out 'tak' and break out of the loop

You cannot break out of the loop "early" if you get a remainder. 如果有剩余,则不能“尽早”突破循环。 That means the divisor you tested is NOT a factor of the number. 这意味着您测试的除数不是数量的因数。 You can only break early if you DO get a remainder of 0 , which means the number's not prime - it's composite. 如果您确实得到0的余数,则只能提早休息,这意味着该数字不是素数-它是合成的。

Hint: 暗示:

All prime numbers (except 2 and 3) can be expressed in the form 6k+1 or 6k-1 , where k is a positive whole number. 所有素数(2和3除外)都可以6k+16k-1的形式表示,其中k是正整数。

Therefore, This should work: 因此,这应该工作:

bool IsPrime( int number )
{

 if ( ( (!(number & 1)) && number != 2 ) || (number < 2) || (number % 3 == 0 && number != 3) )
      return (false);

 for( int k = 1; 36*k*k-12*k < number;++k)
     if ( (number % (6*k+1) == 0) || (number % (6*k-1) == 0) )
         return (false);
     return true;
}

Taken from Determining if a number is prime . 取自确定数字是否为质数

You have not used a flag and thus it says NIE after first check. 您尚未使用标志,因此在第一次检查后会显示NIE Also, you are missing an else statement. 此外,您缺少else语句。 Try this instead: 尝试以下方法:

#include <iostream>
#include <cmath>
using namespace std;
int n,liczba;
int main()
{
    cin >> n; //
    for(int i=0;i<n;i++)
        {
            cin>>liczba;
            if (liczba < 2)
                {
                cout << "NIE" << endl;
                }
            else if (liczba == 2)
            {
                cout << "TAK" << endl;
            }
            else
            {
                bool isPrime = true;
                for (int i=2;i<liczba;i++)
                {

                    if (liczba % i == 0)
                        {
                            isPrime = false;
                            break;
                        } 
                }
                if(isPrime)
                    cout<<"TAK";
                else
                    cout<<"NIE";
            }

        }
    return 0;
}

Currently, if you input 7, It will check if 7 % 2 == 0 . 当前,如果输入7,它将检查7 % 2 == 0 Since it is not, it will print "NIE" and break out of the loop. 由于不是,它将打印“ NIE”并退出循环。

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

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