A1059. 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
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #include <string.h> 5 #include <string> 6 #include <math.h> 7 #include <algorithm> 8 using namespace std; 9 10 11 const int maxn=100000; 12 int prime[maxn],pnum=0; 13 bool p[maxn]={0}; 14 void Find_Prime(){ 15 for(int i=2;i<maxn;i++) 16 { 17 if(p[i]==false) 18 { 19 prime[pnum++]=i; 20 for(int j=i+i;j<maxn;j+=i) 21 { 22 p[j]=true; 23 } 24 } 25 } 26 } 27 28 struct factor{ 29 int x,cnt; 30 }fac[10]; 31 int main(){ 32 Find_Prime(); 33 int n,num=0; 34 scanf("%d",&n); 35 if(n==1){ 36 printf("1=1"); 37 return 0; 38 }else{ 39 printf("%d=",n); 40 } 41 42 43 int sqr=(int)sqrt(1.0*n); 44 int count=0; 45 for(int i=2;i<=sqr;) 46 { 47 if(n%i==0) 48 { 49 fac[num].x=i; 50 fac[num].cnt=0; 51 while(n%i==0) 52 { 53 fac[num].cnt++; 54 n/=i; 55 } 56 num++; 57 } 58 59 count++; 60 i=prime[count]; 61 } 62 if(n>sqr) 63 { 64 fac[num].x=n; 65 fac[num++].cnt=1; 66 } 67 for(int i=0;i<num;i++) 68 { 69 if(i>0)printf("*"); 70 printf("%d",fac[i].x); 71 if(fac[i].cnt>1) 72 { 73 printf("^%d",fac[i].cnt); 74 } 75 } 76 return 0; 77 }