/*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 that maintains the stack so that two adjacent C C fractions on the stack, say n/d and n'/d', satisfy d*n'-n*d'=1. C C C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/ #include <stdio.h> #include <math.h> unsigned int lagrange(unsigned int N, unsigned int *R); unsigned int farey(unsigned int N, unsigned int *R, unsigned int *STACK); void main() { unsigned int N,R[30000],S[30000],MAXN; unsigned int L,M,I; unsigned int STACK[200]; // // 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); // // GENERATE FAREY SERIES USING ALGORITHM AND COMPARE RESULTS // M=farey(N,R,STACK); printf("order=%d, size=%d, size=%d \n",N,L,M); for (I=0; I<M; 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; }