COBOL

If you're a "right-thinking" computer scientist, you're supposed to ... no let me correct that, you're obligated to hate COBOL. I never figured out why. Admitedly, it's a pretty peculiar-looking language, but it gave us structures, and eventually classes and objects. It was one of the first languages that you could program in without using GOTO's. It was also how I earned my living for several years, so I have nothing against the language. I'm not an advocate, but I don't hate it either.

One peculiarity of COBOL is that everything beyond colunm 72 is comments. I once discovered a program that had been reported to IBM's tech support because it inexplicably refused to work. The problem stems from this. An IF statement begins with the keyword "IF" and ends with the first period following the "IF". All statements following the IF are part of the "THEN" body until you hit a period. The mysterious program had an IF statement with a single statement in the THEN body. The period following the statement was in column 73, making it a comment. This caused the IF to "eat" the next statement as well. As far as the programmer could see, the statement following the IF was supposed to execute when the condition was false, but it wouldn't.

This program compiles and runs using the Micro Focus Net Express 5.0 personal edition COBOL compiler integrated with Visual Studio 2005. This compiler is crippleware, but will accept programs up to 2000 lines.

The indenting you see below is required. In the procedure division, labels must start in column 8 and executable statements must start in column 12 or beyond. This same sort of indenting is required in the other sections as well.


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


* // Name: Peter M. Maurer


* // Program: Sieve of Eratosthenes


* // Due: Never


* // Language: COBOL


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



identification division.


program-id. SieveE as "SieveE.SieveE".



environment division.


configuration section.


repository.




data division.


working-storage section.


77 i pic 99999.


77 j pic 99999.


77 Product pic 99999.


77 Showit pic ZZZZZ.


77 Candidates pic 99999 occurs 999.


procedure division.


Main_Part.


Move 1 to I.


Perform Init_1 999 times.


Move 0 to Candidates(1).


* start the sieve with the integer 1


Move 1 to i.


Perform Check_Number Until i >= 1000.


Move 1 to i.


Perform Print 999 times.


goback.


Init_1.


Move 1 to Candidates(i).


Add 1 to I.


Check_Number.


* advance to the next un-crossed out number.


* this number must be a prime.


Perform Advance Until i >= 1000 or Candidates(i) NOT = 0.


* Avoid running off the end of the data structure.


if i >= 1000 EXIT.


Move 2 to j.


Multiply i by j giving Product.


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


Perform Cross_Out Until Product >= 1000.


* go on to next prime.


Add 1 to i.


Advance.


Add 1 to i.


Cross_Out.


Move 0 to Candidates(Product).


Add 1 to j.


Multiply i by j giving Product.


Print.


if Candidates(i) NOT = 0


Move i to Showit


Display Showit, " is prime.".


Add 1 to i.


end program SieveE.

Click Here for the actual code.