Sturmian word: Difference between revisions

python, rational case
imported>CosmiaNebula
(fibonacci word example)
imported>CosmiaNebula
(python, rational case)
Line 27:
 
where <math>m/n</math> is the first continued fraction approximant to <math>\sqrt a</math> with a denominator <math>n \geq k</math>
 
== [[Python]] ==
For rational numbers:<syntaxhighlight lang="python3">
def sturmian_word(m, n):
sturmian = ""
def invert(string):
return ''.join(list(map(lambda b: {"0":"1", "1":"0"}[b], string)))
if m > n:
return invert(sturmian_word(n, m))
 
k = 1
while True:
current_floor = int(k * m / n)
previous_floor = int((k - 1) * m / n)
if k * m % n == 0:
break
if previous_floor == current_floor:
sturmian += "0"
else:
sturmian += "10"
k += 1
return sturmian
 
</syntaxhighlight>
Checking that it works on the finite Fibonacci word:<syntaxhighlight lang="python3">
def fibWord(n):
Sn_1 = "0"
Sn = "01"
tmp = ""
for i in range(2, n + 1):
tmp = Sn
Sn += Sn_1
Sn_1 = tmp
return Sn
 
fib = fibWord(10)
sturmian = sturmian_word(13, 21)
assert fib[:len(sturmian)] == sturmian
print(sturmian)
 
# Output:
# 01001010010010100101001001010010
</syntaxhighlight>
Anonymous user