Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
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 <vector>
#include <math.h>
#include <algorithm>
#include <map>
#include <sstream>
#include <string.h>
using namespace std;
typedef long long ll;
struct l
{
ll prime=0;
ll num=0;
} ;
vector<l> primes;
bool check(ll n)
{
if(n==1)
return false;
if(n==2||n==3)
return true;
for(int i=2; i<=sqrt(n); i++)
{
if(n%i==0)
return false;
}
return true;
}
int main()
{
ll n,m;
cin>>n;
m=n;
for(ll i=2; i<=sqrt(n); i++)
{
if(check(i)==false)
continue;
if(n%i==0)
{
l ls;
ls.prime=i;
ls.num=1;
n/=i;
while(n%i==0)
{
ls.num++;
n/=i;
}
primes.push_back(ls);
}
}
if(n!=1||m==1)
{
l ls;
ls.prime=n;
ls.num=1;
primes.push_back(ls);
}
cout<<m<<"=";
for(ll i=0;i<primes.size();i++)
{
cout<<primes[i].prime;
if(primes[i].num>1)
cout<<"^"<<primes[i].num;
if(i!=primes.size()-1)
{
cout<<"*";
}
}
return 0;
}