/*CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                             C
C  CHECK RESULTS GIVEN BY FAREY SERIES ALGORITHM                              C
C  06/11/07 (DKC)							      C
C                                                                             C
C  This program checks the results given by the algorithm for generating the  C
C  fractions in a Farey series after two given successive fractions (four     C
C  theorems are employed).						      C
C									      C
C  If the denominators of the given successive fractions are increasing, the  C
C  first two theorems can be used to find the next pair of successive         C
C  fractions where the denominators are decreasing.  The last two theorems    C
C  can then be used to find the next pair of successive fractions where the   C
C  denominators are increasing, etc.  Similar logic applies when the          C
C  denominators of the given successive fractions are decreasing.             C
C                                                                             C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/
#include <stdio.h>
#include <math.h>
unsigned int lagrange(unsigned int N, unsigned int *R);
void harfar(unsigned int N, unsigned int M, unsigned int *R,
	    unsigned int NUM, unsigned int DEN, unsigned int OLDNUM,
	    unsigned int OLDDEN);
void main() {
unsigned int N,R[30000],S[30000],NUM,DEN,OLDNUM,OLDDEN,MAXN;
unsigned int L,M,I,J;
//
// ORDER OF FAREY SERIES
//
MAXN=200;
/***************/
/* BEGIN LOOP  */
/***************/
for (N=2; N<=MAXN; N++) {
//
// GENERATE FAREY SERIES (USING HAROS' THEOREM AND LAGRANGE'S ALGORITHM)
//
   L=lagrange(N,S);
   printf("order=%d, size=%d \n",N,L);
//
// GENERATE FAREY SERIES USING ALGORITHM AND COMPARE RESULTS
//
   for (J=0; J<L-2; J++) {
      M=J;
      OLDNUM=S[2*J];
      OLDDEN=S[2*J+1];
      NUM=S[2*J+2];
      DEN=S[2*J+3];
      for (I=J; I<L; I++) {
	 R[2*I]=0;
	 R[2*I+1]=0;
	 }
      harfar(N,M,R,NUM,DEN,OLDNUM,OLDDEN);
      for (I=J; I<L; I++) {
	 if(R[2*I]!=S[2*I]) {
	    printf("error: I=%d, NUMS=%d, %d \n",I,R[2*I], S[2*I]);
	    goto L400;
	    }
	 if(R[2*I+1]!=S[2*I+1]) {
	    printf("error: I=%d, DENS=%d, %d \n",I,R[2*I+1], S[2*I+1]);
	    goto L400;
	    }
	 }
      }
   }
/***************/
/* END LOOP    */
/***************/
L400: return;
}