AWK

This program is really an abuse of the AWK language. AWK was designed as a tool for manipulating text files not as a general purpose programming language. An AWK script consists of a set of pattern/action pairs. A script is supposed to be run against a text file. The patterns are used to select text, and the actions are used to manipulate this text. This script will run on Linux under the GAWK interpreter, but you must run it in the following way.

Save the file as SieveE.awk.
execute the Linux command:
awk -f SieveE.awk
type CTRL-D

Because an extra input file is always required, and you haven't supplied one, the AWK command assumes that the input file is coming from stdin. The CTRL-D keystroke is an End-Of-File and will terminate the reading of stdin. The END keyword in the script will match the End-of-file and cause the script to be executed.

AWK code is essentially C with additions for selecting text. Note, however that AWK does not use semicolons or declarations. Array elements are created by the assignments in the first for loop.

Those who take time to learn AWK generally find it quite useful.


END { for (i=0 ; i<1000 ; i++)


{


Candidates[i] = 1


}


Candidates[0] = 0


Candidates[1] = 0


i = 0


while (i<1000)


{


while (i<1000 && Candidates[i] == 0)


{

i++


}


if (i<1000)


{


for (j=2 ; j*i < 1000 ; j++)


{

Candidates[j*i] = 0


}


i++


}


}


for (i=0 ; i<1000 ; i++)


{


if (Candidates[i] != 0)


{

print i," is prime"


}


}


}

Click Here for the actual code.