首页 文章

C将一个数字分解为素数因子

提问于
浏览
2

我编写了一个程序,将数字计入其主要因子,然后将它们存储在一个向量中,最后询问是否通过将它们相乘来验证结果 .

它以这种方式工作:请求一个数字(代码中的 num ),并将其除以2和更高 .

如果它找到一个数字(代码中的 divisor ),其模数(当 num mod divisor )为零时,将该除数存储到向量中,并将 num 除以 divisor 并将其存储到 temp 中,并将除数重置为1(并且 while 循环中的最后一个语句将其增加到2.如果未找到此数字, divisor 将增加,直到它大于或等于 num . 此过程将继续,直到 divisor 大于 num .

这是代码:

#include <iostream>
#include <vector>

using namespace std;

int main() {

    //num=the number of interest
    //divisor=the number dividing the number of interest each time
    unsigned long  divisor=2, num, temp ; //num=13699293826d
    char c;

    vector<unsigned long> divisors;
    cout<<"Enter a number: "<<endl;
    cin>>num;

    //temp stores the number that is reduced each time
    temp=num;

    while(divisor<=num)
    {
        if(temp%divisor==0)
        {
             temp=temp/divisor;
             divisors.push_back(divisor);
             cout<<"one "<<divisor<<endl;
             cout<<"the number of interest is now"<<temp<<endl;
             divisor=1;
        }
        if(divisor==temp&&temp!=1)
        {
            cout<<"two " << divisor<<endl;
            divisors.push_back(divisor);
        }

        divisor++;
    }

    if(divisors[0]==num)
    {
        cout<<"The number: "<<num<<" is prime. ";
    }
    else
    {
        cout<<"Its proper divisors are: ";
        for(unsigned int count=0; count<divisors.size(); count++ )
        {
            cout<<divisors[count]<<"\t";
        }
    }

    cout<<"Print out the multiplication? Press 'Y' or 'N'."<<endl;
    cin>>c;

    if(c=='Y'||c=='y')
    {
        for(unsigned int count=0; count<divisors.size(); count++)
        {
            temp*=divisors[count];
            cout<<temp<<"\t";
        }
    }
    return 0;
}

我打印了一些debug cout 语句 .

我遇到的问题是:当数字足够大时,调试语句“感兴趣的数量现在是”,其后面有数字1 . 然后,程序崩溃了 .

代码有什么问题?

谢谢 .


是的,我在64位上运行它 .

示例程序输出:

Enter a number: 
    13699293826
    one 3
    the number of interest is now: 1431655765
    one 5
    the number of interest is now: 286331153
    one 17
    the number of interest is now: 16843009
    one 257
    the number of interest is now: 65537
    one 65537
    the number of interest is now: 1

然后程序崩溃了 .

我也注意到3的第一个“素因子”是不正确的,因为13699293826除以3是4562761275.333333333333333 .....

编辑#2 ------------------------------------------

temp 65537, divisor 62287

    ..............omitted output

    temp 65537, divisor 65530
    temp 65537, divisor 65531
    temp 65537, divisor 65532
    temp 65537, divisor 65533
    temp 65537, divisor 65534
    temp 65537, divisor 65535
    temp 65537, divisor 65536
    temp 65537, divisor 65537
    one 65537
    the number of interest is now: 1
    Its proper divisors are: 3  5   17  257 65537   Print out the                 multiplication? Press 'Y' or 'N'.

然后程序停止响应,当我按“y”并输入时它不起作用 .

而且,乘以的数字不正确;结果是4294967295 ...在谷歌搜索后,它说它是“你可以使用32位(BInary digiTS)获得的最高数字” . 但在我的电脑上,它说操作系统是64位 .

1 回答

  • 2

    当您收到消息"the number of interest is now 1"时,表示 temp == 1 现在 . 你应该已经停止了,但是你继续,因为你的循环错误地将 divisornum 进行了比较,而它应该将它与 temp 进行比较 .

    所以现在 temp == 1divisor == 2 ,你将循环直到 unsigned long divisor 回绕到0.此时你的检查 if(temp%divisor==0) 导致除零 . 我希望任何输入都会发生这种情况 .


    您不应重置 divisor ,并且您的循环条件错误 . 你的循环应该是这样的:

    while( divisor*divisor <= temp)
    {
        if(temp%divisor==0)
        {
             temp=temp/divisor;
             divisors.push_back(divisor);
             cout<<"one "<<divisor<<endl;
             cout<<"the number of interest is now"<<temp<<endl;
             ///// divisor=1;
        }
        /* ------ if(divisor==temp&&temp!=1)
        {
            cout<<"two " << divisor<<endl;
            divisors.push_back(divisor);
        } ------- */
        else ////////
            divisor++;
    }
    if( temp > 1)
    {
        divisors.push_back( temp );
        temp = 1;  // <<-------------- ADD THIS
    }
    

相关问题