[ prog / sol / mona ]

prog


e and the Stern-Brocot tree

14 2021-02-10 12:46

>>13
What is the characterization of the subset of real numbers whose SB string expansion can be recognized by a FSM?

class SBtree:
   @ staticmethod
   def string (x):
      while x > 0:
         if x < 1:
            yield 'L'
            x = x / (1 - x)
         else:
            yield 'R'
            x = x - 1

   @ staticmethod
   def nstring (x, n):
      return ''.join (itertools.islice (SBtree.string (x), n))


>>> sb.SBtree.nstring (math.sqrt (2), 32)
'RLLRRLLRRLLRRLLRRLLRRLLRRLLRRLLR'
>>> sb.SBtree.nstring (math.sqrt (3), 32)
'RLRRLRRLRRLRRLRRLRRLRRLRRLRRLRRL'
>>> sb.SBtree.nstring (math.sqrt (5), 32)
'RRLLLLRRRRLLLLRRRRLLLLRRRRLLLLRR'
>>> sb.SBtree.nstring (math.sqrt (7), 32)
'RRLRLRRRRLRLRRRRLRLRRRRLRLRRRRLR'
>>> sb.SBtree.nstring ((1 + math.sqrt (5)) / 2, 32)
'RLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL'
>>> sb.SBtree.nstring (math.e, 32)
'RRLRRLRLLLLRLRRRRRRLRLLLLLLLLRLR'
>>> sb.SBtree.nstring (math.pi, 32)
'RRRLLLLLLLRRRRRRRRRRRRRRRLRRRRRR'
54


VIP:

do not edit these