### Decomposition Powers Three

So I am trying to write a program that produces the decomposition of an integer using powers of three which are: 1, 3, 9, 27, and 81.

example:

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

Seems like you'll need a variable for 1 and 3 to start. Those are your main variables and you can derive any number with those two.

What is this program trying to accomplish, exactly?

Is it suppose to include all real values that can be decomposed by the power of three, or only up until 121?
this program basically asks the user to input an integer. Then, the program should decompose that integer into integers which are powers of 3. for example:

7 can be decomposed to 9 - 3 + 1

Last edited on
I had something really long typed out... Then my browser crashed and lost it..Anyways it was something along the ling of you should look for values which are closest and work with them and not pick random values.

short example
Notes: equal or less = add next
greater = subtract next

123
closest is 81 (it is less so we add) current result: 81
distance = 123 - 81 = 42
closest is 27 (it is less so we add) current result: 81 + 27 = 108
distance = 42 - 27 = 15
closest is 9(it is less so we add) current result: 81 + 27 + 9 = 117
distance = 15 - 9 = 6
closest is 3(it is less so we add, we choose smallest of the closest to avoid subtracting) current result: 81 + 27 + 9 + 3 = 120
distance = 6 - 3 = 3
closest is 3(it is equal so we add) current result: 81 + 27 + 9 + 3 + 3 = 123

Coding this shouldn't be too difficult.

*eidt when I say add/subtract I mainly mean flip the next operation (starting with add)

60
closest = 81 current result: 81 -
distance = 21
closet = 27 current result: 81 - 27 +
distance = 6
closet = 3 current result: 81 - 27 + 3 +
distance = 3
closest = 3 current result: 81 - 27 + 3 + 3

so lets look at it now:
81 - 27 = 54
54 + 3 = 57
57 + 3 = 60

Last edited on
What kind of structures can I use to code this? and is it necessary to use modulus operator: % to accomplish this?

