/*
C      int nextprime(int n) ! Return next prime number >= n
C
C     Return next prime number .GE. N
C
C     For N .GE. 1,018,801 returns next number relatively prime to all
C     primes below 1009.
C     Note: 1009 is next prime after 997, and 1,018,801 = 1009^2.
C           The database may be extended if larger primes are desired.
C
C     Outline:
C         1.  If number .LE. 3 then return number.
C             Number is prime, zero, or negative.
C         2.  If number between 4 and 997 then search PRIME array
C             for next prime .GE. number.  An approximating polynomial
C             is used to give us a good initial guess for the index I
C             so that PRIME(I) is very close to the prime we seek.
C             The approximating polynomial is:
C
C                 I = -0.3034277E-04*X^2 + 0.1918667*X + 8.0918350
C
C              The optimum coefficients for this polynomial were obtained
C              using the OPTIMIZE program in the [.OPTIMIZE] directory.
C              Look there for further details.  Thus, this function is right
C              on the money 452 times, is low by one 224 times, is low by two
C              40 times, and is low by three 1 time.  It's high by one 185
C              times, high by two 68 times, high by three 12 times, high by
C              four 7 times, high by five 4 times, and high by 6 one time.
C
C         3.  Else number is greater than 997.  Do normal search for next prime.
C             3a.  Increment NUMBER to an odd number.  Since even numbers are
C                  not prime we skip over even numbers.
C             3b.  Test if NUMBER is evenly divisible by any prime in the
C                  PRIME array less than SQRT(NUMBER).
C                  If NUMBER MOD PRIME(I) = 0, then NUMBER is not prime.
*/
#include <stdio.h>
#define NUMPRIMES 168
#define MIN(a,b)  ( ((a) > (b)) ? (b) : (a) )

int nextprime( n )    /* Return next prime number >= n */
int n;
{
   int i;
   int number;
   static int prime[NUMPRIMES+1] = { 1,
         2,   3,   5,   7,  11,  13,  17,  19,  23,  29,  31,  37,  41,
        43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97, 101,
       103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
       173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
       241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
       317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
       401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
       479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569,
       571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
       647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
       739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
       827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
       919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 };
   number = n;
   if (number <= 3) return(number);  /* if number .le. 3 then it's prime */
   if (number <= 997)      /* just search through PRIME array */
   {
      i = (-0.3034277e-04*number + 0.1918667)*number + 8.0918350;	/*(see comments above)*/
      i = MIN(i,NUMPRIMES);			/* don't let I go above 997*/
      while (prime[i] < number) ++i; /* Search upward for first prime greater than NUMBER */
      while (prime[i] >= number) --i;  /* Search downward for first prime less than NUMBER, */
      return( prime[i+1] );		/* then take next prime. */
   }
   else					/* Else normal search for next prime */
   {
      if ((number % 2) == 0) ++number;	/* rule out even numbers.  They are not prime. */
      i = 2;				/* start with second prime since we only test odd numbers. */
      while (prime[i]*prime[i] <= number)  /* test for all primes .LE. SQRT(NUMBER) */
      {
         if ( (number % prime[i]) == 0 )	/* it's not prime */
         {
            number += 2;			/* even numbers aren't prime */
               i = 2;				/* start over again with new number */
         }
         else
         {
            ++i;				 /* test with next prime */
            if ( i > NUMPRIMES) return(number);   /* NUMBER is relatively prime to first 168 primes. */
         }
      }
      return(number);
   }
}

main()
{
   int n1, ians;
   for(;;)
   {
      printf("\nEnter a number: ");
      scanf("%d", &n1);
      ians = nextprime( n1 );
      printf("%d\n",ians);
   }
}

Back