Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pm^km.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N = p1k1p2k2…*pm^km, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi – hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
题意
给出⼀个整数,按照从⼩到⼤的顺序输出其分解为质因数的乘法算式
分析
先用埃氏筛选法建⽴个10000以内的素数表,然后从2开始⼀直判断是否为它的素数,如果是就将x=x/i继续判断i是否为x的素数,判断完成后输出这个素数因⼦和个数
//1059 Prime Factors (25分)
#include <iostream>
#include <cmath>
using namespace std;
struct factor
{
int f,cnt;
} fac[100];
const int maxn=10000;
int prime[maxn],pNum=0;
bool p[maxn]= {false};
int num=0;
void find_Prime()
{
for(int i=2; i<maxn; i++)
{
if(p[i]==false)
{
prime[pNum++]=i;
for(int j=i*i; j<maxn; j+=i)
p[j]=true;
}
}
}
void prime_factor(long int x)
{
for(int i=0; i<sqrt(x); i++)
{
if(x%prime[i]==0)
{
fac[num].f=prime[i];
fac[num].cnt=0;
while(x%prime[i]==0)
{
fac[num].cnt++;
x/=prime[i];
}
num++;
}
}
if(x!=1)
{
fac[num].f=x;
fac[num++].cnt=1;
}
}
int main()
{
long int n;
cin>>n;
find_Prime();
prime_factor(n);
cout<<n<<"=";
if(n==1)
cout<<1;
for(int i=0; i<num; i++)
{
if(i>0)
cout<<"*";
cout<<fac[i].f;
if(fac[i].cnt>1)
cout<<"^"<<fac[i].cnt;
}
return 0;
}