﻿ algorithm for generating a Farey series
```/*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 in the    C
C  series.  (This algorithm uses the theorem that if h/k and h'/k' are        C
C  successive fractions in a Farey series and k' > k, then the fraction in    C
C  the series after the fractions corresponding to the points on the line     C
C  between (h,k) and (h',k') is (h' - h)/(k' - k).)                           C
C                                                                             C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/
#include <stdio.h>
#include <math.h>
unsigned int lagrange(unsigned int N, unsigned int *R);
void haros(unsigned int N, unsigned int M, unsigned int *R,
unsigned int OLDNUM, unsigned int OLDDEN, unsigned int NUM,
unsigned int DEN);
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;
}
haros(N,M,R,OLDNUM,OLDDEN,NUM,DEN);
for (I=J; I<L; I++) {
if((R[2*I]!=S[2*I])||(R[2*I+1]!=S[2*I+1])) {
printf("error: I=%d, NUMS=%d, %d \n",I,R[2*I], S[2*I]);
printf("error: I=%d, DENS=%d, %d \n",I,R[2*I+1], S[2*I+1]);
goto L400;
}
}
}
}
/***************/
/* END LOOP    */
/***************/
L400: return;
}
```