Showing posts with label Farey generation using the Denominator Recurrence. Show all posts
Showing posts with label Farey generation using the Denominator Recurrence. Show all posts

Tuesday, September 30, 2008

Farey generation using the Denominator Recurrence

/******************************************************************
//
/* Generation of FAREY Sequence using the Denominator recurrence */
//
/******************************************************************
/*
f(2*n+1) = f{n) f(2*n+2} = f(n) + f(n+1),

n=0, 1, 2, ...
*/
/******************************************************************


#include "stdio.h"
#include "math.h"
#include "conio.h"

typedef unsigned __int64 UL_int;

UL_int i, n;
UL_int nom, denom;

UL_int MAX = 0;

UL_int SIR[(0x7FFFFFFF>>4)];

FILE * pf;

UL_int f(UL_int n)
{
    if(SIR[n]!=0) return SIR[n];
    else
    {


if ( (n==1) || (n==2) ) {SIR[n] = 1; return 1;}
else if((n%2)==1) {SIR[n] = (f((UL_int)floor(((n-1)/2.0))) + f((UL_int)floor((n+1)/2.0))); return SIR[n];}
else {SIR[n] = f((UL_int)floor(n/ 2.0)); return SIR[n];}
    }
}

int main(void) {
//clrscr();

pf=fopen("fractions.txt","w+");

//printf ("\nREC: -> NOM(n) / DENOM(n):\n");
for(i=1;i<8388608 16="16" 2="2" bits="bits" for="for" i="i" numbers="numbers" on="on" p="p">{
nom = f (i) ;
denom = f (i+1) ;

if(MAXif(MAX
//printf ("\ni=%llu %llu / %llu",i,nom,denom) ;
//fprintf (pf,"\ni=%llu %llu / %llu",i,nom,denom) ;
}

printf ("\nMAX= %llu", MAX) ;

getch () ;

fclose(pf);

return 1;
}


Reference:
http://www.cut-the-knot.org/blue/Fusc.shtml