Page 1 of 1

FORTRAN : The Fibonacci Sequence

#1 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 547
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Posted 02 May 2011 - 11:38 AM

FORTRAN : The Fibonacci Sequence


This tutorial introduces you to the ancient world of FORTRAN (only joking ... FORTRAN is alive and well as testified by the FORTRAN 2008). This is not a particularly useful language for developing Windows applications, but has a firm footing in the world of mathematics from whence it originated back in 1954.

The skeleton for a FORTRAN program consists of just three lines, so it is quite compact.

        PROGRAM    name
           IMPLICIT   NONE
        END PROGRAM name

Basic Data Types

FORTRAN supports integers, reals, logical and character data types. Examples of this would be:

        INTEGER         :: Counter
        REAL            :: Value
        LOGICAL         :: Boolean
        CHARACTER       :: A, B
        CHARACTER*20    :: String

In the Fibonacci example below, there are four integers defined, FIRST, SECOND, TEMP and IX.

The DO Loop

The DO loop is equivalent to the for loop in C/C++. it's format is DO index = initial, maximum, step ... END DO. So, in the example below, we have DO IX = 1, 45, 1 which states execute the statements between the DO and END DO setting IX to an initial value of 1, ending the loop when IX reaches 45, and increment IX by one for every loop iteration.

Output To The Display

In the example below, lines 6, 7 and 12 output values to the console display. The format is WRITE (*,*) followed by one or more data fields to write.

The Code

This code has been compiled and tested using the gfortran front-end to the gcc compiler under Linux.

        PROGRAM    Fibonacci
           IMPLICIT   NONE
           FIRST = 0
           SECOND = 1
           WRITE (*,*) FIRST
           WRITE (*,*) SECOND
           DO IX = 1, 45, 1
              TEMP = FIRST + SECOND
              FIRST = SECOND
              SECOND = TEMP
              WRITE (*,*) TEMP
           END DO
        END PROGRAM Fibonacci

Is This A Good Question/Topic? 0
  • +

Page 1 of 1