Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on When The Ternary Is Balance

Post

When The Ternary Is Balance

+4
−0

Inspired by this Rosetta Code article.

Introduction

Balanced Ternary is a method of representing integers using -1, 0 and 1 in base 3.

Decimal 11 = (1 * 32) + (1 * 31) + (−1 * 30) = [1,1,-1] or "++-"

Decimal 6 = (1 * 32) + (−1 * 31) + (0 * 30) = [1,-1,0] or "+-0"

Task

Given any integer, you must give its balanced ternary representation in any permitted format (array, string, etc.)

Your program must support all integers present within its maximum integer limit.

Test Cases

Reference implementation with first 200 values

5 +--
6 +-0
11 ++-
65 +-++-
-44 -++0+
-540 -+-+000

Scoring

This is code-golf. Shortest answer in each language wins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Can I return a number instead of a string for the zero case? (2 comments)
Are leading zeros in the output allowed? (2 comments)
Are leading zeros in the output allowed?
celtschk‭ wrote about 3 years ago

Are leading zeros in the output allowed?

Razetime‭ wrote about 3 years ago

leading zeroes are not allowed in the output.