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:97532468Sample Output:
97532468=2^2*11*17*101*1291
備註:簡單題
#include<stdio.h>
#include<math.h>
int IsPrime(long int n)
{
for(long int i=2;i<=sqrt((double)n);i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main()
{
long int n;
scanf("%ld",&n);
if(IsPrime(n))
{
printf("%ld=%ld",n,n);
return 0;
}
printf("%ld=",n);
for(long int i=2;i<=n;i++)
{
if(IsPrime(i))
{
int exp = 0;
while(n%i==0)
{
n=n/i;
exp++;
}
if(exp==0)
continue;
else if(exp==1)
{
if(n==1)
{
printf("%ld",i);
break;
}
else
printf("%ld*",i);
}
else
{
if(n==1)
{
printf("%ld^%d",i,exp);
break;
}
else
printf("%ld^%d*",i,exp);
}
}
}
return 0;
}