Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

prime1

By: cmiN on Mar 6th, 2011  |  syntax: C++  |  size: 1.06 KB  |  views: 232  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <cstdio>
  2. #include <cmath>
  3.  
  4. unsigned int numbers[3500], len;
  5.  
  6. inline bool prime(unsigned int x)
  7. {
  8.     unsigned int i, last = sqrt(x);
  9.     for (i = 2; i <= last; i++) {
  10.         if (!(x % i)) {
  11.             return 0;
  12.         }
  13.     }
  14.     return 1;
  15. }
  16.  
  17. void generate()
  18. {
  19.     for (unsigned int i = 2; i < 32000; i++) {
  20.         if (prime(i)) {
  21.             numbers[len++] = i;
  22.         }
  23.     }
  24. }
  25.  
  26. inline bool process(unsigned long x)
  27. {
  28.     unsigned int i, last = sqrt(x);
  29.     for (i = 0; i < len && numbers[i] <= last; i++) {
  30.         if (!(x % numbers[i])) {
  31.             return 0;
  32.         }
  33.     }
  34.     return 1;
  35. }
  36.  
  37. int main()
  38. {
  39.     int tests;
  40.     unsigned long begin, end;
  41.     generate();
  42.     scanf("%d", &tests);
  43.     while (tests-- > 0) {
  44.         scanf("%u %u", &begin, &end);
  45.         if (begin == 1) {
  46.             begin++;
  47.         }
  48.         while (begin <= end) {
  49.             if (process(begin)) {
  50.                 printf("%u\n", begin);
  51.             }
  52.             begin++;
  53.         }
  54.         printf("\n");
  55.     }
  56.     return 0;
  57. }