Computer Programming Contest Preparation

ToolBox - Source for: 114/11417/t.c



/home/toolbox/public_html/solutions/114/11417/t.c
    1 int mat[501][100]
    2 int primes[100] = { 0, 1,
    3                     2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
    4                     101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
    5                     211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
    6                     307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
    7                     401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499
    8                   };
    9 
   10 
   11 void factorAll()
   12 {
   13     /* FUNCTION factorAll */
   14     int i;
   15     int j;
   16     int t;
   17     int next;
   18 
   19     /* empty out matrix that represents prime factors of a number */
   20     for (i=0; 500>=i; i++)
   21         {
   22             /* for each number */
   23             mat[i][1] = 1;
   24             for (j=2; 96>j; j++)
   25                 {
   26                     /* for each possible prime factor */
   27                     mat[i][j] = 0;
   28                 } /* for each possible prime factor */
   29         } /* for each number */
   30     /* preload primes */
   31     for (j=2; 96>j; j++)
   32         {
   33             /* for each possible prime factor */
   34             mat[j][primes[j]] = 1;
   35         } /* for each possible prime factor */
   36     /* factor each number */
   37     for (i=0; 500>=i; i++)
   38         {
   39             /* for each number */
   40             if (0 == mat[i][i])
   41                 {
   42                     /* not prime, so factor */
   43                     t = i;
   44                     next = 2;
   45                     while (1 < t)
   46                         {
   47                             /* loop as long as number is greater than 1 */
   48                             if (1 == mat[t][t])
   49                                 {
   50                                     /* t is prime */
   51                                     mat[i][t]++;
   52                                     t = 1;
   53                                 } /* t is prime */
   54                             else
   55                                 {
   56                                     /* find factor */
   57                                     if (0 == (t % primes[next]))
   58                                         {
   59                                             /* found a factor */
   60                                             mat[i][primes[next]]++;
   61                                             t = t / primes[next];
   62                                         } /* found a factor */
   63                                     else
   64                                         {
   65                                             /* not a factor - go to mext */
   66                                             next++;
   67                                         } /* not a factor - go to mext */
   68                                 } /* find factor */
   69                         } /* loop as long as number is greater than 1 */
   70                 } /* not prime, so factor */
   71         } /* for each number */
   72 } /* FUNCTION factorAll */
   73