      PROGRAM beepfish
!================================================================
! A program which turns a pure real or pure imaginary integer 
! into beep fish doom... format
!================================================================
      IMPLICIT none
!
      INTEGER numprimes
      PARAMETER(numprimes = 23)
      INTEGER primes(numprimes), factors(numprimes), int, i, j, 
     c        junk, check, trialprime, isitprime, l, k, 
     c        lengths(numprimes), sum, inte
      DOUBLE PRECISION tart, sporran
      CHARACTER names(numprimes)*16, blank*1, txtinp*16
      COMPLEX mikie
!----------------------------------------------------------------
! Sets the variable 'blank' to a space
!----------------------------------------------------------------
      WRITE(blank,'(A1)') ' '
!----------------------------------------------------------------
! Reads the names of the first 'numprimes' primes
!----------------------------------------------------------------
      OPEN(1, FILE='names.txt', STATUS='old')
      DO i = 1, numprimes
         READ(1,*,END=2) names(i)
      ENDDO
 2    CLOSE(1)
!----------------------------------------------------------------
! Then works out the lengths of the names
!----------------------------------------------------------------
      DO i = 1, numprimes
        lengths(i) = 0
        DO j = 1, 16
          IF (names(i)(j:j).EQ.blank) THEN
            lengths(i) = j-1
            GOTO 3
          ELSE
            CONTINUE
          ENDIF
        ENDDO
 3      CONTINUE
      ENDDO
!----------------------------------------------------------------
! This bit of the programme is currently pointless...
!----------------------------------------------------------------
      tart=acos(-1.D0)
      sporran=exp(1.D0)
!     mikie=(0,1)
!----------------------------------------------------------------
! Reads in the number to convert
!----------------------------------------------------------------
!
 4    WRITE(*,'("Enter integer to convert: ", $)')
      READ(*,*,ERR=9000) txtinp
      WRITE(*,'(7X,A,$)') ' = '
!----------------------------------------------------------------
! Finds number, multiplies by mikie if followed by i, multiplies
! by mikie mikie if negative
!----------------------------------------------------------------
!
      DO i = 1, 16
        IF (txtinp(i:i).EQ.blank) THEN
          READ(txtinp(1:i-1),*,ERR=9000) inte
          GOTO 5
        ELSEIF (txtinp(i:i).EQ.'i') THEN
          READ(txtinp(1:i-1),*) inte
          WRITE(*,'("mikie ",$)')
          GOTO 5
        ELSE
          CONTINUE
        ENDIF
      ENDDO
!
 5    IF (inte.NE.ABS(inte)) THEN
        WRITE(*,'("mikie mikie ",$)')
        inte = ABS(inte)
      ELSEIF(inte.EQ.0) THEN
        WRITE(*,'("cake")')
        GOTO 9999
      ELSE
        CONTINUE
      ENDIF
! 
      int = inte
!----------------------------------------------------------------
! Works out first 'numprimes' primes
!----------------------------------------------------------------
      trialprime = 0
      l = 1
      DO i = 1, 10000
        trialprime = trialprime + 1
        isitprime = 0 
        primes(l) = 0 
        IF (l.GT.numprimes) THEN
          GOTO 6
        ELSE 
          CONTINUE
        ENDIF
        DO j = 1, trialprime-2
          junk = trialprime / (j+1)
          check = (j+1) * junk
          IF (check.EQ.trialprime) THEN
            isitprime = 1
          ELSE
            CONTINUE
          ENDIF
        ENDDO
        IF (isitprime.EQ.0) THEN
          primes(l) = trialprime
          l = l+1
        ELSE
          CONTINUE
        ENDIF
      ENDDO
!----------------------------------------------------------------
! Tries factorising into the primes currently available
!----------------------------------------------------------------
  6   DO i = 1, numprimes
        factors(i) = 0
        DO j = 1, int
          junk = int / primes(i)
          check = junk * primes(i)
          IF (check.EQ.int) THEN
             factors(i) = factors(i) + 1
             int = junk
          ELSE
             GOTO 10
          ENDIF
        ENDDO
 10     CONTINUE
      ENDDO
!----------------------------------------------------------------
! Checks to see if this infact works
!----------------------------------------------------------------
      sum = 1
      DO i = 1, numprimes
          sum = sum * primes(i)**factors(i) 
      ENDDO
      IF (sum.NE.inte) THEN
        WRITE(*,'(/,3A)') 'Erm, there''s a problem somewhere, ',
     c 'probably one of the factors is undefined. Would advise ',
     c  'bugging Dave to define some more primes.'
        GOTO 9999
      ELSE
        CONTINUE
      ENDIF
!----------------------------------------------------------------
! Writes out the value of 'inte' in beep fish doom... format
!----------------------------------------------------------------
      DO i = 2, numprimes
        k = lengths(i)
        IF (factors(i).GT.0) THEN
        DO j = 1, factors(i)
          WRITE(*,20) names(i)(1:lengths(i)+1)
 20       FORMAT(A,$)
        ENDDO
        ELSE
          CONTINUE
        ENDIF
      ENDDO
!
      WRITE(*,'(/)')
!----------------------------------------------------------------
! Error message(s)
!----------------------------------------------------------------
      GOTO 9999
 9000 WRITE(*,'(/,A)') 'Must type in an integer ... try again'
      GOTO 4
!
 9999 STOP
      END
