One of this week’s opportunities to find humour in these dark days was the announcement that the US state of New Jersey despirately needed COBOL programmers. Of course, this was due to pandemic mass layoffs overwhelming the neglected state unemployment insurance scheme so this isn’t really that funny—but pickings are slim at present, and people take their amusement where they can.

I don’t have any experience with COBOL, alas, but it did remind me of the semi-obsolete language I’ve been meaning to look at for years: FORTRAN, or rather Fortran because I’m not quite yet ready to subject myself to anything older than Fortran90, when they changed the capitalisation.

Fortran was designed for numerical computation, which is very much my programming jam. I usually encounter it in (typically older) binary R packages: on Linux R has a preference for downloading and compiling the source, and if your system lacks the GNU Fortran compiler (gfortran) you may get caught out by this! It’s certainly happened to me in the past, although I will note that this has not been an issue with Manjaro.

I won’t pretend to have suddenly become an expert, but I have played around a bit over the last few days. My own idea of a “hello world” program is testing primality, i.e. whether a number has divisors other than 1 or itself. I was obsessed with prime numbers as a kid, particularly around the time when I learned to program with QBASIC on an old Windows 98SE, and used to daydream about disproving the Riemann hypothesis and cracking RSA encryption. Teenagers have the strangest life plans.

With apologies for doubtless non-standard and inconsistent coding style, here is my code:

!This is file : prime
! Author = petra
! Started at: 06.04.20
Program  prime
    Implicit None
    integer(kind = 16):: test
    real:: upper
    logical:: res
    character(100):: cmdtemp
    integer(4):: args, itarg
    args = command_argument_count()
    itarg = 1

    do while (itarg .le. args)
    call get_command_argument(itarg, cmdtemp)
    read(cmdtemp, *)test
    res = isPrime(test)
    print "(i30': 'l1)", test, res
    itarg = itarg+1
    end do

    Function isPrime (val)
        Implicit None
        logical:: isPrime
        integer(kind = 16):: val

        integer(kind = 16):: counter
        real:: upper

        if (val .le. 1) then
            isPrime = .false.
        else if (val .eq. 2) then
            isPrime = .true.
        else if (modulo(val, 2) .eq. 0) then
            isPrime = .false.
        end if
        upper = sqrt(real(val))
        counter = 3
        do while (counter .le. upper)
        if (modulo(val, counter) .eq. 0) then
            isPrime = .false.
        end if
        counter = counter+2
        end do
        isPrime = .true.

    End Function isPrime

End Program  prime

This program (compiled with gfortran prime.f90) takes a list of numbers and outputs whether they are prime (T) or composite (F). For example:

[petra@inoc fortran-scratchings]$ ./a.out 1 2 3 4 5 6 7 23456789                                                                        
                             1: F                                                                                                       
                             2: T                                                                                                       
                             3: T                                                                                                       
                             4: F                                                                                                       
                             5: T                                                                                                       
                             6: F                                                                                                       
                             7: T                                                                                                       
                      23456789: T  

23456789 is one of my favourite numbers: it’s the largest easy to memorise prime I know. On the graphics calculator I had in highschool this would have taken a while, but with a vaguely modern computer and Fortran it’s as near to instant as I could hope.

I really wish I had a Fortran compiler and manual on my computer as a kid. I’m not sure how much I can really do with it now, but it’s certainly fun!