Advertisement
yuawn

algo2017_week1_prime_factor

Oct 3rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.     int x;
  6.    
  7.     scanf("%d",&x);
  8.     printf("%d=",x);
  9.    
  10.     bool print = 0;
  11.     int num;
  12.     int limit = static_cast<int>(sqrt(x));
  13.    
  14.     for( int i = 2; i <= limit; i++  ){
  15.         num = 0;
  16.         while( x % i == 0 ){
  17.             num++;
  18.             x /= i;
  19.         }
  20.         if( num )
  21.             if( print ){
  22.                 printf( "*%d", i );
  23.                 if( num != 1 ) printf( "^%d", num );
  24.             }
  25.             else{
  26.                 print = 1;
  27.                 printf( "%d", i );
  28.                 if( num != 1 ) printf( "^%d", num );
  29.             }
  30.     }
  31.    
  32.     if( x != 1 )
  33.         if( print )
  34.             printf( "*%d", x );
  35.         else
  36.             printf( "%d", x );
  37.            
  38.     puts("");
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement