Balanced ternary: Difference between revisions
Content added Content deleted
(Added Julia language) |
m (→{{header|REXX}}: added/changed comments and whitespace, used a template for the OUTPUTs, optimized a function.) |
||
Line 4,051: | Line 4,051: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The REXX program could be optimized by using (procedure) with '''expose''' and having the <big>'''$.'''</big> and <big>'''@.'''</big> variables set only once. |
The REXX program could be optimized by using (procedure) with '''expose''' and having the <big>'''$.'''</big> and <big>'''@.'''</big> variables set only once. |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX program converts decimal ◄───► balanced ternary; it also performs arithmetic. */ |
||
numeric digits 10000 /*be able to handle gihugic numbers. */ |
numeric digits 10000 /*be able to handle gihugic numbers. */ |
||
Ao = '+-0++0+' ; Abt = Ao /* [↓] 2 literals used by subroutine*/ |
Ao = '+-0++0+' ; Abt = Ao /* [↓] 2 literals used by subroutine*/ |
||
Bo = '-436' ; Bbt = d2bt(Bo) |
Bo = '-436' ; Bbt = d2bt(Bo); @ = '(decimal)' |
||
Co = '+-++-' ; Cbt = Co |
Co = '+-++-' ; Cbt = Co ; @@ = 'balanced ternary =' |
||
call btShow '[a]', Abt |
call btShow '[a]', Abt |
||
call btShow '[b]', Bbt |
call btShow '[b]', Bbt |
||
call btShow '[c]', Cbt |
call btShow '[c]', Cbt |
||
say; $bt = btMul(Abt,btSub(Bbt,Cbt)) |
say; $bt = btMul(Abt, btSub(Bbt, Cbt) ) |
||
call btShow '[a*(b-c)]', $bt |
call btShow '[a*(b-c)]', $bt |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
d2bt: procedure; parse arg x 1; p=0; $.='-'; $.1='+'; $.0=0; #= |
d2bt: procedure; parse arg x 1; p=0; $.='-'; $.1='+'; $.0=0; #= |
||
x=x/1 |
x=x / 1 |
||
do until x==0; |
do until x==0; _= (x // (3** (p+1) ) ) % 3**p |
||
if _== |
if _== 2 then _= -1; else if _== -2 then _=1 |
||
x=x - _ * (3**p); p=p+1; #=$._ || # |
|||
end /*until*/; return # |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
end /*until ···*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return # |
|||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
x_=substr(rx, j, 1); xn=@.x_ |
|||
y_=substr(ry, j, 1); yn=@.y_ |
|||
⚫ | |||
s=xn + yn + carry; carry= 0 |
|||
if s== 2 then do; s=-1; carry= 1; end |
|||
if s== 3 then do; s= 0; carry= 1; end |
|||
if s==-2 then do; s= 1; carry=-1; end |
|||
#=$.s || # |
|||
end /*j*/ |
|||
if carry\==0 then #=$.carry || #; return btNorm(#) |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
btMul: procedure; parse arg x 1 x1 2, y 1 y1 2; if x==0 | y==0 then return 0 |
|||
S=1; x=btNorm(x); y=btNorm(y) /*handle: 0-xxx values.*/ |
|||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
if y1=='-' then do; y=btNeg(y); S=-S; end /* " " " */ |
|||
btMul: procedure; parse arg x 1 x1 2, y 1 y1 2; if x==0 | y==0 then return 0 |
|||
if length(y)>length(x) then parse value x y with y x /*optimize " " */ |
|||
⚫ | |||
⚫ | |||
do until y==0 /*keep adding 'til done*/ |
|||
P=btAdd(P, x) /*multiple the hard way*/ |
|||
y=btSub(y, '+') /*subtract 1 from Y.*/ |
|||
P=0 |
|||
end /*until*/ |
|||
if S==-1 then P=btNeg(P); return P /*adjust the product sign; return. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
btNeg: return translate(arg(1), '-+', "+-") /*negate bal_ternary #.*/ |
|||
⚫ | |||
if S==-1 then P=btNeg(P) /*adjust product sign. */ |
|||
btSub: return btAdd(arg(1), btNeg(arg(2))) /*subtract two BT args.*/ |
|||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
btNeg: return translate(arg(1), '-+', "+-") /*negate the bal_tern #*/ |
|||
⚫ | |||
btSub: return btAdd(arg(1), btNeg(arg(2))) /*subtract two BT args.*/ |
|||
⚫ | |||
⚫ | |||
<pre> |
<pre> |
||
[a] +-0++0+ balanced ternary = 523 (decimal) |
[a] +-0++0+ balanced ternary = 523 (decimal) |