【问题标题】:finding prime factors of a given number c++ without functions在没有函数的情况下找到给定数字c ++的素数
【发布时间】:2014-05-02 07:04:55
【问题描述】:

我需要找出用户输入的数字的质因数

例子:

输入一个数字:1430。1430 的质因数是 2,5,11,13

我不想使用函数,因为我还没有介绍过它

这是我的代码

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std ;

int main () 
{
    int count, i , i2 ;
    int userInput, prime ;
    bool flag = false ;

    cout << "Enterr: " ;
    cin >> userInput ;
    cout << "The prime factors are: " ;
    for (i = 2; i < userInput ; i++)
    {
        if (userInput % i == 0) // this for loop is to check is the counter (i) is a multiple of userInput
        {   
            prime = i;
            // the next for loop is to check is the multiple is a prime number
            for( i2 = 2; i2< ceil(sqrt(prime))  ; i2++)
            {
                if (prime % i2 == 0)
                    flag = true ;
            }
        }
        if (flag == false ) 
            cout << i << ", "  ;
        flag = false ;
    }

    cout<< endl ;

    return 0 ;
}

我的输出完全忽略了第二个循环,只输出小于userInput的所有整数

我可以在这里创建一段代码来检查一个数字是否是质数:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std ;

int main () 
{
    int userInput, prime ;
    int i ;
    bool flag = false ;

    cin >> userInput ;

    for( i = 2; i < static_cast<int>(sqrt(userInput) + 1) ; i++)
    {
        if (userInput % i == 0)
            flag = true;        
    }

    if (flag == false ) 
        cout << "Number is a prime" << endl ;
    else
        cout << "Number is not a prime " << endl ;

    return 0 ; 
}

如有错误,请见谅。我在C++还是个初学者

【问题讨论】:

  • 正在使用一个函数。你认为main() 是什么?
  • @wallyk:或者更有趣的是ceilsqrt

标签: c++ primes prime-factoring


【解决方案1】:

你快到了 - 你只需要移动这个块

  if (flag == false )
          cout << i << ", "  ;
      flag = false ;

进入你的

if (userInput % i == 0)

块(您只想打印作为输入数字除数的数字),一切都应该没问题。

【讨论】:

    【解决方案2】:

    你不需要测试你找到的除数的素数,如果你在找到它们时将它们从你的数字中除掉,同时按升序列举候选者:

        for (i = 2; i <= userInput/i;  )
        {
            if (userInput % i == 0) 
            { 
                cout << i << ", ";   // i is a prime factor of the original
                userInput /= i;      //   number in userInput
            }
            else
                ++i;
        }
        if (userInput > 1)
        {
            cout << userInput;       // the biggest prime factor of the original
        }                            //   input number 
    

    因此找到的输入数的素数也按升序打印出来。

    【讨论】:

      【解决方案3】:

      您是否尝试调试(甚至是在心理上)您的程序中发生了什么?我对此表示怀疑,否则您会注意到以下内容:

      if (userInput % i == 0)
      

      对于所有除数都是错误的。这意味着你会直接去check

      if (flag == false ) 
      

      这是真的,因为您没有将标志更改为真,因此您错误地将数字作为主要因素。

      您还应该考虑极端情况。例如,质数的质因数只是质数本身(您永远无法输出,因为您永远无法到达它(i &lt; userInput;

      关于你的无函数限制:你不能避免函数,否则你的代码不会做任何事情。但是,您可以避免看起来像函数调用的事情(看起来像 name())。

      要消除sqrtceil,您应该将条件重新排列为i2 * i2 &lt;= prime

      【讨论】:

      • 嗯?为什么if (userInput % i == 0) 对于所有非素因数都是错误的?你的意思是所有不是因数的数字
      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2016-06-01
      相关资源
      最近更新 更多