Edit: Sorry but I still don't understand how will I use the information you put and code it. I just don't understand the concept I guess. Can you explain it differently? Sorry for holding you up. And I really really appreciate your help!
Last edited on
You should be fine with a container for the factors of 3, and then a string for the result. I will write up an example but might take a few mins.
It's not very clean and might have a few bugs but I put this together pretty quickly so sorry ahead of time. Hopefully you can use some of this to work on yours. Please don't just copy paste it would be nice if you learned from it.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200`` ``````#include #include #include #include #include int main() { int result; std::cout << "Please enter the desired result: "; std::cin >> result; std::vector const table = {1, 3, 9, 27, 81}; //set might be better but oh well std::string answer = ""; int temp = result; bool add = true; while(temp) { int distance = std::abs(temp - 1); //you can get absolute manually really easy int closest = 1; for(std::size_t i = 1; i < table.size(); ++i) //might be able to get away with less iterations but its only 4 so no big deal { int tempDistance = std::abs(temp - table[i]); if(distance && distance > tempDistance) //make sure we didn't find a perfect match and its decreasing distance { distance = tempDistance; closest = table[i]; } else { break; //getting farther away or we found a match no need to continue } } if(closest > temp) { add = !add; } temp = distance; if(temp) //the last number finished equation so don't putt sign at end { std::ostringstream ss; ss << closest; answer += ss.str(); answer += add ? '+' : '-'; } else { std::ostringstream ss; ss << closest; answer += ss.str(); } } std::cout << result << " = " << answer << std::endl; }`````` ```1 = 1 2 = 1+1 3 = 3 4 = 3+1 5 = 3+1+1 6 = 3+3 7 = 9-1-1 8 = 9-1 9 = 9 10 = 9+1 11 = 9+1+1 12 = 9+3 13 = 9+3+1 14 = 9+3+1+1 15 = 9+3+3 16 = 9+9-1-1 17 = 9+9-1 18 = 9+9 19 = 27-9+1 20 = 27-9+1+1 21 = 27-3-3 22 = 27-3-1-1 23 = 27-3-1 24 = 27-3 25 = 27-1-1 26 = 27-1 27 = 27 28 = 27+1 29 = 27+1+1 30 = 27+3 31 = 27+3+1 32 = 27+3+1+1 33 = 27+3+3 34 = 27+9-1-1 35 = 27+9-1 36 = 27+9 37 = 27+9+1 38 = 27+9+1+1 39 = 27+9+3 40 = 27+9+3+1 41 = 27+9+3+1+1 42 = 27+9+3+3 43 = 27+9+9-1-1 44 = 27+9+9-1 45 = 27+9+9 46 = 27+27-9+1 47 = 27+27-9+1+1 48 = 27+27-3-3 49 = 27+27-3-1-1 50 = 27+27-3-1 51 = 27+27-3 52 = 27+27-1-1 53 = 27+27-1 54 = 27+27 55 = 81-27+1 56 = 81-27+1+1 57 = 81-27+3 58 = 81-27+3+1 59 = 81-27+3+1+1 60 = 81-27+3+3 61 = 81-27+9-1-1 62 = 81-27+9-1 63 = 81-9-9 64 = 81-9-9+1 65 = 81-9-9+1+1 66 = 81-9-3-3 67 = 81-9-3-1-1 68 = 81-9-3-1 69 = 81-9-3 70 = 81-9-1-1 71 = 81-9-1 72 = 81-9 73 = 81-9+1 74 = 81-9+1+1 75 = 81-3-3 76 = 81-3-1-1 77 = 81-3-1 78 = 81-3 79 = 81-1-1 80 = 81-1 81 = 81 82 = 81+1 83 = 81+1+1 84 = 81+3 85 = 81+3+1 86 = 81+3+1+1 87 = 81+3+3 88 = 81+9-1-1 89 = 81+9-1 90 = 81+9 91 = 81+9+1 92 = 81+9+1+1 93 = 81+9+3 94 = 81+9+3+1 95 = 81+9+3+1+1 96 = 81+9+3+3 97 = 81+9+9-1-1 98 = 81+9+9-1 99 = 81+9+9 100 = 81+27-9+1 101 = 81+27-9+1+1 102 = 81+27-3-3 103 = 81+27-3-1-1 104 = 81+27-3-1 105 = 81+27-3 106 = 81+27-1-1 107 = 81+27-1 108 = 81+27 109 = 81+27+1 110 = 81+27+1+1 111 = 81+27+3 112 = 81+27+3+1 113 = 81+27+3+1+1 114 = 81+27+3+3 115 = 81+27+9-1-1 116 = 81+27+9-1 117 = 81+27+9 118 = 81+27+9+1 119 = 81+27+9+1+1 120 = 81+27+9+3 121 = 81+27+9+3+1 122 = 81+27+9+3+1+1 123 = 81+27+9+3+3 124 = 81+27+9+9-1-1 125 = 81+27+9+9-1 126 = 81+27+9+9 127 = 81+27+27-9+1 128 = 81+27+27-9+1+1 129 = 81+27+27-3-3 130 = 81+27+27-3-1-1 131 = 81+27+27-3-1 132 = 81+27+27-3 133 = 81+27+27-1-1 134 = 81+27+27-1 135 = 81+27+27 136 = 81+81-27+1 137 = 81+81-27+1+1 138 = 81+81-27+3 139 = 81+81-27+3+1 140 = 81+81-27+3+1+1 141 = 81+81-27+3+3 142 = 81+81-27+9-1-1 143 = 81+81-27+9-1 144 = 81+81-9-9 145 = 81+81-9-9+1 146 = 81+81-9-9+1+1 147 = 81+81-9-3-3 148 = 81+81-9-3-1-1 149 = 81+81-9-3-1 150 = 81+81-9-3 151 = 81+81-9-1-1 152 = 81+81-9-1 153 = 81+81-9 154 = 81+81-9+1 155 = 81+81-9+1+1 156 = 81+81-3-3 157 = 81+81-3-1-1 158 = 81+81-3-1 159 = 81+81-3 160 = 81+81-1-1 161 = 81+81-1 162 = 81+81 163 = 81+81+1 164 = 81+81+1+1 165 = 81+81+3 166 = 81+81+3+1 167 = 81+81+3+1+1 168 = 81+81+3+3 169 = 81+81+9-1-1 170 = 81+81+9-1 171 = 81+81+9 172 = 81+81+9+1 173 = 81+81+9+1+1 174 = 81+81+9+3 175 = 81+81+9+3+1 176 = 81+81+9+3+1+1 177 = 81+81+9+3+3 178 = 81+81+9+9-1-1 179 = 81+81+9+9-1 180 = 81+81+9+9 181 = 81+81+27-9+1 182 = 81+81+27-9+1+1 183 = 81+81+27-3-3 184 = 81+81+27-3-1-1 185 = 81+81+27-3-1 186 = 81+81+27-3 187 = 81+81+27-1-1 188 = 81+81+27-1 189 = 81+81+27 190 = 81+81+27+1 191 = 81+81+27+1+1 192 = 81+81+27+3 193 = 81+81+27+3+1 194 = 81+81+27+3+1+1 195 = 81+81+27+3+3 196 = 81+81+27+9-1-1 197 = 81+81+27+9-1 198 = 81+81+27+9 199 = 81+81+27+9+1 200 = 81+81+27+9+1+1```
Last edited on
I really appreciate your help! I will definitely look at it and try to learn from it.
I had to do a similar program last semester but had to account for -121 to -1 too. How would you account from starting with an integer from the negative side?
You may want to create your own thread. Anyways, It would pretty much be the same logic but instead of adding up to a positive number you are now adding up to a negative.
Topic archived. No new replies allowed.