permutations of 4 numbers in 7 positions IF the sum of the numbers is 600

3 views (last 30 days)
I want to generate all permutations of 4 numbers in 7 places, but we have a condition that the sum of the 7 places should be 600.
my nambers are 100,125,75,50
It is clear that repetition is allowed.
How can i do that?

Answers (2)

the cyclist
the cyclist on 11 May 2022
This is a "making change" problem, where the smaller numbers can represent coins, and the large number is the total of the change. You can use the makeChange function from the File Exchange to do this.
Below, I've generate all possible combinations. It wasn't clear to me if you need up to 7 numbers, or exactly 7 numbers, but you should be able to trim the list accordingly.
[count, combos] = makeChange(600,[125,100,75,50])
count = 41
combos = 41×4
4 1 0 0 4 0 0 2 3 1 1 1 3 0 3 0 3 0 1 3 2 2 2 0 2 2 0 3 2 1 2 2 2 1 0 5 2 0 4 1

Matt J
Matt J on 11 May 2022
Edited: Matt J on 11 May 2022
See the attached file.
combos = diophantine(ones(1,7),600,[0,100,125,75,50])
combos = 4585×7
125 125 125 125 100 0 0 125 125 125 100 125 0 0 125 125 100 125 125 0 0 125 100 125 125 125 0 0 100 125 125 125 125 0 0 125 125 125 125 0 100 0 100 100 100 100 100 100 0 75 125 100 100 100 100 0 125 75 100 100 100 100 0 75 100 125 100 100 100 0
all(sum(combos,2)==600)
ans = logical
1
  2 Comments
Matt J
Matt J on 11 May 2022
Don't they?
combos = diophantine(ones(1,7),600,[0,100,125,75,50]);
all(sum(combos,2)==600)
ans = logical
1

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!