      PROGRAM VDW
       
      IMPLICIT NONE

C  HANDLES K=3 TO 10 
       
      INTEGER*2  K,MAXLENGTH,PPSIZE,C
      INTEGER*4  NUMBSURV
       
      PARAMETER (K=8,PPSIZE=8,C=0,NUMBSURV=1000000,MAXLENGTH=200)
       
      CHARACTER*8 FILENUM
      INTEGER*2  ADDPOINT,FLAG,PP(PPSIZE),S,N
      INTEGER*4  NUMSURVIVORS,NUMPREADD,I,J
      INTEGER*2  TEMPSET(MAXLENGTH),PREADD(MAXLENGTH,NUMBSURV)
      INTEGER*2  GOOD(2),NUMGOOD,T,SURVIVORS(MAXLENGTH,NUMBSURV)
       
      INTEGER*2  GCOLOR
       
      FILENUM='FIB8'
       
      FLAG=0
      ADDPOINT=1
      NUMPREADD=1
      NUMSURVIVORS=0
    
C   INITIAL CONDITIONS:  PREADD=[[0]]       
           
      PREADD(1,1)=0

C  PP IS THE SET OF DIFFERENCE IN A DIFFSEQUENCE

                                                                           
      PP(1)=1
      PP(2)=2
      PP(3)=3
      PP(4)=5
      PP(5)=8
      PP(6)=13
      PP(7)=21
      PP(8)=35


      DO WHILE (FLAG.EQ.0)


C  N IS THE LENGTH OF THE GOOD COLORINGS
       
C  N=ADDPOINT

         ADDPOINT=ADDPOINT+1
         WRITE(10,*) 'ADDING THE INTEGER' , ADDPOINT
         WRITE(10,*) 'NUM SURVIVORS' , NUMPREADD
       
         IF (ADDPOINT.GT.MAXLENGTH) THEN
         OPEN(UNIT=10,FILE=FILENUM)
             WRITE(10,*)
     *             'WARNING:  OUT OF BOUNDS, INCREASE MAXLENGTH'
         END IF
         IF (NUMPREADD.GT.NUMBSURV) THEN
         OPEN(UNIT=10,FILE=FILENUM)
            WRITE(10,*)
     *             'WARNING:  OUT OF BOUNDS, INCREASE NUMBSURV'
         END IF
       
                  
         DO I=1,NUMPREADD

            DO J=1,ADDPOINT-1
               TEMPSET(J)=PREADD(J,I)
            END DO
         
C   WE NOW CALL GCOLOR TO FIND OUT WHAT VALUE ADDPOINT
C   CAN TAKE.  GOOD IS EITHER {},{0},{1}, OR {0,1} FOR TWO
C   COLORS.  IF GOOD={} THE VALUE 0 IS RETURNED, ELSE THE
C   VALUE 1 IS RETURNED.  IF WE GET 1 BACK, WE CAN
C   GET 1 OR 2 SURVIVORS FROM TEMPSET.  NUMGOOD TELLS US
C   HOW MANY SURVIVORS WE GET FROM TEMPSET.
C   THE CODE IS WRITTEN SO THAT IT IS EASILY MODIFIED TO
C   RUN FOR MORE THAN 2 COLORS
         
            IF (GCOLOR(TEMPSET,ADDPOINT,K,GOOD,NUMGOOD,PP,PPSIZE)   
     *          .EQ.1) THEN
         
                NUMSURVIVORS=NUMSURVIVORS+NUMGOOD
                DO T=1,ADDPOINT-1
                   DO S=1,NUMGOOD
                      SURVIVORS(T,NUMSURVIVORS-NUMGOOD+S)=TEMPSET(T)
                   END DO
                END DO
                DO S=1,NUMGOOD
                   SURVIVORS(ADDPOINT,NUMSURVIVORS-NUMGOOD+S)=GOOD(S)
                END DO
            END IF
         END DO 

       

