Balanced ternary: Difference between revisions

Content added Content deleted
(→‎{{header|11l}}: Static type variables and static methods are supported now)
Line 29: Line 29:
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang="11l">F py_idiv(a, b)
<syntaxhighlight lang="11l">T BalancedTernary
I a >= 0
R a I/ b
R (a - b + 1) I/ b

T BalancedTernary
:str2dig = [‘+’ = 1, ‘-’ = -1, ‘0’ = 0]
:str2dig = [‘+’ = 1, ‘-’ = -1, ‘0’ = 0]
:dig2str = [1 = ‘+’, -1 = ‘-’, 0 = ‘0’]
:dig2str = [1 = ‘+’, -1 = ‘-’, 0 = ‘0’]
Line 51: Line 46:
R [Int]()
R [Int]()
V n3 = ((n % 3) + 3) % 3
V n3 = ((n % 3) + 3) % 3
I n3 == 0 {R [0] [+] .:int2ternary(py_idiv(n, 3))}
I n3 == 0 {R [0] [+] .:int2ternary(n -I/ 3)}
I n3 == 1 {R [1] [+] .:int2ternary(py_idiv(n, 3))}
I n3 == 1 {R [1] [+] .:int2ternary(n -I/ 3)}
I n3 == 2 {R [-1] [+] .:int2ternary(py_idiv((n + 1), 3))}
I n3 == 2 {R [-1] [+] .:int2ternary((n + 1) -I/ 3)}
X RuntimeError(‘’)
X RuntimeError(‘’)