Dynamical Programming - Sum of numbers / Advanced Problem

In summary, this person is discussing a problem from Thermodynamics and suggesting ways to solve it computationally.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
Let us suppose we have an equation such that

$$N = \sum_{i=1}^N ix_i = x_1 + 2x_2 + 3x_3 + ...+Nx_N$$

and we also know that the solutions (i.e ##x_i##) ranges from ##\{0, N\}##.

For example, if ##N=4## we would have

$$x_1 + 2x_2 + 3x_3 + 4x_4 = 4$$

and ##x_1,x_2,x_3,x_4## will range from ##\{0, 4\}##. So the possible answers are
$$(x_1, x_2, x_3, x_4) = (0,0,0,1) / (1,0,1,0) / (0,2,0,0), (2, 1,0,0), (4,0,0,0)$$

Similarly for ##N=3## we would have

$$x_1 + 2x_2 + 3x_3 = 3$$

and ##x_1,x_2,x_3## will range from ##\{0, 3\}##. So the possible answers are
$$(x_1, x_2, x_3) = (0,0,1) / (1,1, 0) / (3,0,0)$$

I was studying Thermodynamics and this problem come to my mind which is actually used to describe the available states of the system (Note that this picture is about fermions, but my calculation works for the bosons. Nevertheless the idea is similar.)

1611000443647.png


I neglected the ##q=0## and actually in my question ##q \equiv N##. I thought this could be a nice problem to computationally solve. The first solution come to mind is to create an array that contains all the possible answers of ##x_i## but that means we need ##N^N## elements which becomes quite hard to process for the computer. But that's indeed a silly solution.

More clever one is well for whatever number we put on the sum of the solutions cannot exceed ##N## (i.e ##x_1 + x_2 + ...+x_N \leq N## . So that might reduce the permutation number by a great amount. But I am also open to other ideas.
 
Last edited:
Technology news on Phys.org
  • #2
It can be done by a quite small program

Python:
def search(list, target, maxi):
    global solutions

    if maxi > 0:
        for k in range (0, (target//maxi ) +1):
            if target == k * maxi:
                #print (solutions, list + [k])
                solutions+=1;
            else:
                search (list + [k],  target - k * maxi, maxi-1)

for i in range (2,51):
    solutions = 0
    search ([],i,i)
    print (i, solutions)

Calculating all the solutions up to N = 50 (204226 solutions) takes less than 10 seconds.
If you just want to calculate the number of solutions, you can get rid of the list parameter.

If you uncomment the print() statement, you can see the solutions.
I'm starting with the most significant X_i.
The solution [1] for N=4 means x_4 = 1, and the rest is 0.
 
  • Like
Likes Arman777
  • #3
It seems really nice, thanks
 
  • #4
Arman777 said:
It seems really nice, thanks
The number of solutions is equal to the number of partitions of N allowing repeats,
4 = 1+1+1+1 = 1+1+2 = 1 + 3 = 2 + 2. 5 solutions here also.
so there is lot of work done on this already
https://oeis.org/A000041
 

What is Dynamical Programming?

Dynamical Programming is a mathematical optimization technique used to solve complex problems by breaking them down into smaller subproblems and finding the optimal solution for each subproblem. The solutions to the subproblems are then combined to find the overall optimal solution.

What is the Sum of Numbers problem in Dynamical Programming?

The Sum of Numbers problem in Dynamical Programming is a classic problem where the goal is to find the sum of a given set of numbers. The unique aspect of this problem is that it can be solved using Dynamical Programming, which makes it an excellent example to understand the concept.

What makes the Sum of Numbers problem an advanced problem in Dynamical Programming?

The Sum of Numbers problem becomes an advanced problem in Dynamical Programming when there is a constraint added, such as finding the sum of numbers that are divisible by a certain number or finding the sum of numbers that are within a specific range. These constraints add an extra layer of complexity and require a deeper understanding of Dynamical Programming principles to solve.

What is the time complexity of solving the Sum of Numbers problem using Dynamical Programming?

The time complexity of solving the Sum of Numbers problem using Dynamical Programming is O(n), where n is the number of elements in the given set of numbers. This is because Dynamical Programming involves solving smaller subproblems, and the number of subproblems is directly proportional to the number of elements in the original problem.

Can Dynamical Programming be used to solve other types of problems?

Yes, Dynamical Programming can be used to solve a wide variety of problems, including optimization, scheduling, and routing problems. It is a powerful technique that can be applied to any problem that can be broken down into smaller subproblems with optimal solutions that can be combined to find the overall optimal solution.

Similar threads

Replies
3
Views
735
  • Precalculus Mathematics Homework Help
Replies
10
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
940
  • Precalculus Mathematics Homework Help
Replies
32
Views
844
  • Linear and Abstract Algebra
Replies
7
Views
824
  • Calculus and Beyond Homework Help
Replies
9
Views
797
Replies
24
Views
2K
  • Precalculus Mathematics Homework Help
Replies
5
Views
503
  • Set Theory, Logic, Probability, Statistics
Replies
28
Views
4K
Back
Top