Problem 1288. Balanced Ternary Numbers: Easy as |, |-, |o
This problem concerns the so-called balanced ternary system for representing numbers. It is a Base 3 system in which the digits can be 1, 0, or -1.
In balanced ternary, the number 8 would be represented as 9 (or 3^2) minus 1 (or 3^0). Typographically we will use "|" for one, "o" for zero (that's a lower-case O), and "-" for negative one. So if the decimal input d is the number 8, the balanced ternary output is the string "|o-". Thus
dec 8 => bt "|o-"
Here are some more examples.
dec 3 => bt "|o" dec 15 => bt "|--o" dec 52 => bt "|-o-|"
Given an integer d, return the string bt. Leading zeros should always be suppressed.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers55
Suggested Problems
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15609 Solvers
-
Project Euler: Problem 7, Nth prime
1565 Solvers
-
Split a string into chunks of specified length
1878 Solvers
-
Calculate the Number of Sign Changes in a Row Vector (No Element Is Zero)
767 Solvers
-
10001 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!