Decomposition_Powers_Three

I am trying to solve a problem.

Write an algorithm, called Decomposition_Powers_Three, which produces the decomposition of each integer using powers of 3, namely 1, 3, 9, 27, and 81, and the + and – operators. Each power of 3 should appear at most once in the decomposition.

Examples:
1 = 1
2 = 3 – 1
3 = 3
4 = 3 + 1
5 = 9 – 3 – 1
7 = 9 – 3 + 1
14 = 27 – 9 – 3 – 1
28 = 27 + 1
43 = 81 – 27 – 9 – 3 + 1
121 = 81 + 27 + 9 + 3 + 1

I just want to know how can I start and what should I use to solve this problem.
Last edited on
This kind of looks like a homework problem so I won't give you the code but I will point you in the correct direction as this is a great problem for intuitive thinking.

You can solve this problem with the following operator: %

This is the modulus operator. If you don't know already, it returns the remainder of a division problem. For example:

30 % 4 = 2 (4*7 = 28 remainder = 2).

With this information, you should be able to solve this algorithm. Good luck!
Topic archived. No new replies allowed.