阶乘的素因子的输出功率

时间:2014-09-13 14:49:44

标签: c++ prime-factoring exponent

我正在尝试为我的班级编写代码。程序必须让用户输入一个数字,它将输出其阶乘的每个素因子的指数。例如,当用户输入数字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 <<" ";
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

在计算因子之前,你不应该完成计算阶乘吗?即在}之后有一个factorial = factorial * x;