当前位置: 首页 > 知识库问答 >
问题:

将一个数字分解为质因数

翟永春
2023-03-14

我写了一个程序,将数字分解为它的质因数,然后将它们存储在一个向量中,最后询问是否通过将它们相乘来验证结果。

它的工作方式是这样的:要求一个数字(代码中的num),然后将其除以2及以上。

如果它找到一个模(当nummod除数时)为零的数字(代码中的 ),则将该除数存储到一个向量中,并将其除以 ,然后将其存储到 中,然后将除数重置为1(而 循环中的最后一条语句将其递增为2)。如果没有找到这样的数字, 除数将增加,直到它大于或等于 num。此过程将一直持续到除数大于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;
}

我打印了一些debugcout语句。

我遇到的问题是:当数字足够大时,调试语句“感兴趣的数量现在是”,后面有数字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.3333333333333333.....

编辑#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位(二进制数字)可以获得的最高数字”。但在我的电脑上,它说操作系统是64位的。


共有1个答案

杜嘉慕
2023-03-14
匿名用户

当您收到消息“感兴趣的数量现在为1”时,这意味着temp==1now。您应该在这一点上停止,但您继续,因为您的循环错误地将除数num进行比较,而它应该将其与temp进行比较。

所以现在< code>temp == 1和< code>divisor == 2,你将循环直到< code >无符号长除数绕回0。此时,您的检查< code>if(temp%divisor==0)会导致被零除。我认为任何输入都会发生这种情况。

您不应该重置除,并且您的循环条件是错误的。您的循环应如下所示:

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
}

 类似资料:
  • 我想把一个数分解成一个大小尽可能接近的数元组,其乘积就是初始数。输入是我们想要的因子数和所需因子数。 对于双因子情况(),寻找小于平方根的最大因子就足够了,所以我可以做这样的事情 所以用调用它将导致。 我意识到,这些数字“在大小上彼此接近”意味着什么,存在一些模糊性。我不介意这被解释为最小化∑(x\u I-x\u avg)或∑(x\u I-x\u avg)^2或其他类似的东西。 对于m==3的情况

  • 使用椭圆曲线分解(在Python中),我能够在~0.5秒内找到50位数字的PRIME因子。有什么方法可以将质因数转换为数字的因子吗? 通过对小数位(496和28)进行测试,将质因数按特定顺序相乘,我实现了什么。然后,将这些数字相乘,几乎就能得到因数,但这并不太灵活,因为我只从一个小的质因数列表(1,2,3,5)中得到需要相乘的公式。

  • 我在python中工作,试图将数字1800分解成三个随机因子。我并不真正关心输出的格式。我可以使用一个元组,一个列表,甚至三个独立的变量数。我也不关心让所有的因素都得到同样的重视。我的输出可能是2,2450或12,15,10或其他任何东西。 我最好的想法是使用素数分解(2,2,2,3,3,5,5),创建三个随机子集,并将每个子集中的项相乘。我研究了多种方法,它们似乎都有一些问题。 来自scikit

  • 问题如下:给定两个数字n和k。对于区间[1, n]中的每个数字,您的任务是计算其不可被k整除的最大除数。打印所有这些除数的和。注意:k始终是质数。t=3*10^5,1 我解决这个问题的方法是:对于1到n范围内的每个i,所需的除数是i本身,只有当i不是k的倍数时。如果i是k的倍数,那么我们必须找到一个数字的最大除数并与k匹配。如果不匹配,那么这个除数就是我的答案。否则,第二大除数就是我的答案。 例如

  • 本文向大家介绍java编程实现求质数与因式分解代码分享,包括了java编程实现求质数与因式分解代码分享的使用技巧和注意事项,需要的朋友参考一下 1、求解质数 1.1说明 首先,我们来了解这样一个概念,那就是什么叫做质数?质数:一个数如果只能被1和它自己整除,这样的数被称为质数,与之对应的,称为和数。基于这样的一个概念,我们可以很快想到一个方法,就是从1开始,不断试探,看从1到它自己,是否有数字能够

  • 问题内容: 我试图将一个Int拆分为其各个数字,例如3489到3 4 8 9,然后我要将这些数字放入一个Int数组中。 我已经尝试过将数字放入字符串中,然后遍历每个数字,但是它不起作用: 有任何想法吗? 问题答案: 我们还可以扩展StringProtocol并创建一个计算属性: 编辑/更新: Xcode 11•Swift 5.1 在 Swift 5中, 我们现在可以使用新属性