The F-Word of Programming


In Three ways to solve the goats, wolves and lions puzzle, I described my feaasible brute force algorithm, which I had coded in my first programming language (and still the one in which I am reasonably fluent): Fortran, the, in the meantime, F-word of programming.
Input device of the kind I used in 1978
http://blogs.laprensagrafica.com/litoibarra/?p=2808

I know that Fortran is less elegant than, say, the Wolfram language, but
(a) I like it, and
(b) I am quite good in wiriting small sample codes.
The third reason will be given below.

The code reads as (everything hard coded)


      logical status (0:78,0:78,0:78)
      integer i,j,k,animals, count
      do i=0,78
      do j=0,78
      do k=0,78
      status (i,j,k)=.false.
      end do
      end do
      end do
c
c status (i,j,k) will be set to true if a forest with i goats, j wolves and k is reached.
c
      status (17,55,6)=.true.
      count=0
      do animals= 78,1,-1
       do i=0,animals
        do j=0,animals-i
         k=animals-i-j


         if(status(i,j,k))then
c
c count counts all possible forests
c
           count=count+1
c ------------------------------------------------------------
c the following lines print possible final states
c
           if(i. eq. 0 .and. j .eq. 0)then
             write (*,*)i,j,k
           end if
           if(i. eq. 0 .and. k .eq. 0)then
             write (*,*)i,j,k
           end if          
           if(j. eq. 0 .and. k .eq. 0)then
             write (*,*)i,j,k
           end if
c------------------------------------------------------------
c in the sequel: Possible meals
c
           if(i*j .ne. 0)then
             status (i-1,j-1,k+1)=.true.
           end if
           if(i*k .ne. 0)then
             status (i-1,j+1,k-1)=.true.
           end if
           if(j*k .ne. 0)then
             status (i+1,j-1,k-1)=.true.
           end if
c
         end if


         end do
        end do
      end do
      write(*,*) "count= ",count
      end

It turns out that with the specific example, 4208 different states of forests can be obtained, and it takes less than one second to calculate them (compiled with the free G95 compiler and executed on one core of my i5 desktop computer).

The above code is not memory efficient at all, and memory allocation needs quite a portion of elapsed time,

Like in finite elements for a transient diffusion equations, we do not need a three-dimensional array but two (old and new) two-dimensional ones would be sufficient.

When we increase the number of initial animals to (117, 155, 106) (yielding 223 lions at the maximal final forest), the Fortran program still performs in less than 2 seconds and calculates 985508 possible forests. Can a functional language do this as well?