Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /*This program will generate a list of prime numbers up to UPPERLIMIT*/
  6. int IsPrime(int);
  7.  
  8. #define UPPERLIMIT 2000000
  9.  
  10. FILE *file;
  11. char *filename = "C:\\Users\\chris\\Documents\\primelist.txt";
  12.  
  13. int main()
  14. {
  15.     int x;
  16.     unsigned long long total = 2;
  17.  
  18.     printf("Opening file %s\n", filename);
  19.  
  20.     file = fopen(filename,"a");
  21.  
  22.     if (file == NULL)
  23.     {
  24.         printf("File could not be open\n");
  25.         return 0;
  26.     }
  27.  
  28.     printf("Generating list of primes to %s\n", filename);
  29.     /*fprintf(file, "---Starting Generation---\n");*/
  30.     printf("2\n");
  31.     fprintf(file, "%i\n", 2);
  32.     for (x = 3; x <= UPPERLIMIT; x+=2)
  33.     {
  34.         if (IsPrime(x))
  35.         {
  36.             printf("%i\n", x);
  37.             total = total + x;
  38.             fprintf(file, "%i\n", x);
  39.         }
  40.     }
  41.  
  42.     printf("%llu\n", total);
  43.     fclose(file);
  44.     return 0;
  45. }
  46.  
  47. int IsPrime(int x)
  48. {
  49.     int index;
  50.  
  51.     if (x % 2 == 0) {return 0;}
  52.  
  53.     for (index = 3; index < x; index++)
  54.         {
  55.             if ((x % index) == 0)
  56.             {
  57.                 return 0;
  58.             }
  59.         }
  60.     return 1;
  61. }