Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 25th, 2013, 08:29 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Ternary Digits

Suppose a new computer uses radix-3 (ternary) logic instead of radix-2 (binary) logic to represent unsigned integers.

What is the largest value that can be represented by 6 ternary digits?

#2
September 25th, 2013, 09:26 AM
 ManiacDan
Sarcky

Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 10,499
Time spent in forums: 2 Months 3 Weeks 5 Days 8 h 27 m 58 sec
Reputation Power: 6300
3^6
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

#3
September 26th, 2013, 03:10 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
If I were to convert the decimal 47 to radix-3, do I divide it repeatedly by 3?

#4
September 26th, 2013, 09:31 AM
 ManiacDan
Sarcky

Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 10,499
Time spent in forums: 2 Months 3 Weeks 5 Days 8 h 27 m 58 sec
Reputation Power: 6300
No. To convert something to another base, you must deal with powers. Each digit represents a power of the base. In decimal, the first (right most) digit is 10^0 times the value of the number. The second digit is 10^1 times the value of the digit. So in base 10:
12 = ((10^0)*2)+((10^1)*1)

For binary it's the same rule:
1101 = ((2^0)*1)+((2^1)*0)+((2^2)*1)+((2^3)*1) = 13

And finally, for trinary you'd calculate it the same way:
2101 = ((3^0)*1)+((3^1)*0)+((3^2)*1)+((3^3)*2) = 64

To get the answer in the other way, work backwards. Start with figuring out how many digits there will be. The highest number you can represent with 3 digits of trinary is 222, which (by our rules above) is equal to 26, so you need 4 digits. Now, work through those digits largest-to-smallest:
3^3 times WHAT is less than or equal to 47? 1. The first digit is 1. Subtract ((3^3)*1) from your number and move on.
3^2 times WHAT is less than 20? 2. The second digit is 2.
3^1 times WHAT is less than 2? Nothing, 3 > 2. The third digit is 0.
3^0 times WHAT is less than or equal to 2. 2, obviously.

47 in trinary is 1202.

1202 = ((3^0)*2)+((3^1)*0)+((3^2)*2)+((3^3)*1) = 47

#5
September 26th, 2013, 11:10 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Question 1:
3^6 = 729
I just want to confirm if the largest value that can be represented by 6 ternary digits is 729 or 728 (including 0)?

Question 2:
I seem to have arrived to (1202) base 3 by repeated division and taking the remainders.
Code:
http://s23.postimg.org/wm9vfhgpn/DSC_0170.jpg

We are taught the same way for binary, octal, and hex -- dividing the decimal by radix until reaching 0 for the quotient then writing the remainders bottom-up.

Thank you ManiacDan for your assistance.

#6
September 26th, 2013, 11:17 AM
 ManiacDan
Sarcky

Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 10,499
Time spent in forums: 2 Months 3 Weeks 5 Days 8 h 27 m 58 sec
Reputation Power: 6300
Sorry, I wasn't QUITE right before. The largest number represented by 6 ternary digits is ((3^6)-1), much like for 6 binary digits is ((2^6)-1).

Short proof, the largest possible number with 6 digits:
((3^0)*2)+((3^1)*2)+((3^2)*2)+((3^3)*2)+((3^4)*2)+((3^5)*2) = 728.

You don't "include zero" for determining the largest number. You can represent 729 numbers, the highest of which is 728.

The repeated-division method works as well, I prefer not to do it that way because it's a bit of a cheat. You're using a trick in base-10 math that happens to work out properly for base-3 math. Why not just do it in base-3? (It's slower my way unless you think in that base commonly or are able to do the math in your head without going crazy)

#7
September 27th, 2013, 04:07 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Like in my other thread where 2^5 = 32, the signed decimal range would be -16 to +15, which is 32 integers "including 0". I am thinking in the algebra/real number line concept, but it is good to see the mathematical proof behind it.

I tried with another odd number, radix-7 and the repeated division worked well enough.
(158) base 10 = (314) base 7

If it worked on radix-3 ONLY, then it is indeed sort of a "cheat" for radix-3.

If it is a solution that works consistently on both odd and even bases, I guess it is better appropriated as an "alternative method" for converting decimal to any radix-x.

Well, there goes my two cents.

Thank you for showing me the power method though. It will be a useful reference and good way to double-check things.

#8
September 27th, 2013, 05:41 AM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,106
Time spent in forums: 2 Months 3 Weeks 4 Days 13 h 33 m 35 sec
Reputation Power: 1063
Hi,

the repeated division is the standard algorithm for converting bases. In Germany, we call it "reverse Horner's scheme", but there may be different names.

No idea why ManiacDan calls it a "cheat" and claim that it only works for base 10. It works for any two bases.

If you wanted to, say, get the base 5 representation of 1526(7), you'd divide 1526(7) by 5 (in base 7). The remainder is the last digit of the base 5 representation. And the result is again divided by 5 etc.

This is very simple and quick. The "alternative" described by ManiacDan may work for small numbers, but it's completely inappropriate for bigger numbers or actual algorithm. Or would you tell me real quick what's the next biggest power of 42 for 52398257916?

Last edited by Jacques1 : September 27th, 2013 at 05:44 AM.

#9
September 27th, 2013, 09:05 AM
 kathy025
Registered User

Join Date: Jul 2013
Location: Manila, Philippines
Posts: 24
Time spent in forums: 7 h 43 m 34 sec
Reputation Power: 0
Quote:
 Originally Posted by Jacques1 It works for any two bases. If you wanted to, say, get the base 5 representation of 1526(7), you'd divide 1526(7) by 5 (in base 7).

Thank you. This is good to know. So far, I have only tried it for base 10.

 Viewing: Dev Shed Forums > Other > Beginner Programming > Ternary Digits