首页 > 解决方案 > 我的 C++ 素数标识符和素因子查找器有一个错误

问题描述

我是 C++ 和这个网站的初学者,所以任何愚蠢的错误都是出于无知。为了练习,我正在尝试编写一个程序来识别素数并在用户请求时给出复合的素数。素数 IDer 有效,但素数因子无效。当我输入 12 作为我的数字时,它给了我因数 2 3 和 5,而 12 的质因数是 2 2 和 3。我在搞砸什么?这是代码。不要介意奇怪的空格或名称。

#include <iostream>
#include <vector>
#include <cmath>
#include <string>

//Printing factors help
void print(std::vector<int> const& factors)
{
    for (int i = 0; i < factors.size(); i++) {
        std::cout << factors.at(i) << ' ';
    }
}


int main() {

    //Restarting it
    std::string again;
    again = "Yes";

    //Actual loop
    while (again == "Yes") {

        //Variable/vectors
        std::string pfacts;
        double input = 0;
        double result = 0;
        double looper = 2;
        int looper2 = 2;
        int printed = 0;
        int printed2 = 0;

        std::vector<int> factors;
        std::vector<int> holders;

        //Asking for number
        std::cout << "Please enter your number.\n";
        std::cin >> input;
        double holder = input;

        //Ratting out trolls
        if (input == 0) {
            std::cout << "Your number is neither.\n";
            looper = 1000003;
        }

        if (input == 1) {
            std::cout << "Your number is neither.\n";
            looper = 1000003;
        }


        //Prime/composite loop
        while (looper < 1000002 and input != 1 and fmod(result, 1) == 0) {
            result = input / looper;

            //Finding composite
            if (fmod(result, 1) == 0 and printed == 0) {
                std::cout << "Your number is composite.\n";
                printed = 1;
                looper = 1000003;
            }

            //Finding prime
            else if (fmod(result, 1) != 0 and printed2 == 0) {
                std::cout << "Your number is prime.\n";
                printed2 = 1;
            }

        }

        //Asking about factors
        if (printed == 1) {
            std::cout << "Would you like to know it's prime factors? (Please type Yes or No exactly)\n";
            std::cin >> pfacts;
        }

        //Actually finding them
        if (pfacts == "Yes") {
            while (looper2 < 1000002) {

                if (holder / looper2 == 0) {
                    factors.push_back(holder);
                    looper2 = 1000003;
                }

                if (looper2 < 1000002 and fmod  (fmod(holder, looper2),   1) == 0 and looper2 % 2 != 0 or looper2 / 2 == 1) {
                    factors.push_back(looper2);
                    holder = holder / looper2;
                }
                looper2 = looper2 + 1;
            }

            //Printing them
            print(factors);
            std::cout << "\n";


        }













        //Again?
        std::cout << "Do you need to input another number? (Please type, exactly: Yes or No)\n";
        std::cin >> again;

    }

}

标签: c++

解决方案


你只是用looper,打印的东西让一个非常简单的事情变得更复杂,一个好的程序员是一个以简单的方式解决困难问题的人。所以我很难理解你在上面的代码中到底在做什么,至少没有足够的注释。

所以这是我的解决方案,我只是提供算法,以便您可以编写自己的代码并从中学习。

首先注意下面的事情。

  1. 素数是一个正整数,它有两个因子,第一个是 1,另一个是那个数字本身,所以两个是最小的素数。

  2. 如果一个数字不能被 2 整除到它的平方根,那么它也是素数,你应该考虑这个事实来提高性能。

现在算法:

  1. 取变量中的数字num

  2. 检查它是否是大于 1 的正整数,如果不是则它不是素数。

  3. num借助sqrt()变量中的函数取's squareroot,num因为您不再需要原始函数num

  4. 现在加num 一并取其绝对值。

  5. i = 2从to开始循环num:您从 2 开始,因为 2 是最小的素数。

  6. 在每次迭代检查num % i == 0中,如果是,则它不是质数并且您会中断,否则不做任何事情。

  7. 现在在循环结束后检查 if i == num,如果是,则意味着您从未破坏过,因此该数字是素数并且您已完成,否则您破坏了循环,因此该数字不是素数并且您要求素数。

  8. 现在你又从j = 2num

  9. 现在如果num % j == 0,你打印j并做num = num / j,否则你增加j

我希望它会有所帮助。我没有在 IDE 上测试它,因为我没有一个,因为我不再编写 c++ 代码了。如果您发现任何错误,请告诉我。


推荐阅读