6 kyu

Expressing Integers as Sum of Powers of Three

125 of 161CheeSenTan

Description:

Problem

All integers can be uniquely expressed as a sum of powers of 3 using each power of 3 at most once.

For example:

17 = (-1) +   0  + (-9) + 27 = (-1 * 3^0) + ( 0 * 3^1) + (-1 * 3^2) + (1 * 3^3)
-8 =   1  +   0  + (-9)      = ( 1 * 3^0) + ( 0 * 3^1) + (-1 * 3^2)
25 =   1  + (-3) +   0  + 27 = ( 1 * 3^0) + (-1 * 3^1) + ( 0 * 3^2) + (1 * 3^3)

We can use the string +-0+ to represent 25 as the sum of powers of 3:

Symbols      :   "+"   "-"   "0"   "+"
Powers of 3  :    1     3     9    27
Values       :    1    -3     0    27

Given an integer n (not necessarily strictly positive), we want to write a function that expresses n as a sum of powers of 3 using the symbols -0+:

n = 17 -> "-0-+"
n = -8 -> "+0-"

Note: The last symbol in the solution string represents the largest power of 3 used (added + or subtracted -) and will never be 0, except if the integer is 0 itself.

Fundamentals

More By Author:

Check out these other kata created by CheeSenTan

Stats:

CreatedJan 28, 2021
PublishedJan 28, 2021
Warriors Trained1062
Total Skips35
Total Code Submissions973
Total Times Completed161
Python Completions125
Haskell Completions10
JavaScript Completions35
Lua Completions7
Total Stars28
% of votes with a positive feedback rating88% of 51
Total "Very Satisfied" Votes42
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes3
Total Rank Assessments8
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • CheeSenTan Avatar
  • JohanWiltink Avatar
  • monadius Avatar
  • dolamroth Avatar
  • Pnd72 Avatar
  • dfhwze Avatar
  • metatable Avatar
Ad