FORTRAN

FORTRAN, being the granddaddy of all high-level languages, needs to be represented here.

This program is written in FORTRAN 95. (Well, it will probably work with FORTRAN 77 compilers too.) I can actually write FORTRAN IV and FORTRAN II, but I despise both of those languages, and refuse to write anything in either one.

The thing that I particularly hate is following type of IF statements.

IF (K-25) 14,17,22

This statement tests K against the value 25. If the result of the subtraction is negative, control transfers to the statement labeled 14. If the result is zero control transfers to label 17, and if the result is positive, control transers to label 22. This and the GOTO and the DO 10 J = 1 TO 10 statements are the only flow control statements in FORTRAN II and FORTRAN IV. The if statments are horrid to use because you always forget which direction the subtraction should go. Replicating labels in the 3-way IF statement is OK, and is much more common than three different labels. It was quite common to see label lists like 23,24,24 (less than) or 14,14,15, (greater than) or 20,30,20 (equal to). One of the "jumps" was usually to the next statement in line.

I hate the fact that every variable that starts with an "I" is an integer and every variable that starts with a "Q" is real.

I hate the COMMON statement that allows subroutines to rename global variables.

I hate statement labels.

I hate those stupid comparison operators .LT. and .EQ. . (Yeah, you need the dots.)

I hate FORTRAN. I hate FORTRAN 77 and FORTRAN 95 too, just not as much as I hate FORTRAN II and FORTRAN IV.

Did I mention that I hate FORTRAN?

I hate FORTRAN.

This program compiles and runs with the Silverfrost F95 compiler integrated with Visual Studio 2005. Note that this compiler is free, but it's crippleware.


! //////////////////////////////////////////////////////////


! // Name: Peter M. Maurer


! // Program: Sieve of Eratosthenes


! // Due: Never


! // Language: FORTRAN


! /////////////////////////////////////////////////////////



PROGRAM SieveE


! define the sieve data structure


INTEGER Candidates(999);


INTEGER i,j;


DO 10 i=1 , 999


! // everything is potentially prime until proven otherwise


Candidates(i) = 1


10 CONTINUE


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


Candidates(1) = 0


! // start the sieve with the integer 1


i = 1


DO WHILE (i .LT. 1000)


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


! // this number must be a prime


DO WHILE (i .LT. 1000 .AND. Candidates(i) .EQ. 0)


i = i+1


END DO


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


IF (i .LT. 1000) THEN


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


j = 2


DO WHILE (j*i .LT. 1000)


Candidates(j*i) = 0


j = j + 1


END DO


! // advance to the next candidate


i = i+1;


ENDIF


END DO


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


! // print all primes


DO 20 i=1 , 999


IF (Candidates(i) .NE. 0) THEN


PRINT *,i," is prime";


ENDIF


20 CONTINUE


END

Click Here for the actual code.