C  IF NO SURVIVORS THEN THE ALGORITHM IS DONE
       
         IF (NUMSURVIVORS.EQ.0) THEN
         OPEN(UNIT=10,FILE=FILENUM)
            FLAG=1
            WRITE(10,*) 'F(N)=', ADDPOINT
            WRITE(10,*) 'NUMBER MAX VALID COLORINGS:' , NUMPREADD
         
            DO J=1,NUMPREADD
               WRITE(10,*) 'VALID COLORING NUMBER' , J , ':' ,
     * (PREADD(I,J),I=1,ADDPOINT-1)
               WRITE(10,*) ' '
            END DO
         
C   ELSE IF THERE ARE SURVIVORS, WE REINITIALIZE AND REPEAT THE
C   LOOP WITH ADDPOINT=ADDPOINT+1 (DONE AT TOP OF LOOP)         
         
         ELSE
            DO J=1,NUMSURVIVORS
               DO I=1,ADDPOINT
                  PREADD(I,J)=SURVIVORS(I,J)
               END DO
            END DO
            NUMPREADD=NUMSURVIVORS
            NUMSURVIVORS=0
         END IF
      END DO 
   
      STOP
      END
         

      INTEGER*2 FUNCTION GCOLOR(TEMPSET,ADDPOINT,K,
     *                          GOOD,NUMGOOD,PP,PPSIZE)
      IMPLICIT NONE
      INTEGER*2 K,ADDPOINT,NUMGOOD,D,BR,X,T1,T2,PPSIZE
      INTEGER*2 TEMPSET(ADDPOINT-1),GOOD(2),PP(PPSIZE)
      INTEGER*2 I1,I2,I3,I4,I5,I6,I7,I8,I9,TMP(K-1)
      INTEGER*2 J1,J2,J3,J4,J5,J6,J7,J8,J9,NOGOOD

