简体   繁体   English

找出一个数字的因素。 没有得到准确的结果

[英]Finding factors of a number. Not getting accurate results

Can someone help correct my algorithm? 有人可以帮助纠正我的算法吗? I've tested it on a few numbers, and it doesn't output the complete factorization. 我已经在一些数字上测试了它,并没有输出完整的因子分解。 For numbers with a large number of factors, it just completely fails. 对于具有大量因素的数字,它只是完全失败。

int num = 20;

for(int i = 2; i <= num; i++)
{
    if(num%i == 0)
    {
        cout << i << endl;
        cout << num << endl;
        num = num/i;    
    }
}

EDIT: The two answers provided did not work, still not getting full results. 编辑:提供的两个答案不起作用,仍然没有得到完整的结果。

EDIT2: Divisors VS Factors EDIT2:除数VS因子

Judging from you comment to @ Luchian Grigore , you're confusing divisors with (prime) factorization . 从你对@ Luchian Grigore评论来判断,你Luchian Grigore 除数(素数)因子分解混淆。 Divisors of a number are all numbers for which num % i == 0 is true. 数字的除数是num % i == 0为真的所有数字。 Factorization means getting a representation of num by a product of smaller numbers. 分解意味着通过较小数字的乘积来获得num的表示。 If you want uniqueness of factorization, you usually use prime factorization. 如果您想要分解的唯一性,通常使用素数分解。

To get all the divisors your code should be 要获得所有除数,你的代码应该是

for ( int i = 1; i <= num; ++i ) // note that 1 and num are both trivially divisors of num
{
    if ( num % i == 0 ) // only check for divisibility
    {
        std::cout << i << std::endl;
    }
}

to get the (prime) factorization, it's 获得(素数)因子分解,它是

for ( int i = 2; i <= num; ++i )
{
    while ( num % i == 0 ) // check for divisibility
    {
        num /= i;
        std::cout << i << std::endl;
    }
    // at this point, i cannot be a divisor of the (possibly modified) num.
}

The problem is that you're increasing i even if it is a divisor, and you shouldn't unless you find all its occurences. 问题是,即使它是一个除数,你也在增加i ,除非你发现它的所有出现,否则你不应该这样做。

So, for 4, you'd have 2 twice. 所以,对于4,你有两次。 But after the first 2 you encounter, you exit the loop because i is incremented to 3 and num became 2. 但是在你遇到的前两个之后,你退出循环,因为i增加到3而num变为2。

The following should work: 以下应该有效:

for(int i = 2; i <= num; )
{
    if(num%i == 0)
    {
        cout << i << endl;
        cout << num << endl;
        num = num/i;    
    }
    else
    {
        i++;
    }
}
for(int i = 2; i <= num; i++)
{
    if(num%i == 0)
    {
        cout << i << endl;
        cout << num << endl;
        num = num/i;    
        i--; // Add this to account for multiple divisors
    }
}

for(int i = 2; i <= num; i++)
{
    if(num%i == 0)
    {
        cout << i << endl;
        cout << num << endl;
    }
}

This should work. 这应该工作。 Note, should use c++11 for move constructor, otherwise you are going to want to pass in a std::list& instead. 注意,应该使用c ++ 11作为移动构造函数,否则你将要传入一个std :: list&。

std::list<int64_t> factor(int64_t f)
{
    std::list<int64_t> factors;
    for(int64_t ii = 2; ii<=f; ii++) {
        while(f % ii == 0) {
            f = f/ii;
            factors.push_back(ii);
        }
    }

    return factors;
}

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

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