-1

I have a number (for example 301, but can be even 10^11).

n = lenght of that number

I have to break it down to sum of max n components. Those components are 0^n, 1^n, 2^n, 3^n...9^n.

How can I do that?

  • 1
    Could you supply the value of `n` and the result for the example 301 please? – Bathsheba Apr 20 '22 at 17:41
  • 1
    This seems like a homework problem. Can you tell us what you've tried so far? – CoffeeTableEspresso Apr 20 '22 at 17:46
  • If `n` is the length then for 301 it would be 6^n + 4^n + 2(2^n) +5(1^n). – Jim Rhodes Apr 20 '22 at 18:01
  • 1
    Why is 0^n even included here? It's going to be 0 every time... – CoffeeTableEspresso Apr 20 '22 at 18:27
  • for 301 n=3, 1000 n=4, 10000 n=5 etc. For 301 my components are: 0, 1, 8, 27, 64, 125, 216... (0^3, 1^3, 2^3...9^3) And for example for 251 this HAVE TO be sum of 3 numbers from this list (216+27+8) I know this isn't possible for every number, but for given one it is. I've tried to check every possible combination, but for number like 10^11 it's just too many combination and program is too slow – MementoMori Apr 20 '22 at 19:15
  • So, for 301, there is no solution? – vish4071 Apr 20 '22 at 19:23

2 Answers2

2

Since you have 1^n included in your options, this becomes a really simple problem solvable through Greedy Approach.

Firstly, let me clarify that the way I understand this, for an input N of length n, you want some solution to this equation:

A.1^n + B.2^n + C.3^n + ... + H.8^n + I.9^n

There are infinitely many possible solutions (just by theory of equations). One possible solution can be found as follows:

a = [x ** n for x in range(0,10)]
consts = [0] * 10
ctr = 9
while N > 0:
    consts[ctr] = N // a[ctr]
    N = N % a[ctr]
    ctr -= 1
return consts

This consts array will have the constant values for the above equation at respective indices.

PS: I've written this in python but you can translate it to C++ as you want. I saw that tag later. If you have any confusion regarding the code, feel free to ask in the comments.

vish4071
  • 5,135
  • 4
  • 35
  • 65
  • O dear, sure...I'll rather incorporate `a's` range to have `0^n` as well, as OP mentioned `0^n` as well, even though it will always be redundant. – vish4071 Apr 20 '22 at 19:02
  • Thanks, maybe I didn't specify what i mean correctly. Only problem I can think of with this solution is that I can add up to n (length of that number) components, not more. – MementoMori Apr 20 '22 at 19:09
  • You need to explain your problem properly then. What is the expected output for your example (301)? – vish4071 Apr 20 '22 at 19:22
  • For what i can think of, for 301 there is not such solution, for 251 for example there is 6^3+3^3+2^3 or 5^3+5^3+1^3 (always 3 components, exactly my task is to find the biggest number i can make of those digits which powers I've added up (6+3+2 or 5+5+1) but when I'll get help with how to find it, I'll manage to do it) – MementoMori Apr 20 '22 at 19:27
  • So, doesn't it mean if the final solution has `sum of constants <= n`, then you can use that else there is no solution. – vish4071 Apr 20 '22 at 19:49
  • However, this won't be able to distinguish between multiple solutions. So, maybe we can think something about it. – vish4071 Apr 20 '22 at 19:50
  • Yea, and provided that there must be solution for given number, there is always sum of constants <=n, but i think that your solution can sometimes miss the correct answer, am I wrong? Because for example 4^n can "fit", but not necessary be used in correct solution – MementoMori Apr 20 '22 at 19:55
  • Thank you so much for your help btw – MementoMori Apr 20 '22 at 19:55
  • Yes, that's the issue with greedy approach. This will never be able to distinguish between 2 solutions if they exist, eg. my algo will never find `5,5,1` for `251` but will always give `632` as output. – vish4071 Apr 20 '22 at 20:12
0

You could use the following to determine the number of components.

    int remain = 301;   // Target number
    int exp = 3; // Length of number (exponent)
    int total = 0; // Number of components
    bool first = true; // Used to determinie if plus sign is output
    for ( int comp = 9; comp > 0; --comp )
    {
        int count = 0; // Number of times this component is needed
        while ( pow(comp, exp) <= remain )
        {
            ++total; // Count up total number of components
            ++count; // Count up number of times this component is used
            remain -= int(pow(comp, exp));
        }
        if ( count ) // If count is not zero, component is used
        {
            if ( first )
            {
                first = false;
            }
            else
            {
                printf(" + ");
            }
            if ( count > 1 )
            {
                printf("%d(%d^%d)", count, comp, exp);
            }
            else
            {
                printf("%d^%d", comp, exp);
            }
        }
    }
    if ( total == exp )
    {
        printf("\r\nTarget number has %d components", exp);
    }
    else if ( total < exp )
    {
        printf("\r\nTarget number has less than %d components", exp);
    }
    else
    {
        printf("\r\nTarget number has more than %d components", exp);
    }

Output for 301:

6^3 + 4^3 + 2(2^3) + 5(1^3)
Target number has more than 3 components

Output for 251:

6^3 + 3^3 + 2^3
Target number has 3 components
Jim Rhodes
  • 5,021
  • 4
  • 25
  • 38