C  INITIALIZATION TO DISTINGUISH WHICH COLOR IS NO GOOD
C  THE NUMBER 9 IS MEANINGLESS (OTHER THAN IT IS NOT
C  ZERO NOR ONE - USED SO THAT WE DON'T GET A STRAY
C  0 OR 1 WHEN WE COMPARE AND DON'T INITIALIZE)

      NUMGOOD=2
      NOGOOD=9

      IF (K.EQ.3) THEN

         DO I1=1,PPSIZE 
            TMP(1)=PP(I1)
            DO I2=1,PPSIZE
               TMP(2)=PP(I2)
               IF (TMP(1)+TMP(2).LT.ADDPOINT) THEN
                  J1=ADDPOINT-TMP(1)
                  J2=J1-TMP(2)
                  IF (NOGOOD.GT.0.AND.
     *                     (TEMPSET(J1)+TEMPSET(J2).EQ.0)) THEN
                     NOGOOD=0
                     NUMGOOD=NUMGOOD-1
                  ELSE IF ((NOGOOD.EQ.0.OR.NOGOOD.GT.1).AND.
     *                       (TEMPSET(J1)+TEMPSET(J2).EQ.2)) THEN
                          NOGOOD=1
                          NUMGOOD=NUMGOOD-1
                  END IF
                  IF (NUMGOOD.EQ.0) THEN
                     GCOLOR=0
                     RETURN
                  END IF
               END IF
            END DO
         END DO

      ELSE IF (K.EQ.4) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       IF (TMP(1)+TMP(2)+TMP(3).LT.ADDPOINT) THEN
                          J1=ADDPOINT-TMP(1)
                          J2=J1-TMP(2)
                          J3=J2-TMP(3)
                          IF (NOGOOD.GT.0.AND.(TEMPSET(J1)+
     *                        TEMPSET(J2)+TEMPSET(J3).EQ.0)) THEN
                             NOGOOD=0
                             NUMGOOD=NUMGOOD-1
                          ELSE IF ((NOGOOD.EQ.0.OR.NOGOOD.GT.1).AND.
     *                             (TEMPSET(J1)+TEMPSET(J2)+
     *                             TEMPSET(J3).EQ.3)) THEN
                                  NOGOOD=1
                                  NUMGOOD=NUMGOOD-1
                          END IF
                          IF (NUMGOOD.EQ.0) THEN
                             GCOLOR=0
                             RETURN
                          END IF
                       END IF
                    END DO
                 END DO
              END DO

      ELSE IF (K.EQ.5) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          IF (TMP(1)+TMP(2)+TMP(3)+TMP(4).LT.
     *                        ADDPOINT) THEN
                             J1=ADDPOINT-TMP(1)
                             J2=J1-TMP(2)
                             J3=J2-TMP(3)
                             J4=J3-TMP(4)
                             IF (NOGOOD.GT.0.AND.(TEMPSET(J1)+
     *                           TEMPSET(J2)+TEMPSET(J3)+
     *                           TEMPSET(J4).EQ.0)) THEN
                                NOGOOD=0
                                NUMGOOD=NUMGOOD-1
                             ELSE IF ((NOGOOD.EQ.0.OR.NOGOOD.GT.1).
     *                                AND.(TEMPSET(J1)+TEMPSET(J2)+
     *                                TEMPSET(J3)+TEMPSET(J4) 
     *                                .EQ.4)) THEN
                                     NOGOOD=1
                                     NUMGOOD=NUMGOOD-1
                             END IF
                             IF (NUMGOOD.EQ.0) THEN
                                GCOLOR=0
                                RETURN
                             END IF
                          END IF
                       END DO
                    END DO
                 END DO
              END DO

      ELSE IF (K.EQ.6) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          DO I5=1,PPSIZE
                             TMP(5)=PP(I5)
                             IF (TMP(1)+TMP(2)+TMP(3)+TMP(4)
     *                           +TMP(5).LT.ADDPOINT) THEN
                                J1=ADDPOINT-TMP(1)
                                J2=J1-TMP(2)
                                J3=J2-TMP(3)
                                J4=J3-TMP(4)
                                J5=J4-TMP(5)
                                IF (NOGOOD.GT.0.AND.
     *                              (TEMPSET(J1)+TEMPSET(J2)+
     *                              TEMPSET(J3)+TEMPSET(J4)+
     *                              TEMPSET(J5).EQ.0)) THEN
                                    NOGOOD=0
                                    NUMGOOD=NUMGOOD-1
                                ELSE IF ((NOGOOD.EQ.0.OR.
     *                                   NOGOOD.GT.1).AND.(
     *                                   TEMPSET(J1)+
     *                                   TEMPSET(J2)+
     *                                   TEMPSET(J3)+
     *                                   TEMPSET(J4)+
     *                                   TEMPSET(J5)
     *                                   .EQ.5)) THEN
                                        NOGOOD=1
                                        NUMGOOD=NUMGOOD-1
                                END IF
                                IF (NUMGOOD.EQ.0) THEN
                                   GCOLOR=0 
                                   RETURN
                                END IF
                             END IF
                          END DO
                       END DO
                    END DO
                 END DO
              END DO                    

      ELSE IF (K.EQ.7) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          DO I5=1,PPSIZE
                             TMP(5)=PP(I5)
                             DO I6=1,PPSIZE
                                TMP(6)=PP(I6)
                                IF (TMP(1)+TMP(2)+TMP(3)+TMP(4)
     *                              +TMP(5)+TMP(6).LT.ADDPOINT)
     *                              THEN
                                   J1=ADDPOINT-TMP(1)
                                   J2=J1-TMP(2)
                                   J3=J2-TMP(3)
                                   J4=J3-TMP(4)
                                   J5=J4-TMP(5)
                                   J6=J5-TMP(6)
                                   IF (NOGOOD.GT.0.AND.
     *                                 (TEMPSET(J1)+TEMPSET(J2)
     *                                 +TEMPSET(J3)+TEMPSET(J4)
     *                                 +TEMPSET(J5)+TEMPSET(J6)
     *                                 .EQ.0)) THEN
                                      NOGOOD=0
                                      NUMGOOD=NUMGOOD-1
                                   ELSE IF ((NOGOOD.EQ.0.OR.
     *                                      NOGOOD.GT.1).AND.
     *                                      (TEMPSET(J1)+
     *                                      TEMPSET(J2)+
     *                                      TEMPSET(J3)+
     *                                      TEMPSET(J4)+
     *                                      TEMPSET(J5)+
     *                                      TEMPSET(J6)
     *                                      .EQ.6)) THEN
                                           NOGOOD=1
                                           NUMGOOD=NUMGOOD-1
                                   END IF
                                   IF (NUMGOOD.EQ.0) THEN
                                      GCOLOR=0
                                       RETURN
                                  END IF
                                END IF
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO   

      ELSE IF (K.EQ.8) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          DO I5=1,PPSIZE
                             TMP(5)=PP(I5)
                             DO I6=1,PPSIZE
                                TMP(6)=PP(I6)
                                DO I7=1,PPSIZE
                                   TMP(7)=PP(I7)
                                   IF (TMP(1)+TMP(2)+TMP(3)+
     *                                 TMP(4)+TMP(5)+TMP(6)+
     *                                 TMP(7).LT.ADDPOINT) THEN
                                      J1=ADDPOINT-TMP(1)
                                      J2=J1-TMP(2)
                                      J3=J2-TMP(3)
                                      J4=J3-TMP(4)
                                      J5=J4-TMP(5)
                                      J6=J5-TMP(6)
                                      J7=J6-TMP(7)
                                      IF (NOGOOD.GT.0.AND.
     *                                    (TEMPSET(J1)+TEMPSET(J2)+
     *                                    TEMPSET(J3)+TEMPSET(J4)+
     *                                    TEMPSET(J5)+TEMPSET(J6)+
     *                                    TEMPSET(J7).EQ.0)) THEN
                                         NOGOOD=0
                                         NUMGOOD=NUMGOOD-1
                                      ELSE IF ((NOGOOD.EQ.0.OR.
     *                                         NOGOOD.GT.1).AND.
     *                                         (TEMPSET(J1)+
     *                                         TEMPSET(J2)+
     *                                         TEMPSET(J3)+
     *                                         TEMPSET(J4)+
     *                                         TEMPSET(J5)+
     *                                         TEMPSET(J6)+
     *                                         TEMPSET(J7)
     *                                         .EQ.7)) THEN
                                              NOGOOD=1
                                              NUMGOOD=NUMGOOD-1
                                      END IF
                                      IF (NUMGOOD.EQ.0) THEN
                                         GCOLOR=0
                                         RETURN
                                      END IF
                                   END IF
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO   

      ELSE IF (K.EQ.9) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          DO I5=1,PPSIZE
                             TMP(5)=PP(I5)
                             DO I6=1,PPSIZE
                                TMP(6)=PP(I6)
                                DO I7=1,PPSIZE
                                   TMP(7)=PP(I7)
                                   DO I8=1,PPSIZE
                                      TMP(8)=PP(I8)
                                      IF (TMP(1)+TMP(2)+TMP(3)+TMP(4)
     *                                    +TMP(5)+TMP(6)+TMP(7)
     *                                    +TMP(8).LT.ADDPOINT) THEN
                                          J1=ADDPOINT-TMP(1)
                                          J2=J1-TMP(2)
                                          J3=J2-TMP(3)
                                          J4=J3-TMP(4)
                                          J5=J4-TMP(5)
                                          J6=J5-TMP(6)
                                          J7=J6-TMP(7)
                                          J8=J7-TMP(8)
                                          IF (NOGOOD.GT.0.AND.
     *                                       (TEMPSET(J1)+TEMPSET(J2)+
     *                                       TEMPSET(J3)+TEMPSET(J4)+
     *                                       TEMPSET(J5)+TEMPSET(J6)+
     *                                       TEMPSET(J7)+TEMPSET(J8)
     *                                       .EQ.0)) THEN
                                             NOGOOD=0
                                             NUMGOOD=NUMGOOD-1
                                          ELSE IF ((NOGOOD.EQ.0.OR.
     *                                             NOGOOD.GT.1).AND.
     *                                             (TEMPSET(J1)+
     *                                             TEMPSET(J2)+
     *                                             TEMPSET(J3)+
     *                                             TEMPSET(J4)+
     *                                             TEMPSET(J5)+
     *                                             TEMPSET(J6)+
     *                                             TEMPSET(J7)+
     *                                             TEMPSET(J8)
     *                                             .EQ.8)) THEN
                                                  NOGOOD=1
                                                  NUMGOOD=NUMGOOD-1
                                          END IF
                                          IF (NUMGOOD.EQ.0) THEN
                                             GCOLOR=0
                                             RETURN
                                          END IF
                                      END IF
                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO  


      ELSE IF (K.EQ.10) THEN

              DO I1=1,PPSIZE 
                 TMP(1)=PP(I1)
                 DO I2=1,PPSIZE
                    TMP(2)=PP(I2)
                    DO I3=1,PPSIZE
                       TMP(3)=PP(I3)
                       DO I4=1,PPSIZE
                          TMP(4)=PP(I4)
                          DO I5=1,PPSIZE
                             TMP(5)=PP(I5)
                             DO I6=1,PPSIZE
                                TMP(6)=PP(I6)
                                DO I7=1,PPSIZE
                                   TMP(7)=PP(I7)
                                   DO I8=1,PPSIZE
                                      TMP(8)=PP(I8)
                                      DO I9=1,PPSIZE
                                         TMP(9)=PP(I9)
                                         IF (TMP(1)+TMP(2)+TMP(3)+TMP(4)
     *                                    +TMP(5)+TMP(6)+TMP(7)
     *                                    +TMP(8)+TMP(9).LT.ADDPOINT)
     *                                    THEN
                                          J1=ADDPOINT-TMP(1)
                                          J2=J1-TMP(2)
                                          J3=J2-TMP(3)
                                          J4=J3-TMP(4)
                                          J5=J4-TMP(5)
                                          J6=J5-TMP(6)
                                          J7=J6-TMP(7)
                                          J8=J7-TMP(8)
                                          J9=J8-TMP(9)
                                          IF (NOGOOD.GT.0.AND.
     *                                       (TEMPSET(J1)+TEMPSET(J2)+
     *                                       TEMPSET(J3)+TEMPSET(J4)+
     *                                       TEMPSET(J5)+TEMPSET(J6)+
     *                                       TEMPSET(J7)+TEMPSET(J8)+
     *                                       TEMPSET(J9)
     *                                       .EQ.0)) THEN
                                             NOGOOD=0
                                             NUMGOOD=NUMGOOD-1
                                          ELSE IF ((NOGOOD.EQ.0.OR.
     *                                             NOGOOD.GT.1).AND.
     *                                             (TEMPSET(J1)+
     *                                             TEMPSET(J2)+
     *                                             TEMPSET(J3)+
     *                                             TEMPSET(J4)+
     *                                             TEMPSET(J5)+
     *                                             TEMPSET(J6)+
     *                                             TEMPSET(J7)+
     *                                             TEMPSET(J8)+
     *                                             TEMPSET(J9)
     *                                             .EQ.9)) THEN
                                                  NOGOOD=1
                                                  NUMGOOD=NUMGOOD-1
                                          END IF
                                          IF (NUMGOOD.EQ.0) THEN
                                             GCOLOR=0
                                             RETURN
                                          END IF
                                      END IF
                                   END DO
                                END DO
                             END DO
                          END DO
                       END DO
                    END DO
                 END DO
              END DO  
           END DO

      END IF  



C  IF IT MAKES IT OUT OF THE DO LOOPS THEN NUMGOOD = 1 OR 2 OR 9

      IF (NUMGOOD.EQ.2.OR.NUMGOOD.EQ.9) THEN
         GCOLOR=1
         GOOD(1)=0
         GOOD(2)=1
         RETURN
      ELSE 

C  ELSE NUMGOOD EQUAL 1 SO WE CHECK WHICH COLOR IS GOOD

         IF (NOGOOD.EQ.0) THEN
            GCOLOR=1
            GOOD(1)=1
            RETURN
         ELSE IF (NOGOOD.EQ.1) THEN
                 GCOLOR=1
                 GOOD(1)=0
                 RETURN
         END IF

      END IF

      RETURN 
      END


             
       
       



