OFFSET
1,2
LINKS
FORMULA
canonical_fractions_to_whole_SternBrocot_permutation(30);
EXAMPLE
Whole Stern-Brocot tree: 1/1 1/2 2/1 1/3 2/3 3/2 3/1 1/4 2/5 3/5 3/4 4/3 5/3 5/2 4/1 1/5 2/7
Canonical fractions: 1/1 1/2 2/1 1/3 3/1 1/4 2/3 3/2 4/1 1/5 5/1 1/6 2/5 3/4 4/3 5/2 6/1
MAPLE
cfrac2binexp := proc(c) local i, e, n; n := 0; for i from 1 to nops(c) do e := c[i]; if(i = nops(c)) then e := e-1; fi; n := ((2^e)*n) + ((i mod 2)*((2^e)-1)); od; RETURN(n); end;
frac2position_in_whole_SB_tree := proc(r) local k, msb; if(1 = r) then RETURN(1); else if(r > 1) then k := cfrac2binexp(convert(r, confrac)); else k := ReflectBinTreePermutation(cfrac2binexp(convert(1/r, confrac))); fi; msb := floor_log_2(k); if(r > 1) then RETURN(k + (2^(msb+1))); else RETURN(k + (2^(msb+1)) - (2^msb)); fi; fi; end;
canonical_fractions_to_whole_SternBrocot_permutation := proc(u) local a, n, i; a := []; for n from 2 to u do for i from 1 to n-1 do if (1 = igcd(n, i)) then a := [op(a), frac2position_in_whole_SB_tree(i/(n-i))]; fi; od; od; RETURN(a); end; # ReflectBinTreePermutation and floor_log_2 given in A054429
CROSSREFS
KEYWORD
nonn
AUTHOR
Antti Karttunen
STATUS
approved