#ifndef lint static char *rcsid = "$Header: /tmp_mnt/vida/disks/disk5/Users/terry/r/gassy/RCS/next_prime.c,v 1.3 1992/08/09 22:07:40 terry Exp terry $"; #endif #include "types.h" #include #define ERROR -1 #define NEITHER 0 #define COMPOSITE 1 #define PRIME 2 INT prime(n) LONG n; { extern DOUBLE sqrt(); register LONG lim; register LONG i; if (n < 0){ return ERROR; } if (n == 0L || n == 1L){ return NEITHER; } if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0){ return COMPOSITE; } lim = 1 + (LONG)sqrt((DOUBLE)n); for (i = 7; i <= lim; i += 2){ if (n % i == 0){ return COMPOSITE; } } return PRIME; } INT next_prime(n) INT n; { if (n < (INT) 0){ error("Next prime called with negative (%d) argument.", n); } if (n % 2 == 0){ n++; } while (prime(n) != PRIME){ n += 2; } return n; }