Advertisement
B1KMusic

Composite Number Program #1

Jul 6th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4.  
  5. #define PERIOD 1000
  6.  
  7. void
  8. wait()
  9. {/*{{{*/
  10.     usleep(PERIOD);
  11. }/*}}}*/
  12.  
  13. int
  14. factor(int n)
  15. {/*{{{*/
  16.     int i = 1,
  17.         factors = 0;
  18.  
  19.     for(i = 1; i <= n; i++)
  20.         if(n % i == 0)
  21.             factors++;
  22.  
  23.     return factors;
  24. }/*}}}*/
  25.  
  26. int
  27. main(int argc, char **argv)
  28. {/*{{{*/
  29.     int i = 0,
  30.         factors = 0,
  31.         record = 0,
  32.         quiet = 0;
  33.  
  34.     if(argv[1]){
  35.         if(!strcmp(argv[1], "-q")){
  36.             quiet = 1;
  37.         }
  38.     }
  39.  
  40.     while(++i){
  41.         factors = factor(i);
  42.  
  43.         if(!quiet)
  44.             printf("%i: %i", i, factors);
  45.  
  46.         if(factors > record){
  47.             record = factors;
  48.  
  49.             if(!quiet)
  50.                 printf(" NEW RECORD!");
  51.             else
  52.                 printf("%i: %i\n", i, factors);
  53.         }
  54.  
  55.         if(!quiet){
  56.             printf("\n");
  57.             wait();
  58.         }
  59.     }
  60. }/*}}}*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement