【问题标题】:Output powers of prime factors of a factorial阶乘素数的输出功率
【发布时间】:2014-09-13 14:49:44
【问题描述】:

我正在尝试为我的班级编写代码。该程序必须让用户输入一个数字,它将输出其阶乘的每个素因子的指数。例如,当用户输入数字 5 时,输出将是 3 1 1 (2^3, 3^1, 5^1)。到目前为止,我有代码来获取阶乘的主要因素。但我无法得到指数。

我的代码如下:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

int main()
{
int number, factor, exp, product, x, factorial=1;

cout <<"PRIME FACTORIALS" <<endl;
cout <<" " <<endl;
cout <<"Welcome! This program allows users to find the prime factors of a number and its exponents or how many times each prime factor is multiplied." <<endl;
cout <<" " <<endl;
cout <<"To begin, please input a positive integer below:" <<endl;
cin >>number;

if ((number<1) || (number>100))
{
    cout <<"You have entered a number that is out of range. Please enter a number from 1-100." <<endl;
    system("PAUSE");
    system("cls");
    main();
}

else
{
    for (x=1; x<=number; x++)
    {
        factorial=factorial*x;

        for (factor=2; factor<=factorial; factor++)
        {
            while (factorial%factor==0)
            {
                factorial/=factor;
                cout <<factor <<" ";
            }
        }
    }
}
}

【问题讨论】:

  • 是的,我已经解决了这个问题。知道如何输出质因数的指数吗?
  • 计算每个因素除法成功的次数。 (尽管您目前正在尝试所有可能的因素,而不仅仅是主要因素。)

标签: c++ prime-factoring exponent


【解决方案1】:

您不应该在计算出因子之前完成因子的计算吗? IE。在factorial = factorial * x; 之后有}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 2014-02-07
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    相关资源
    最近更新 更多