Decompose int to powers of 3

111111111111111111111111
Last edited on
Keep the code that does the positive stuff and throw out the code that does the negative stuff.

Now, the only difference between, say -100 and 100 is that one is multiplied by -1, right?

The same applies to the resulting sequence:

100 == 81 + 27 - 9 + 1

(-1)100 == (-1)(81 + 27 - 9 + 1)

-100 == -81 - 27 + 9 - 1

So, when you start, check to see if the number is < 0 (meaning that it is negative). (Make sure to remember that in a variable somewhere.) If it is, negate the number (making it positive), run it through the algorithm, and remember that every time you want to put a '+' make it '-' instead, and every time you want to put a '-' put a '+' instead.

Hope this helps.
@Duoas,

Thanks for the help.

But I tried to find where exactly my code works with a negative value and I tried to change it but it didn't work.

Can you please see where exactly my code works with negative values? The way I built it is not just dealing with positive or negative integer. I guess it's more complicated than this.

Last edited on
Your code doesn't work with negative values -- that's the problem.

But, realize that the answer for a negative value is the same as an answer for a positive value, only with all the signs reversed.

So you only need the code for positive values.

If you are given a negative value, negate it, then when you output the results, negate every result.

Hope this helps.
Topic archived. No new replies allowed.