#include #define MaxPrimes 50 int Prime[MaxPrimes], UpperBound; void CheckPrime ( int K) { int J ; // the plan : see i f J divides K, f o r a l l values J which are // (a) themselves prime (no need to t r y J i f i t i s nonprime ) , and // (b) less than or equal to s q r t (K) ( i f K has a d i v i s o r l a r g e r // than t h i s square root , i t must also have a smaller one , // so no need to check f o r l a r g e r ones) J = 2; // while ( 1 ) { for(J=2; J*J <= K; J++){ if ( Prime [ J ] == 1) if (K % J == 0) { Prime [K] = 0; return ; } J++; } Prime[K]=1; } main() { int N; printf("enter upper bound\n"); scanf("%d", &UpperBound); Prime[2] = 1; for (N = 3; N <= UpperBound; N += 2){ CheckPrime(N); if (Prime[N]) printf("%d is a prime\n",N); } return 0; }