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:
1 |
97532468 |
Sample Output:
1 |
97532468=2^2*11*17*101*1291 |
测试点3,n=1时,输出结果为”1=1″
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#include<stdio.h> #include<math.h> int is_prime(long long n) { long long i,end; end=sqrt((double)n); if(n<2) { return 0; } for(i=2;i<=end;i++) { if(n%i==0) { return 0; } } return 1; } int main() { long long i,n,temp,factor[64],offset=0,count; scanf("%lld",&n); temp=n; for(i=2;i<=temp;i+=2) { if(is_prime(i)&&temp%i==0) { factor[offset++]=i; temp/=i; i=i-2; } if(i==2) { i=1; } } if(n<2) { printf("%lld=%lld\n",n,n);//测试点3,n=1时,输出结果为"1=1" return 0; } printf("%lld=%lld",n,factor[0]); count=1; for(i=1;i<offset;i++) { if(factor[i]==factor[i-1]) { count++; } else { if(count>1) { printf("^%lld",count); } printf("*%lld",factor[i]); count=1; } } if(count>1) { printf("^%lld",count); } } |