Python Forum
Help with a query - to show possible formulas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with a query - to show possible formulas (/thread-30699.html)



Help with a query - to show possible formulas - Yanos - Nov-01-2020

Hi, thank you in advance for taking the time to read this query and help me.

I am very stuck with this problem!!!

I am trying to produce a query in Python3. Which will show all the possible calculations to a specific value using a restricted range of numbers.

So if the number i input ( N= 4 )

if my options are 1,3,4 the result would show:

1+1+1+1
3+1
1+3
4


Currently i have been able to find code to tell me the number of possibilities but not show me what they are; my code so far is:
def Sums134(n):

    DP = [0 for i in range(0, n + 1)]


    DP[0] = DP[1] = DP[2] = 1
    DP[3] = 2


    for i in range(4, n + 1):
        DP[i] = DP[i - 1] + DP[i - 3] + DP[i - 4]

    return DP[n]



n = 10
print (Sums134(n))



RE: Help with a query - to show possible formulas - jefsummers - Nov-02-2020

With a generalized solution, since you don't know if one of the numbers is 1, you need to find all of the permutations of the numbers, allowing repeats, of n numbers. Once you have the permutations, see how many add up to n.

Potential is a lot of permutations. If you have 3 numbers as in your example, and n=4 as in the example, you will have 3 1 digit options, 3 squared 2 digit options, 3 cubed 3 digit options, and 3 to the 4th power 4 digit options. Total is 3+9+27+81=120 combinations. Most will not add up to 4.