My first compiled language was Fortran. I was an undergraduate physics student at the University of Wisconsin-River Falls, and as part of the physics program, every student had to learn Fortran programming. Since this was the very early 1990s, we used FORTRAN 77 (the new capitalization of "Fortran" would be established with Fortran 90, a few years later).

We learned FORTRAN 77 so could do numerical analysis, or other programmatic analysis of lab data. For example, while spreadsheets of the era could calculate a linear fit to x-y data, including standard deviations, we could not fit polynomials to nonlinear data. But given a maths textbook, you could write your own program in FORTRAN 77. I wrote many programs like this throughout my undergraduate career.

As a research intern at a national lab between my junior and senior years, my mentors discovered I knew FORTRAN. So I got the assignment to port a FORTRAN IV program to FORTRAN 77 (Fortran 90 had recently been defined, and the lab didn't have the compiler yet). It was my first programming "job" and through experience I realized I wanted a career in IT rather than physics research.

I also taught myself the C programming language, and thereafter switched to C when I needed to write a program. I haven't needed to write Fortran code since then.

The last time I wrote anything in Fortran was a few years ago. At the time, I read an article about a proposed proof to the Collatz conjecture: the so-called "hailstone" sequence. From Slashdot: "A hailstone sequence starts from any positive integer

*n*the next number in the sequence is

*n*/2 if

*n*is even and 3

*n*+1 if

*n*is odd. The conjecture is that this simple sequence always ends in 1. Simple to state but very difficult to prove and it has taken more than 60 years to get close to a solution."

I hadn't heard of the hailstone sequence before, but I thought it was an interesting problem. I could have easily written this program in C or even Bash, but I used the opportunity to return to my roots with Fortran. I created a simple FORTRAN 77 program to iterate the hailstone sequence. To celebrate Fortran's milestone birthday, I'd like to share my program with you:

PROGRAM HAILSTNC A PROGRAM TO ITERATE THE HAILSTONE SEQUENCE. C C THE RULE FOR A HAILSTONE SEQUENCE IS: C C START AT ANY POSITIVE INTEGER, N. C IF N IS EVEN, THE NEXT NUMBER IS N/2. IF N IS ODD, THE NEXT NUMBER C IS 3N+1. ITERATE. C C IN THEORY, ALL HAILSTONE SEQUENCES WILL END WITH 1.10 PRINT *, 'Enter starting number (any positive integer):' READ *, N C PRINT *, 'You entered:', N IF (N.LT.1) THEN PRINT *, 'You must enter a positive integer' GOTO 10 ENDIF C ITERATE PRINT *, N ITER = 0 20 IF (MOD(N,2).EQ.0) THEN N = N / 2 ELSE N = (3 * N) + 1 ENDIF ITER = ITER + 1 PRINT *, N IF (N.NE.1) GOTO 20 PRINT *, 'Number of iterations:', ITER END PROGRAM

This program doesn't demonstrate the best programming practices, but it does represent many FORTRAN 77 programs. To illustrate, allow me to walk you through the code:

First, FORTRAN code was originally written on punched cards. The first FORTRAN used columns to understand the program listing. FORTRAN 77 programs used the same column rules:

- If a
`C`

or`*`

in column 1, the line is a comment - Program labels (line numbers) are in columns 1–5
- Program statements begin on column 7, but cannot go beyond column 72
- Any character in column 6 will continue the line from the preceding line (not used here)

While you could (and should) declare variables to be of a certain type, FORTRAN 77 used a set of implicit rules to assign variable types: all variables starting

`I`

–`N`

are assumed `INTEGER`

, and variables starting with other letters are declared `REAL`

(floating point).My program uses only two variables,

`N`

and `ITER`

, which are both integer variables.FORTRAN 77 is a simple language, so you should be able to figure out what the program is doing based on those rules. I'll add a note about the code starting with line label

`20`

. FORTRAN 77 doesn't have a do-while loop concept, so you end up constructing your own using a label, `IF`

, and `GOTO`

.And that's what happens starting at label

`20`

. The program begins a loop iteration, following the rules of the hailstone sequence: for each positive integer *n*the next number in the sequence is

*n*/2 if

*n*is even and 3

*n*+1 if

*n*is odd. After updating the

`ITER`

iteration count and printing the current value of `N`

, the program continues to loop back to line label `20`

(using `GOTO`

) until `N`

reaches 1.When the loop is complete, the program prints the number of iterations, and exits.

Here's a sample run:

Enter starting number (any positive integer):1111 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Number of iterations: 14

Happy birthday, Fortran!

## No comments:

## Post a Comment