Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdbool.h>  //This is to bring in the define of true
  3.  
  4. #include <math.h>  //This is to bring in the define of sqrt()
  5.  
  6. int main (int argc, const char * argv[]) {
  7.     bool    isPrime;
  8.     int     startingPoint, candidate, last, i;
  9.    
  10.     startingPoint = 100;
  11.    
  12.     if ( startingPoint <= 2 ) {
  13.         candidate = 3;
  14.     }
  15.     else {
  16.         candidate = startingPoint;
  17.         if (candidate % 2 == 0)             // Test only odd numbers
  18.             candidate--;
  19.         do {
  20.             isPrime = true;                 // Assume glorious success
  21.             candidate += 2;                 // Bump to the next number to test
  22.             last = sqrt( candidate );       // We'll check to see if candidate
  23.                                             // has any factors, from 3 to last
  24.                                             // Loop through odd numbers only
  25.             for ( i = 3; (i <= last) && isPrime; i += 2 ) {
  26.                 if ( (candidate % i) == 0 )
  27.                     isPrime = false;
  28.             }
  29.         } while ( ! isPrime );
  30.     }
  31.  
  32.    
  33.    
  34.     printf( "The next prime after %d is %d. Happy?\n",
  35.            startingPoint, candidate );
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement