1059. Prime Factors (25)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*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 = p1^k1 * p2^k2 *…*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
#include"iostream"
#include "algorithm"
#include "math.h"
#include<stdlib.h>
using namespace std;
long int n;
bool IsPrimer(long n)
{
long i,tmp;
tmp = sqrt(double(n))+1;
for(int i=3;i<=tmp;i++)
if(n%i==0)
return false;
return true;
}
int main()
{
cin >> n;
cout<<n<<"=";
if(n==1)
cout<<n<<endl;
else
{
int k;
int tmp = n;
for(int i=2;i<=tmp;i++)
{
k=0;
if(IsPrimer(i))
{
while(n%i==0)
{
k++;
n=n/i;
}
}
if(k>1)
{
cout<<i<<"^"<<k;
}
else if(k==1)
cout<<i;
if(n!=1&&k>=1)
cout<<"*";
else if(n==1)
break;
}
}
cout<<endl;
return 0;
}