Jump to content

Minkowski question-mark function: Difference between revisions

Added 11l
m (added whitespace.)
(Added 11l)
Line 26:
 
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>-V MAXITER = 151
 
F minkowski(x) -> Float
I x > 1 | x < 0
R floor(x) + minkowski(x - floor(x))
 
V p = Int(x)
V q = 1
V r = p + 1
V s = 1
V d = 1.0
V y = Float(p)
 
L
d /= 2
I y + d == y
L.break
 
V m = p + r
I m < 0 | p < 0
L.break
 
V n = q + s
I n < 0
L.break
 
I x < Float(m) / n
r = m
s = n
E
y += d
p = m
q = n
 
R y + d
 
F minkowski_inv(=x) -> Float
I x > 1 | x < 0
R floor(x) + minkowski_inv(x - floor(x))
 
I x == 1 | x == 0
R x
 
V cont_frac = [0]
V current = 0
V count = 1
V i = 0
 
L
x *= 2
 
I current == 0
I x < 1
count++
E
cont_frac.append(0)
cont_frac[i] = count
 
i++
count = 1
current = 1
x--
E
I x > 1
count++
x--
E
cont_frac.append(0)
cont_frac[i] = count
 
i++
count = 1
current = 0
 
I x == floor(x)
cont_frac[i] = count
L.break
 
I i == :MAXITER
L.break
 
V ret = 1.0 / cont_frac[i]
L(j) (i-1 .. 0).step(-1)
ret = cont_frac[j] + 1.0 / ret
 
R 1.0 / ret
 
print(‘#2.16 #2.16’.format(minkowski(0.5 * (1 + sqrt(5))), 5.0 / 3.0))
print(‘#2.16 #2.16’.format(minkowski_inv(-5.0 / 9.0), (sqrt(13) - 7) / 6))
print(‘#2.16 #2.16’.format(minkowski(minkowski_inv(0.718281828)), minkowski_inv(minkowski(0.1213141516171819))))</lang>
 
{{out}}
<pre>
1.6666666666696983 1.6666666666666667
-0.5657414540893350 -0.5657414540893351
0.7182818280000091 0.1213141516171819
</pre>
 
=={{header|Factor}}==
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.