C   19

primes.c

Guest on 7th May 2021 04:41:37 PM

  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     /* when do we stop? */
  5.     //unsigned long long END = 18446744073709551614;
  6.     unsigned long long END = 100;
  7.  
  8.     /* stage keeps track of how much we should add to get the next number */
  9.     unsigned int stage = 2;
  10.  
  11.     /* current is the value we are checking now */
  12.     unsigned long long current = 11;
  13.  
  14.     /* something to store results in */
  15.     short noPrime;
  16.  
  17.     while(current < END) {
  18.         /* check if we have a prime on our hands */
  19.  
  20.         noPrime = 0;
  21.  
  22.         if( current%3 == 0 ) noPrime = 1;
  23.         else if( current%5 == 0 ) noPrime = 1;
  24.         else if( current%7 == 0 ) noPrime = 1;
  25.  
  26.         if(noPrime == 0) printf("%llu\n", current);
  27.  
  28.         /* skip numbers that can't be primes by anyway */
  29.         if(stage == 3) {
  30.             stage = 0;
  31.             current += 4;
  32.         } else {
  33.             stage++;
  34.             current += 2;
  35.         }
  36.     }
  37.  
  38.     return 0;
  39. }

Raw Paste


Login or Register to edit or fork this paste. It's free.