/******************************
作者:cncoderalex
博客:http://blog.csdn.net/cncoderalex
*******************************/
#include <iostream>
#include <memory>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
bool Mark[1000];
int Primes[1000];
int PrimeSize;
int PrimeFactorExp[1000];
void CalcPrimes()
{
PrimeSize = 0;
for (int i = 2; i < 1000; i++)
{
if (Mark[i])
continue;
Primes[PrimeSize++] = i;
for (int j = i * i; j < 1000; j += i)
{
Mark[j] = true;
}
}
}
void DecompositePrimeFactor(int n)
{
for (int i = 0; i < PrimeSize; i++)
{
int t = n;
while (t)
{
t /= Primes[i];
PrimeFactorExp[i] += t;
}
}
}
void PrintAns()
{
bool bPrinted = false;
for (int i = 0; i < PrimeSize; i++)
{
if (PrimeFactorExp[i] > 0)
{
if (bPrinted)
{
printf(" * %d^%d", Primes[i], PrimeFactorExp[i]);
}
else
{
printf("%d^%d", Primes[i], PrimeFactorExp[i]);
bPrinted = true;
}
}
}
printf("\n");
}
void PrintPrimes()
{
for (int i = 0; i < PrimeSize; i++)
{
if (i % 10 == 0)
printf("\n");
printf("%d ", Primes[i]);
}
printf("\n\n");
}
int main()
{
printf("http://blog.csdn.net/cncoderalex");
printf("\n");
CalcPrimes();
PrintPrimes();
DecompositePrimeFactor(10);
PrintAns();
system("pause");
return 0;
}
n阶乘的素数分解
最新推荐文章于 2022-07-27 23:44:45 发布