This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,12 @@
trigits=: 1+3 <.@^. 2 * 1&>.@|
trinOfN=: |.@((_1 + ] #: #.&1@] + [) #&3@trigits) :. nOfTrin
nOfTrin=: p.&3 :. trinOfN
trinOfStr=: 0 1 _1 {~ '0+-'&i.@|. :. strOfTrin
strOfTrin=: {&'0+-'@|. :. trinOfStr
carry=: +//.@:(trinOfN"0)^:_
trimLead0=: (}.~ i.&1@:~:&0)&.|.
add=: carry@(+/@,:)
neg=: -
mul=: trimLead0@carry@(+//.@(*/))

View file

@ -0,0 +1,3 @@
a=: trinOfStr '+-0++0+'
b=: trinOfN -436
c=: trinOfStr '+-++-'

View file

@ -0,0 +1,7 @@
nOfTrin&> a;b;c
523 _436 65
strOfTrin a mul b (add -) c
----0+--0++0
nOfTrin a mul b (add -) c
_262023

View file

@ -0,0 +1,22 @@
frombt = FromDigits[StringCases[#, {"+" -> 1, "-" -> -1, "0" -> 0}],
3] &;
tobt = If[Quotient[#, 3, -1] == 0,
"", #0@Quotient[#, 3, -1]] <> (Mod[#,
3, -1] /. {1 -> "+", -1 -> "-", 0 -> "0"}) &;
btnegate = StringReplace[#, {"+" -> "-", "-" -> "+"}] &;
btadd = StringReplace[
StringJoin[
Fold[Sort@{#1[[1]],
Sequence @@ #2} /. {{x_, x_, x_} :> {x,
"0" <> #1[[2]]}, {"-", "+", x_} | {x_, "-", "+"} | {x_,
"0", "0"} :> {"0", x <> #1[[2]]}, {"+", "+", "0"} -> {"+",
"-" <> #1[[2]]}, {"-", "-", "0"} -> {"-",
"+" <> #1[[2]]}} &, {"0", ""},
Reverse@Transpose@PadLeft[Characters /@ {#1, #2}] /. {0 ->
"0"}]], StartOfString ~~ "0" .. ~~ x__ :> x] &;
btsubtract = btadd[#1, btnegate@#2] &;
btmultiply =
btadd[Switch[StringTake[#2, -1], "0", "0", "+", #1, "-",
btnegate@#1],
If[StringLength@#2 == 1,
"0", #0[#1, StringDrop[#2, -1]] <> "0"]] &;

View file

@ -0,0 +1,4 @@
frombt[a = "+-0++0+"]
b = tobt@-436
frombt[c = "+-++-"]
btmultiply[a, btsubtract[b, c]]