hello everyone

So i have a project to do a code about using these powers of three numbers: 1, 3, 9, 27, and 81 (we dont have to use all and a number of them can only be used up to one time per number being decomposed) and use them to decompose numbers from -121 to 121. I brainstormed everything and discovered some patterns, and there is intervals given for us as hints:

[-121,-41], [-40, -14], [-13, -5], [-4, -2], [-1, -1], [1, 1], [2, 4], [5, 13], [14, 40], and [41, 121].

So if we take 5 till 14 for instance, we notice that 5 decomposes to 9-3-1. 6=9-3. 7=9-3+1. 8=9-1. 9=9. 10=9+1. 11=9+3-1. 12=9+3. 13=9+3+1. and 14=27-9-3-1
notice that 14 starts with 27, the next power of 3 number, yet it just takes the characteristic of the numbers before it (9-3-1). also, if you notice, at 9, the decomposition is just 9. if you look before 9, so from 5-8, and after 9, 10-13, you find that the signs are opposite, but numbers are same. so this proves same for all other intervals (of course different numbers, but for the 14-40 interval, at 27=27, the numbers before 27 will be same as numbers after, except opposite signs.)

so, thats my thinking and breaking of the problem. i know i am on the right track. and i even started the following code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;

    int p3n[10] = { -81, -27, -9, -3, -1, 1, 3, 9, 27, 81 };

    cin >> x;

    int interval_1[2] = { 1, 1 };

    int interval_2[3] = { 2, 3, 4 };

    int interval_3[9] = { 5, 6, 7, 8, 9, 10, 11, 12, 13 };

    int interval_4[27] = { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };

    int interval_5[]

    for (x = 0; x >= -121 || x <= 121; x++) 
    {
        cout << 
    }


    system("pause");
    return 0;
}

so the idea is to grab a number based on the user's input and somehow relate it to the powers of 3 array (p3n). i created arrays for all intervals to make some kind of coding sequence like the one i explained above, but in code. I am still weak at programming, so I hope I provided something that proves I am not here for code, rather, just some help on how to continue. showing me some code will help a lot, i never copy code, but showing me will better make me understand what you might propose as an answer.

also, I have a question: as you see, i am trying to write every single number for each interval in the array elements. the first 3 was easy, but the 2 others get sooooo long! is there a way to make it like for the array that has about 27 elements make it like 14, 15, then ...40 at the end in simple short way, or i have to define every single element?

There are two approaches to this sort of problem (I'm not going to get into details how to do it). One is an iterative computational process - slow when numbers get big. The other is a recursive algorithm where you start with a moderately sized array of the numbers and related values, and use that to leverage the answers to the next set of larger numbers/values. This is a hint - and not simple, but not hard ether! Done properly, the recursive method can be orders of magnitude faster than iterative programming.

FWIW, the programming is the simple part. Understanding the problem is the problem, so to speak! :-)

lol nooo (for ur last fWIW comment, the coding is the hard part and i already figured the problem but i am facing difficulties of how to implement it as a code)

also, if no one wants to help me well, can u at leas answer me the last question i had about arrays? thats the only path in my mind now, u know could just do i and cout for every singe number, but i feel like it can be dobe much simpler like the approach of code i am taking

no

Did you notice that
1 = 1x1
4 = 1x1 + 1x3
13 = 1x1 + 1x3 + 1x9
40 = 1x1 + 1x3 + 1x9 + 1x27
121 = 1x1 + 1x3 + 1x9 + 1x27 + 1x81

commented: o. o not really. we finished the code in a different way (using 10 if statements) but this looked like another intuitive apporoach. cool. +0

o no i didnt o: wow how simple lol. wait but what about other numbers in between? like say 17 or 71. and negatives?

anyways i got 100% on this project 3 weeks ago when i presented it :) but i used the method of the one i described in my first post of the pattern we noticed. i used 10 if statements and a little cout in between each. it worked, but it wasnt perfect as he wanted lol, he wanted us to have it ideally like the program knows this is the number that decomposes, and not have like negative signs in front of decomposition because it looked like it wasnt programmed well.

i think however that ddanbe your approach may have made me think of something easier to program and still do it rightly. o well, thnx everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.