Advertisement
robonx12

Factor

Apr 1st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. typedef  int BitArray_t[];
  6.  
  7. #define int_size sizeof(int)
  8.  
  9. #define size(x) x[0] * bits
  10.  
  11. #define bits int_size * 8
  12.  
  13. #define div(pos) pos / (bits)
  14.  
  15. #define mod(pos) pos % (bits)
  16.  
  17. #define create(x, amount)   int b_array_size = amount / (bits);\
  18.                                 if (amount % (bits)) b_array_size++;\
  19.                                 int x[b_array_size + 1];\
  20.                                 for(int i = 0; i <= b_array_size; i++) x[i] = 0;\
  21.                                 x[0] = b_array_size
  22.                            
  23. #define GET_BIT_(x, position) (((x[div(position) + 1]) & ((int)1 << mod(position))) != 0)
  24.  
  25.                
  26. #define SET_BIT_(x, position, val) if (val) (x[div(position) + 1] |= ((int)1 << (mod(position))));\
  27.                                        else (x[div(position) + 1] &= ~((int)1 << (mod(position))))
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.  
  32.     int c = atoi(argv[1]);
  33.     const int SC = (int)sqrt(c);
  34.     create(b_array, c);
  35.  
  36.     for (int i = 2; i < SC; i++)
  37.     {
  38.         if (!(GET_BIT_(b_array,i)))
  39.         {
  40.             for (int j = 2; j * i < size(b_array); j++)
  41.             {
  42.                 SET_BIT_(b_array, j * i, 1);
  43.             }
  44.         }
  45.     }
  46.  
  47.     int i = 2;
  48.    
  49.     if (c)
  50.     {
  51.         while (c != 1)
  52.         {
  53.             if ((!(GET_BIT_(b_array, i))) && !(c % i))
  54.             {
  55.                 printf("%d ", i);
  56.                 c /=i;
  57.             }
  58.             else
  59.                 i++;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement