%! % A PostScript file must start with the two characters "%!", % else the printer will print it literally instead of interpreting % it as PostScript. Since any line beginning with % is a comment % in this language, the first %! line is not itself run as PostScript. /erath{ % define a procedure erath, which when invoked with "x erath" for % some integer x returns a boolean array siv of length x+1 (since % PostScript arrays are indexed from zero) whose n-th element is % "true" if n is prime, "false" otherwise. /x exch def % store the argument in the variable x /siv x 1 add array def 2 1 x { siv exch true put } for 0 1 1 { siv exch false put } for % initialize siv so its 0th and 1st elements are "false", the rest "true" 2 1 x sqrt{ /p exch def siv p get { % loop over primes from 2 to sqrt(x) p p add p x {siv exch false put} for % set the 2p-th, 3p-th,... elements of siv to false }if }for siv} def % wrap up and push siv on the stack % sample application: print all the primes up to 2000 % first select a font and define a `carriage-return' utility "cr" % and a right-justification routine `rightjustify' /Times-Roman findfont 10 scalefont setfont /leftmargin 100 def /pagetop 680 def /pagebottom 80 def /columnwidth 60 def /spacing 13 def /cr1{ leftmargin currentpoint spacing sub exch pop moveto }def /cr{ cr1 currentpoint pagebottom le {/leftmargin leftmargin columnwidth add def leftmargin pagetop moveto} if pop } def /rightjustify {dup stringwidth pop neg 0 rmoveto show} def % run the Sieve on x=2000 and pop siv off the stack 2000 erath pop % move to top of 8.5"x11" page and print heading 230 720 moveto (PRIMES UP TO 2000) show leftmargin pagetop moveto % create a character string "str" to hold the 4-digit primes /str ( ) def % and show them ("cvs" = convert to string) 2 1 2000 { dup siv exch get {str cvs rightjustify cr} {pop} ifelse} for % finally print out the page showpage