Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1875
Members Online:
3
Guests Online:
67

Total Post History
Posts:
81446
Topics:
18714

7-Day Post History
New Posts:
0
New Topics:
0
Active Topics:
0

Our newest member
konam534As

Other

FAQ
All times are UTC [ DST ]

Google Ads

balanced ternary

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

balanced ternary

Postby phillip1882 on Mon Mar 21, 2011 5:01 pm

i've always been facinated by the ternary number system, particularly the balanced tenary version. your task is to covert a binary number to balanced ternary. (symbols are +,0,-)
for extra challange, write add, subract, multiply and divide functions for balanced ternary numbers.
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida
Unrated

Re: balanced ternary

Postby Thirster42 on Mon Mar 21, 2011 5:14 pm

perhaps you should provide an example and description of what balanced ternary is?
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: balanced ternary

Postby SQLDenis on Mon Mar 21, 2011 5:22 pm

See here http://en.wikipedia.org/wiki/Ternary_numeral_system

I must admit that I never heard of this till today
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: balanced ternary

Postby phillip1882 on Mon Mar 21, 2011 5:29 pm

++00- would be 3^4 +3^3 +0*3^2 +0*3^1 -3^0 = 81 +27 -1 = 107
+--0+ would be 3^4 -3^3 -3^2 +1 = 81 -27 -9 +1 = 46
the common method to covert is the following.
first convert to standard ternary.
46 | 3
15 r 1
5 r 0
1 r 2
0 r 1

1201. then to convert to balanced ternary, make each 1 a +, and make each 2 a - and carry over a +.
+20+
+--0+
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida
Unrated

Re: balanced ternary

Postby Emtucifor on Thu Mar 24, 2011 4:12 am

I've always loved playing around with alternate bases. I particularly like working with fractional values.

+.+ would thus be 4/3. :)

I am working up a version of this in html and javascript. It will be a full-functioning utility web page of the type you find online when you search for things like "base64 converter" and such. You can supply your own digits or accept the default (0-9A-Z). You'll be able to put in any base you want, from 2 to unlimited (though for bases greater than 36 you must provide an explicit digits list).

At first it will be a simple single panel display of the result, but I'd like to come up with a calculator + command-prompt style output, an "adding tape", if you will, scrolling upward. Including values between 0 and 1 should be pretty easy, though I will be representing them after a "ternary" (or other) point rather than trying to show the nearest reduced fraction. Underflows in the display will just be a fact of life, internally it should calculate fairly well, though I'm sure I could get in over my head trying to duplicate all the functionality of float...

I'm using javascript because that language has been bothering me for a while and I finally sat down and worked through exactly how functions, objects, new objects (created ala `new Fn()`), prototypes, constructors, and the 'this' reference work. Now I want to put my new knowledge to work!

For what it's worth you can do number math in any base you want: ternary, quaternary, quinary, senary ... See this page which will, if I finish, be far from "the best base number converter".

P.S. I just realized I had not fully grasped the significance of balanced ternary, which is more than just a replacement set of digits for 0, 1, 2. But I still intend the same thing. :)
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: balanced ternary

Postby Thirster42 on Thu Mar 24, 2011 3:01 pm

phillip1882 wrote:++00- would be 3^4 +3^3 +0*3^2 +0*3^1 -3^0 = 81 +27 -1 = 107
+--0+ would be 3^4 -3^3 -3^2 +1 = 81 -27 -9 +1 = 46
the common method to covert is the following.
first convert to standard ternary.
46 | 3
15 r 1
5 r 0
1 r 2
0 r 1

1201. then to convert to balanced ternary, make each 1 a +, and make each 2 a - and carry over a +.
+20+
+--0+



not quite following your example. i get the replacing the 1 and 2, but what do you mean by carry over a +? without "carrying over the +" i end up with +0-+
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: balanced ternary

Postby phillip1882 on Thu Mar 24, 2011 3:13 pm

in balanced ternary, + plus + = +-.
so 111222 converted to balanced ternary would be...
+++222
+++23-
+++30-
++200-
+2-00-
2--00-
+---00-

try adding the two balanced ternary numbers
  1.    ++-+00-
  2. +  +-+0-+-
  3. ----------
  4.  

Code is hidden, SHOW
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida
Unrated

Re: balanced ternary

Postby chrissie1 on Thu Mar 24, 2011 3:48 pm

I got the convert to integer thing working

Code is hidden, SHOW


tests to proof my point.

Code is hidden, SHOW


now to get it to go the other way round.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: balanced ternary

Postby chrissie1 on Thu Mar 24, 2011 3:50 pm

and BTW the hide tags have a very limited use, long discussion why that is but not much I can do about it that doesn't take away huge chunks of my sanity. hidecode usually works better for hiding just about anything.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: balanced ternary

Postby chrissie1 on Fri Mar 25, 2011 1:00 pm

0
0
1
+
4
++
13
+++
40
++++
121
+++++
364
++++++
1093
+++++++
3280
++++++++
9841
+++++++++
29524
++++++++++
88573
+++++++++++
265720
++++++++++++
797161
+++++++++++++
2391484
++++++++++++++
7174453
+++++++++++++++
21523360
++++++++++++++++
64570081
+++++++++++++++++
193710244
++++++++++++++++++
581130733
+++++++++++++++++++


This is the code.

Code is hidden, SHOW


And here are the tests I got passing

Code is hidden, SHOW
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium

Re: balanced ternary

Postby chrissie1 on Fri Mar 25, 2011 1:17 pm

Actually instead of using a constant I now use this

Code is hidden, SHOW
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium

Re: balanced ternary

Postby phillip1882 on Mon Mar 28, 2011 7:32 pm

well done crissie!
here's my own version (rather on the inefficent side but works.)
Code is hidden, SHOW


edit: here's a more efficent version that's able to ahndle positive and negative values in a single run.
Code is hidden, SHOW
Last edited by phillip1882 on Mon Mar 28, 2011 9:42 pm, edited 1 time in total.
User avatar
phillip1882
Apprentice
Apprentice
LTD Bronze - Rating: 22
 
Posts: 19
Joined: Mon Mar 14, 2011 11:05 pm
Location: florida
Unrated

Re: balanced ternary

Postby chrissie1 on Mon Mar 28, 2011 7:37 pm

you are allowed to give me points ;-)
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: balanced ternary

Postby Emtucifor on Wed Mar 30, 2011 7:18 am

I worked on my code a bit tonight. I have the arbitrary number base part working, and had an idea about wrapping a normal ternary object to create the balanced ternary one, but there are some weird things about javascript I have to think about and solve before I'll be ready (like how to call a prototype's method using the current object's values).
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033LTD Gold - Rating: 1033
LTD Gold - Rating: 1033
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated