PL/1

PL/1 is another one of those languages that every "right-thinking" computer scientist is supposed to hate. This time with good cause.

PL/1 was supposed to be a universal language that incorporated all the "best" features of FORTRAN, COBOL, and ALGOL. Apparently the determination of what was best was done by someone not in complete possession of their senses because the language resembles a junkyard. It even has FORTRAN FORMAT statements for crying out loud! I'm surprised it doesn't have three-way computed branches too!

In addition to preserving the crud of everything that went before it, it includes a lot of new crud of its own. Look at the variable declarations. You have to specify the number of bits in your integer variables! You can put anything you want in the declaration, but ony 31 and 15 will actually be used. It doesn't show in the code below, but the first character of every line is a blank. Why? because the first character of every line is treated as a comment! (I think you might be able to put line-printer carriage control characters there to format your listing.)

This program compiles and runs on the IBM Visual Age PL/1 compiler. This compiler is not free, but you can try it free for 30 days. (I don't remember where I got this complier, and I'd prefer not to link to non-free products anyway.)

If you would like to play with PL/1 for free, I suggest you get a copy of the Hercules 360/370/390 emulator and the turnkey MVS system. This system includes a PL/1 compiler. If you then suddenly decide that you love this language so much that you want to pay a fortune for a real complier, then feel free to do so.

This program will run on the Hercules PL/1 compiler, but there are a couple of things you need to be careful with. First, MVS PL/1 is intollerant of lower case letters, so you will have to convert everything to upper case. Second, some of the comments in the example are too long, so the terminating */ ends up in columns 73-80 or disappears entirely. This will cause the comment to invisibly eat a chunk of the code. You must shorten all comments so they end before column 72.

The canned JCL for PL1 (the PL1LFCLG procedure) is incorrect on the turnkey MVS system, so you must add a JOBLIB DD statement to get it to work. The statement is:

//JOBLIB DD DSN=SYS1.PL1LIB,DISP=SHR

The JOBLIB "card" comes right after the JOB card. This bug is not the fault of the Turnkey people, it was in MVS all along.


SieveE: PROCEDURE OPTIONS (MAIN);



/* ///////////////////////////////////////////////////////// */


/* / Name: Peter M. Maurer */


/* / Program: Sieve of Eratosthenes */


/* / Due: Never */


/* / Language: PL/1 */


/* ///////////////////////////////////////////////////////// */



DECLARE Candidates(0:999) FIXED BIN(15);


DECLARE i FIXED BIN(31);


DECLARE j FIXED BIN(31);



/* define the sieve data structure */


DO i=0 TO 999 BY 1;


/* everything is potentially prime until proven otherwise */


Candidates(i) = 1;


END;


/* Neither 1 nor 0 is prime, so flag them off */


Candidates(0) = 0;


Candidates(1) = 0;


/* start the sieve with the integer 0 */


i = 0;


DO WHILE (i<1000);


/* advance to the next un-crossed out number. */


/* this number must be a prime */


DO WHILE ((i<1000) & (Candidates(i) = 0));


i = i + 1;


END;


/* insure against running off the end of the data structure */


IF (i<1000) THEN


DO;


/* cross out all multiples of the prime, starting with 2*p. */


DO j=2 TO 999 BY 1 WHILE (j*i < 1000);


Candidates(j*i) = 0;


END;


/* advance to the next candidate */


i = i + 1;


END;


END;


/* all uncrossed-out numbers are prime (and only those numbers) */


/* print all primes */


DO i=0 TO 999 BY 1;


IF (Candidates(i) ^= 0) THEN


DO;


PUT SKIP LIST(i," is prime");


END;


END;


END SieveE;

Click Here for the actual code.