       PROGRAM VDW
       
       IMPLICIT NONE
       
       INTEGER*2  A,B,MAXLENGTH
       INTEGER*2  NUMBSURV
       
       PARAMETER (A=1,B=1,NUMBSURV=100000,MAXLENGTH=1000)
       
       INTEGER*2  ADDPOINT,FILENUM,FLAG
       INTEGER*2  NUMSURVIVORS,NUMPREADD,I,J,S
       INTEGER*2  TEMPSET(MAXLENGTH),PREADD(MAXLENGTH,NUMBSURV)
       INTEGER*2  GOOD(2),NUMGOOD,T,SURVIVORS(MAXLENGTH,NUMBSURV)
       
       INTEGER*2  VDW,GCOLOR
       
       FILENUM=11
       
       FLAG=0
       ADDPOINT=2
       NUMPREADD=4
       NUMSURVIVORS=0
       
       
C   INITIAL CONDITIONS:  PREADD=[[0,0],[0,1],[1,0],[1,1]]       
           
       PREADD(1,1)=0
       PREADD(2,1)=0
       
       PREADD(1,2)=0
       PREADD(2,2)=1
       
       PREADD(1,3)=1
       PREADD(2,3)=0 
       
       PREADD(1,4)=1
       PREADD(2,4)=1
       
       
       
       DO WHILE(FLAG.EQ.0)
       
       ADDPOINT=ADDPOINT+1
       WRITE(FILENUM,*) 'ADDING THE DIFFERENCE' , ADDPOINT
       WRITE(FILENUM,*) 'NUM SURVIVORS' , NUMPREADD
       
       IF (ADDPOINT.GT.MAXLENGTH) THEN
         WRITE(FILENUM,*) 'WARNING:  OUT OF BOUNDS, INCREASE MAXLENGTH'
       END IF
       
       IF (NUMPREADD.GT.NUMBSURV) THEN
         WRITE(FILENUM,*) '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 ADD 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,A,B,GOOD,NUMGOOD).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, AND WE HAVE OUR
C  VALUE FOR F(A,B)=MIN LENGTH OF 2-COLORING TO AVOID THE
C  TRIPLE {X,AX+D,BX+2D}
       
       IF (NUMSURVIVORS.EQ.0) THEN
         WRITE(FILENUM,*) 'F(A,B)=', ADDPOINT
         WRITE(FILENUM,*) 'NUMBER MAX VALID COLORINGS:' , NUMPREADD
         
         DO J=1,NUMPREADD
           WRITE(FILENUM,*) 'VALID COLORING NUMBER' , J
           DO I=1,ADDPOINT-1
           WRITE(FILENUM,*) PREADD(I,J)
           END DO
           WRITE(FILENUM,*) ' '
         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
         


C  IN THIS FUNCTION WE ASSUME THAT BX+2D IS THE LARGEST ELEMENT
C  OF THE TRIPLE.  MAYBE THIS SHOULD BE MODIFIED?????????

C  S IS THE SET OF X IN TEMPSET SUCH THAT BX+2D=ADDPOINT
C  DTOS IS THE D ASSOCIATED WITH SUCH AN X
       		
       INTEGER*2 FUNCTION GCOLOR(TEMPSET,ADDPOINT,A,B,GOOD,NUMGOOD)
       IMPLICIT NONE
       INTEGER*2 A,B,ADDPOINT,NUMGOOD,D,BR,X,T1,T2
       INTEGER*2 TEMPSET(ADDPOINT-1),GOOD(2)
       INTEGER*2 S(ADDPOINT-1),LENGTHOFS, DTOS(ADDPOINT-1)
       
       BR=(ADDPOINT-B)/2
       LENGTHOFS=0
       		
       IF (BR.GT.0) THEN
         DO D=1,BR 
           DIVISORS(ADDPOINT-2*D,SETDIV,LENGTHDIV)
           
           IF (MEMBER(B,SETDIV,LENGTHDIV).EQ.1) THEN
             LENGTHOFS=LENGTHOFS+1
             S(LENGTHOFS)=(ADDPOINT-2*D)/B
             DTOS(LENGTHOFS)=D           
           END IF
           
         END DO
         
       ELSE
         GCOLOR=1
         NUMGOOD=2
         GOOD(1)=0
         GOOD(2)=1
         RETURN
       END IF
       
       FLAG2=0
       
       IF (LENGTHOFS.GT.0) THEN
       
         DO I=1,LENGTHOFS
           X=S(I)
           D=DTOS(I)
           IF TEMPSET(X)=TEMPSET(A*X+D) THEN
           
             IF (FLAG2.EQ.1) THEN
             T2=1-TEMPSET(X)
               IF (T1.NE.T2) THEN
                 GCOLOR=0
                 RETURN
               END IF
             ELSE
             T1=1-TEMPSET(X)
             FLAG2=1
             END IF             
           END IF
         END DO

         IF (FLAG2.EQ.0) THEN
           GCOLOR=1
           NUMGOOD=2
           GOOD(1)=0
           GOOD(2)=1
           RETURN
         END IF
          
       ELSE
         GCOLOR=1
         NUMGOOD=2
         GOOD(1)=0
         GOOD(2)=1
         RETURN
       END IF
              
       GCOLOR=1
       NUMGOOD=1
       GOOD(1)=T1
       
       END
           





       INTEGER*2 DIVISORS(INTG,SETDIV,LENGTHDIV)
       IMPLICIT NONE
       INTEGER*2 INTG,SETDIV(INTG),LENGTHDIV,I,J
       J=0
       DO I=1,INTG
       IF (I*(INTG/I).EQ.INTG) THEN
         J=J+1
         SETDIV(J)=I
       END IF
       END DO
       LENGTHDIV=J
       RETURN
       END


             
       INTEGER*2 FUNCTION MEMBER(ELT,LST,LNGTH)
       IMPLICIT NONE
       INTEGER*2 LNGTH,F,ELT,LST(LNGTH)
       DO F=1,LNGTH
            IF (ELT.EQ.LST(F)) THEN
	      MEMBER=1
	      RETURN
	    END IF
       END DO
       MEMBER=0
       RETURN
       END       
       

       
       
       